The generator.yml
power for your
Symfony2
backends

Table of contents
    Fork me on GitHub

    Internationalization

    This chapter assumes you are familiar with the way Symfony2 deals with internationalization. If not, please read translation chapter first.

    General messages

    Common translations like "Add", "Save" or "Are you sure?" that are shared among all generated admin screens are put into Admingenerator trasnlation domain.

    Currently supported languages are English, French, German, Italian, Polish, Portuguese, Russian, Slovenian, Spanish and Ukrainian. If your language is not listed above please contribute!

    Notice
    Translation files can be found in Admingenerator/GeneratorBundle/Resources/translations/Admingenerator.xx.yml.

    Bundle specific messages

    As you develop you will most likely need to add some translations that are specific to your bundle. Not a problem! All you need to do is tell Admingenerator which translation domain (i18n catalogue) it should use.

    generator: admingenerator.generator.xxxx
    params:
      i18n_catalog: YourTranslationDomain

    This will tell Admingenerator to look for translations in:

    • ../Resources/translations/YourTranslationDomain.xx.yml
    • ../Resources/translations/YourTranslationDomain.xx.xliff

    Notice
    If not set, the default domain will be "Admin".

    Translation parameters

    Let's assume we're building online book store app. Our edit book screen title will probably look like You're editing written by !.

    builders:
      edit:
        params:
          title: You're editing  written by !
          display: [ title, author, publishedAt ]
          actions:
            list: ~
      delete: ~

    In order to translate that we need add i18n_catalog to our generator.yml file:

    generator: admingenerator.generator.doctrine
    params:
      model: Acme\BookstoreBundle\Entity\Book
      namespace_prefix: Acme
      bundle_name: BookstoreBundle
      i18n_catalog: AcmeBookstoreBundle

    Finally, create translation file for your messages:

    • ../Resources/translations/AcmeBookstoreBundle.pl.yml

    Inline parameters

    To adress inline parameters like ` in our exampleYou're editing written by !you simply replace the twig tag opening and closing brackets with%` (percent) signs.

    # AcmeBookstoreBundle.pl.yml
    "You're editing %Book.title% written by %Book.author.name%!":    "Edytujesz %Book.title% autorstwa %Book.author.name%!"

    Notice
    This type of translation is not encouraged, it was introduced to keep backwards compability.

    Parameter bag with full syntax

    To pass parameters along with message you need to add parameter bag.

    Admingenerator requiers a very strict syntax in order to succesfully read parameters.

    • Parameter bag is always appended to original message
    • Parameter bag begins with |{ and ends with }| (spaces after opening and before closing brackets are important!)
    • Parameters in parameter bag are seperated by , (comma and space)
    • Parameter keys and values can consist only of a-z, A-Z, 0-9 and . (dot) characters

    The full syntax approach let you name your parameters:

    String: You're editing %book% written by %author%!|{ %book%: Book.title, %author%: Book.author.name }|.

    # AcmeBookstoreBundle.pl.yml
    "You're editing %book% written by %author%!":    "Edytujesz %book% autorstwa %author%!"

    Or, if you prefer keyword messages translation:

    String: book.edit.title|{ %book%: Book.title, %author%: Book.author.name }|

    # AcmeBookstoreBundle.pl.yml
    book.edit.title:    "Edytujesz %book% autorstwa %author%!"

    Parameter bag with abbreviated syntax

    Some might feel like the key/value approach makes the parameter bag too long. Don't worry! You can always use the abbreviated syntax where parameter value is also it's key:

    String: You're editing %Book.title% written by %Book.author.name%!|{ Book.title, Book.author.name }|

    # AcmeBookstoreBundle.pl.yml
    "You're editing %Book.title% written by %Book.author.name%!":    "Edytujesz %Book.title% autorstwa %Book.author.name%!"

    Or, if you prefer keyword messages translation:

    String: book.edit.title|{ Book.title, Book.author.name }|

    # AcmeBookstoreBundle.pl.yml
    book.edit.title:    "Edytujesz %Book.title% autorstwa %Book.author.name%!"

    Notice
    My personal favourite is keywords messages with abbreviated parameter bag syntax. This approach keeps generator.yml relatively clean and abstracts the generator from actual content (MVC FTW!).

    Generator file example

    For a quick reference here is an example:

    • Acme/DemoBundle/Resources/config/Baloon-generator.yml
    generator: admingenerator.generator.doctrine
    params:
      model: Acme\DemoBundle\Entity\Baloon
      namespace_prefix: Acme
      bundle_name: DemoBundle
      i18n_catalog: AcmeDemoBundle # tells Admingenerator to look for translations in AcmeDemoBundle.xx.yml
      fields:
        id:
          label:  ID
        name:
          label:  baloon.name.label
        color:
          label:  baloon.color.label
          help:   baloon.color.help
        sandbags:
          formType: collection
          extras:
            new_label: baloon.sandbags.new.label
          addFormOptions:
            type: \Acme\DemoBundle\Form\Type\Baloon\EmbedSandbagType
            allow_add: true
            allow_delete: true
            by_reference: false
    builders:
      list:
        params:
          title: baloon.title.list
          display: [ id, name, color ]
          actions:
            new: ~
          batch_actions:
            delete: 
              label:    baloon.delete.label
              confirm:  baloon.delete.confirm
            myCustomAction:
              label:    baloon.myCustomAction.label
              confirm:  baloon.myCustomAction.confirm
          object_actions:
            edit: ~
            delete: ~
          max_per_page: 12
      filters:
        params:
          display: ~
      new:
        params:
          title: baloon.title.new
          display: [ name, color, sandbags ]
          actions:
            list: ~
      edit:
        params:
          title: baloon.title.edit|{ Baloon.name }| 
          display: [ name, color, sandbags ]
          actions:
            list: ~
      delete: ~

    Admin Menu translation

    Before you start translating menu, make sure you have told Admingenerator where to look for your admin menu.

    app/config.yml :

    admingenerator_generator:
      knp_menu_class: Acme\DemoBundle\Menu\AdminMenu

    AdminMenu is based on KnpMenuBundle. To translate KnpMenuBundle you need to overwrite the default menu template.

    src: app/config.yml

    knp_menu:
      twig:
        template: AcmeDemoBundle::knp_menu.html.twig  # your custom knp_menu template

    src: Acme/DemoBundle/Resources/views/knp_menu.html.twig

    {% extends "knp_menu.html.twig" %}
    {% block label %}
      {{ item.label|trans(item.getExtra("translation_params", {}, item.getExtra("translation_domain", "messages")) " }}
    {% endblock %}

    Now your KnpMenuBundle is configured and will accept "translation_domain" extra parameter. Translation domain is simply i18n catalogue you want to use for translation purposes.

    src: Acme/DemoBundle/Menu/AdminMenu.php

    <?php
    
    public function createAdminMenu(Request $request)
    {
      $menu = $this->factory->createItem('root');
    
      $menu->setchildrenAttributes(array('id' => 'main_navigation', 'class'=>'menu'));
    
      $baloons = $menu->addChild('admin.menu.baloon.label', array('uri' => '#'))
                      ->setExtra('translation_domain', 'AcmeDemoBundle');
      $baloons->setLinkAttributes(array('class'=>'sub main'));
      $baloons->addChild('admin.menu.baloon.baloons.label', array('route' => 'Acme_DemoBundle_Baloon_list'))
              ->setExtra('translation_domain', 'AcmeDemoBundle');
      $baloons->addChild('admin.menu.baloon.sandbags.label', array('route' => 'Acme_DemoBundle_Sandbag_list'))
              ->setExtra('translation_domain', 'AcmeDemoBundle');
    }

    This will tell Admingenerator to look for translations in:

    • ../Resources/translations/AcmeDemoBundle.xx.yml
    • ../Resources/translations/AcmeDemoBundle.xx.xliff