Fabiano Palaoro
Fabiano Palaoro

Reputation: 267

translate don't work

I have the following code in config.yml:

framework:
#esi:             ~
translator:     { fallback: pt_BR }
secret:          %secret%
charset:         UTF-8
router:          { resource: "%kernel.root_dir%/config/routing.yml" }
form:            true
csrf_protection: true
validation:      { enable_annotations: true }
templating:      { engines: ['twig'] } #assets_version: SomeVersionScheme
session: 
    default_locale: pt_BR
    auto_start: true

In MyBundle/Resources/translations my file is: MyBundle.pt_BR.yml.

And one example of translate in the file is:

form_my_height: "Altura"

In my Form:

$builder->add('height')

When I clear cache and refresh page first time, the translation is load, but when I go to the other page it stops running.

Upvotes: 0

Views: 516

Answers (3)

Fusselchen
Fusselchen

Reputation: 382

or you can let the template translate your labels

php:

$builder->add('height', 'text', array('label' => 'form_my_height');

twig:

{{ form_label(form.height)|trans }}
{{ form_widget(form.height) }}
{{ form_errors(form.height) }}

Upvotes: 1

Mike
Mike

Reputation: 2374

Perhaps try:

$builder->add('height', 'text', array('label' => 'form_my_height', 'translation_domain' => 'MyBundle'));

Upvotes: 0

Reza S
Reza S

Reputation: 9738

I believe you have to go:

$builder->add($this->get('translator')->trans('form_my_height'))

If you're in a controller, if not you need to pass the translator service reference, and:

$builder->add($translator->trans('form_my_height'))

Upvotes: 0

Related Questions