Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by
  1. Home
  2. Documentation
  3. Bundles
  4. SonataAdminBundle
  5. Translation

Translation

Edit this page

There are two main translation domains in an Admin class:

  • SonataAdminBundle: this domain is used to translate shared messages across different Admins
  • messages: this domain is used to translate the messages for the current Admin

Ideally the messages domain should be changed to avoid any issues with other Admin classes.

You can configure the translation domain for the Admin class by injecting the value through the container:

1
2
3
4
5
6
7
8
<!-- config/services.xml -->

<service id="sonata.page.admin.page" class="Sonata\PageBundle\Admin\PageAdmin">
    <tag name="sonata.admin" model_class="Application\Sonata\PageBundle\Entity\Page" manager_type="orm" group="sonata_page" label="Page"/>
    <call method="setTranslationDomain">
        <argument>SonataPageBundle</argument>
    </call>
</service>

An Admin instance always gets the translator instance, so it can be used to translate messages within the configureFields method or in templates.

1
2
3
4
5
6
7
8
{# the classical call by using the twig trans helper #}
{{ 'message_create_snapshots'|trans({}, 'SonataPageBundle') }}

{# by using the admin trans method with hardcoded translation domain #}
{{ 'message_create_snapshots'|trans({}, 'SonataPageBundle') }}

{# by using the admin trans with the configured translation domain #}
{{ 'message_create_snapshots'|trans({}, admin.translationdomain) }}

The last solution is most flexible, as no translation parameters are hardcoded, and is the recommended one to use.

Translate field labels

The Admin bundle comes with a customized form field template. The most notable change from the original one is the use of the translation domain provided by either the Admin instance or the field description to translate labels.

Overriding the translation domain

The translation domain can be overridden at either the form group or individual field level.

If a translation domain is set at the group level it will cascade down to all fields within the group.

Overriding the translation domain is of particular use when using Customize admin, where the extension and the translations would be defined in one bundle, but implemented in many different Admin instances.

Setting the translation domain on an individual field:

1
2
3
4
5
6
7
8
9
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;

$form
    ->with('form.my_group')
        ->add('publishable', CheckboxType::class, [], [
            'translation_domain' => 'MyTranslationDomain',
        ])
    ->end()
;

The following example sets the default translation domain on a form group and over-rides that setting for one of the fields:

1
2
3
4
5
6
7
8
9
10
11
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\DateType;

$form
    ->with('form.my_group', ['translation_domain' => 'MyDomain'])
        ->add('publishable', CheckboxType::class, [], [
            'translation_domain' => 'AnotherDomain',
        ])
        ->add('start_date', DateType::class, [], [])
    ->end()
;

Translation can also be disabled on a specific field by setting translation_domain to false.

Setting the label name

By default, the label is set to a sanitized version of the field name. A custom label can be defined as the third argument of the add method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// src/Admin/PageAdmin.php

final class PageAdmin extends AbstractAdmin
{
    protected function configureFormFields(FormMapper $form): void
    {
        $form
            ->add('isValid', null, [
                'required' => false,
                'label' => 'label.is_valid',
            ])
        ;
    }
}

Label strategies

There is another option for rapid prototyping or to avoid spending too much time adding the label key to all option fields: Label Strategies. By default labels are generated by using the following rule:

isValid => Is Valid

The AdminBundle comes with different key label generation strategies:

  • sonata.admin.label.strategy.native: DEFAULT - Makes the string human readable
    isValid => Is Valid
  • sonata.admin.label.strategy.form_component: The default behavior from the Form Component
    isValid => Isvalid
  • sonata.admin.label.strategy.underscore: Changes the name into a token suitable for translation by prepending "form.label" to an underscored version of the field name isValid => form.label_is_valid
  • sonata.admin.label.strategy.noop: does not alter the string
    isValid => isValid

sonata.admin.label.strategy.underscore will be better for i18n applications and sonata.admin.label.strategy.native will be better for native (single) language applications based on the field name. It is reasonable to start with the native strategy and then, when the application needs to be translated using generic keys, the configuration can be switched to underscore.

The strategy can be quickly configured when the Admin class is registered in the Container:

1
2
3
4
5
6
7
8
9
10
11
12
<!-- config/services.xml -->

<service id="app.admin.project" class="App\Admin\ProjectAdmin">
    <tag
        name="sonata.admin"
        model_class="App\Entity\Project"
        manager_type="orm"
        group="Project"
        label="Project"
        label_translator_strategy="sonata.admin.label.strategy.native"
        />
</service>

Note

In all cases the label will be used by the Translator. The strategy is a quick way to generate translatable keys. It all depends on the project's requirements.

Note

When the strategy method is called, context (breadcrumb, datagrid, filter, form, list, show, etc.) and type (usually link or label) arguments are passed. For example, the call may look like: getLabel($label_key, 'breadcrumb', 'link')

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    Online Symfony certification, take it now!

    Online Symfony certification, take it now!

    Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

    Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

    Version:

    Table of Contents

    • Translate field labels
      • Overriding the translation domain
      • Setting the label name
      • Label strategies

    Symfony footer

    Avatar of Matthias Schmidt, a Symfony contributor

    Thanks Matthias Schmidt for being a Symfony contributor

    2 commits • 6 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • What is Symfony?
      • Symfony at a Glance
      • Symfony Components
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • Symfony Community
      • SymfonyConnect
      • Events & Meetups
      • Projects using Symfony
      • Contributors
      • Symfony Jobs
      • Backers
      • Code of Conduct
      • Downloads Stats
      • Support
    • Blog

      • All Blog Posts
      • A Week of Symfony
      • Case Studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Living on the edge
      • Releases
      • Security Advisories
      • Symfony Insight
      • Twig
      • SensioLabs Blog
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Powered by

    Follow Symfony