Search code examples
formssymfonydnstranslation

symfony form type translation_domain not working?


i have form type like:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name', null, [
            'translation_domain' => 'Admin.Global',
        ])
        ->add('isoCode', null, [
            'translation_domain' => 'Admin.Global',
        ])
        ->add('languageCode')
        ->add('flag_image', FileType::class, [
            'mapped' => false,
            'label' => 'flag',
            'constraints' => [
                new Image([
                    'maxSize' => '1024K'
                ])
            ],
            'required' => false,
            'translation_domain' => 'Admin.Global',
        ])
        ->add('active', CheckboxType::class, [
            'label_attr' => [
                'class' => 'switch-custom'
            ]
        ])
        ->add('isDefault', CheckboxType::class, [
            'label_attr' => [
                'class' => 'switch-custom'
            ]
        ]);
}

after i run command:

php bin/console translation:update --force en(or zh_CN)

only 'name' label in Admin.Global+intl-icu.en(or zh_CN).xlf file

why the isocode and flag not on xlf file? and do i add 'translation_domain' => 'Admin.Global', to each formtype?


Solution

  • The problem is that the translation:update command, according to the docs, extracts strings to translate from 2 locations only:

    • templates
    • any PHP file/class that injects or autowires the translator service and makes calls to the trans() method.

    Your form type class is neither of these two, so it is not processed.

    You mentioned that the name label gets into xlf file, while other labels do not. Obviously, name got there from some other place (from a template, for example), not from the form class.

    To process form type classes you can install JMSTranslationBundle. It provides a different command, translation:extract, which among other things supports extracting messages from (a quote from docs):

    all form labels that are defined as options to the ->add() method of the FormBuilder