How to translate symfony form error messages?

柔情痞子 提交于 2020-02-25 06:17:26

问题


for symfony standalone translation component I use:

$translator = new Translator('fr_FR');
$translator->addLoader('php', new \Symfony\Component\Translation\Loader\PhpFileLoader());
$translator->addResource('php', ROOT.'/translations/messages.fr.php', 'fr_FR');
$twig->addExtension(new TranslationExtension($translator));

It is working fine to translate symfony form labels. To translate symfony form constraint errors I use:

->add('firstname', TextType::class, [
        'constraints' => [new Assert\Length(['min' => 3, 
                                                                   'minMessage' => 'name.short',
                                                                 ])
                                  ]
    ])

and in messages.fr.php

I have

return [
    .....
    ...
    'name.short' => 'Name is short',
];    

other form labels are in this catalog too. labels are translated fine but for form error, I get name.short instead of its value. What mistake I did? Is it looking for another place to find the translation catalog?

EDIT: for twig I use this without cache parameter,

$twig = new \Twig\Environment($loader, [
    'strict_variables' => true,
    'optimizations' => -1,
    'debug' => true,
]);    

and it seems no twig cache is set, for standalone symfony form and validator I did not set any cache, for sure I checked directories, no cache is set. What else should I check?

EDIT2:

I used this too:

$translator = new Translator('fr_FR');
$translator->addLoader('php', new \Symfony\Component\Translation\Loader\PhpFileLoader());
$translator->addResource('php', ROOT.'/translations/validators.fr.php', 'fr_FR');
$translator->addResource('php', ROOT.'/translations/messages.fr.php', 'fr_FR');
$twig->addExtension(new TranslationExtension($translator));

and in translations/validators.fr.php I have a returning array with element name.shor like above, but still doesn't work.


回答1:


The default domain for validation message is not the default “message”

Try to add a new ressource validators.fr.php, and put the messages in this file.

See documentation for the framework but I think it’s the same process for standalone components https://symfony.com/doc/current/validation/translations.html




回答2:


I guess you have to try the basic way to translate texts.

use Symfony\Contracts\Translation\TranslatorInterface;

class MyFormType extends AbstractType
{
    private $translator;

    public function __construct(TranslatorInterface $translator) 
    {
        $this->translator = $translator;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('firstname', TextType::class, [
                'constraints' => [new Assert\Length([
                    'min' => 3, 
                    'minMessage' => $translator->trans('name.short'),
                ])
            ]

        // ...


来源:https://stackoverflow.com/questions/58366138/how-to-translate-symfony-form-error-messages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!