Q
Q
Qixing2015-01-29 09:11:32
symfony
Qixing, 2015-01-29 09:11:32

How to enable translation in symfony for forms?

I did everything according to the instructions.
app/config.yml file

framework:
    #esi:             ~
    translator:      { fallback: "%locale%" }
    secret:          "%secret%"
    router:
        resource: "%kernel.root_dir%/config/routing.yml"
        strict_requirements: ~
    form:            ~
    csrf_protection: ~
    validation:      { enable_annotations: true }
    templating:
        engines: ['twig']
        assets_version: 0.1a
    default_locale:  "%locale%"

The parameters.yml locale contains
locale: ru
My messages.ru.yml which is src/MyBundle/Resources/translations/messages.ru.yml
form:
    maker: Производитель
    category: Раздел

Form Generator
public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add(
                'maker',
                'entity',
                array(
                    'class' => 'MyBundle:ItemMaker',
                    'property' => 'name',
                    'empty_value' => '-- Все --'
                )
            )

        ->add(
        'category',
        'entity',
        array(
            'class' => 'MyBundleCategory',
            'property' => 'name',
            'empty_value' => '-- Все --'
        )
    );
    }

We check with a debugger:
dcc8a60aed464ac493782c2429f7e424.png
I make a form output:
<div class="filterForm__fld">
                  
                    <div class="filterForm__fld__title">
                        {{ form_label(form.maker) }}
                    </div>

                        {{ form_widget(form.maker, { 'attr': {'class': 'select select_t1 w100p'} }) }}
                </div>
                <div class="filterForm__fld">
                    <div class="filterForm__fld__title">
                        {{ form_label(form.category) }}
                    </div>
                    {{ form_widget(form.category, { 'attr': {'class': 'select select_t1 w100p'} }) }}
                </div>

As a result, the labels {{ form_label(form.maker) }} are not translated, so the maker and category are displayed

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Pavlov, 2015-01-29
@Qixing

do not make the "forms:" section in messages.ru.yml, put the translation directly as it is written on the page. You have maker and category fields in the form, and they are displayed on the page as Maker and Category. So?
Write in translation like this:

Maker: Создатель
Category: Категория

Well, if you want to add the form prefix, then add the label attribute to the form description:
$builder
        ->add(
            'maker',
            'entity',
            array(
                'label' => 'form.maker',
                'class' => 'MyBundle:ItemMaker',
                'property' => 'name',
                'empty_value' => '-- Все --'
            )
        )

        ->add(
            'category',
            'entity',
            array(
                'label' => 'form.category',
                'class' => 'MyBundleCategory',
                'property' => 'name',
                'empty_value' => '-- Все --'
            )
);

that is, specify the label in the same form as the framework sees it in the translation file. But then don't forget to add the English translation too! (if you need it)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question