Translate labels in FormType

穿精又带淫゛_ 提交于 2019-12-12 03:37:48

问题


How can I translate labels values in Form builder.

Example:

->add('google_analytics_key', TextType::class, [
                'label' => 'Analytics Key'
            ])

the "Analytics Key" is the value for the default locale.

I am using the form with rows:

{{ form_row(myForm.google_analytics_key) }}

This renders the label with input type as well, so I cannot use the trans command.

Is there something built in Symfony/Twig or I must implement the form manually?


回答1:


You can add the domain of your translation and the key

For example:

->add('google_analytics_key', TextType::class, [
    'translation_domain' => '<your file name>',//for example 'messages'
    'label' => 'app.analytics_key',
            ])



回答2:


This is for yml configuration.

First Check:

app/config/config.yml

framework: translator: { fallbacks: [en] }

Then Inside translations folder: add your transalation file and add:::

messages.en.yml

test: Analytics Key

and last just add the reference:

->add('google_analytics_key', TextType::class, [
            'label' => 'test'
        ])


来源:https://stackoverflow.com/questions/38173571/translate-labels-in-formtype

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