简体   繁体   中英

How to properly translate strings in SonataAdminBundle

I am trying to translate some Sonata Admin - list page - strings using .xliff file inside my bundle. Those strings belongs to my bundle and not to Sonata so this are the steps I have follow:

  • Create a .xliff file under: src/Clanmovil/PlatformBundle/Resources/translations/PlatformBundle.es.xliff as follow:

     <?xml version="1.0"?> <xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2"> <file source-language="en" datatype="plaintext" original="PlatformBundle.en.xliff" > <body> <trans-unit id="name"> <source>name</source> <target>Nombre</target> </trans-unit> <trans-unit id="description"> <source>description</source> <target>Descripción</target> </trans-unit> <trans-unit id="active"> <source>active</source> <target>Activo?</target> </trans-unit> </body> </file> </xliff> 
  • Setup label property under src/Clanmovil/PlatformBundle/Controller/Admin/CategoryAdmin.php as follow:

     class CategoryAdmin extends Admin { ... // Fields to be shown on lists protected function configureListFields(ListMapper $listMapper) { $listMapper ->add('name', null, array( 'label' => 'name' )) ->add('active', null, array( 'label' => 'active' )); ... } ... } 
  • Setup app/config/config.yml as follow:

     parameters: locale: es framework: #esi: ~ translator: { fallbacks: ["%locale%"] } default_locale: "%locale%" .... 

And is not working I am still seeing name and active strings on list page and I don't know what I am missing. I have read several posts as this , this and many more. Can any give me some advice?


Since the previous issue was fixed with the answer provided but this is almost the same I decide to edit the OP than create a new one so here we go. Take a look to the following setup for SonataAdmin:

sonata_admin:
    dashboard:
        groups:
            configuration:
                icon: <i class="fa fa-lg fa-fw fa fa-folder"></i>
                label: configuration
                items:
                    - sonata.admin.category
                    - sonata.admin.command
                    - sonata.admin.alias

And this is the definition of those services at Clanmovil/PlatformBundle/Resources/config/services.yml :

services:
    sonata.admin.alias:
        class: Clanmovil\PlatformBundle\Controller\Admin\AliasAdmin
        tags:
            - { name: sonata.admin, manager_type: orm, group: "Configuration", label: "alias" }
        arguments:
            - ~
            - Clanmovil\PlatformBundle\Entity\Alias
            - ~
    sonata.admin.category:
        class: Clanmovil\PlatformBundle\Controller\Admin\CategoryAdmin
        tags:
            - { name: sonata.admin, manager_type: orm, group: "Configuration", label: "category" }
        arguments:
            - ~
            - Clanmovil\PlatformBundle\Entity\Category
            - ~
    sonata.admin.command:
        class: Clanmovil\PlatformBundle\Controller\Admin\CommandAdmin
        tags:
            - { name: sonata.admin, manager_type: orm, group: "Configuration", label: "command" }
        arguments:
            - ~
            - Clanmovil\PlatformBundle\Entity\Command
            - ~ 

But strings configuration , command and category (for now) aren't translated, see pic below:

在此处输入图片说明

Why? Those translations are on the same file as the alias one. What could be happening here?

You have to specify translation domain explicitly, if you create your translations files different than messages . messages is the default translation domain. You have two options, rename your PlatformBundle.es.xliff to messages.es.xliff or specify tranlation domain. example:

In form types:

...
$builder->add('name', 'text', ['translation_domain' => 'PlatformBundle']);
...

in twig trmplates:

{{ 'name'|trans({}, 'PlatformBundle') }}

I don't know what is the ListMapper class, but there should be support (I guess) for translation domains also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM