0

I'm using Symfony 2.3 and the KnpMenuBundle.

Is it possible to use a translation domain for menu items?

Like this:

$menu['management']->addChild(
    'msg.user.list',
    array(
        'route' => 'user_list',
        'translation_domain' => 'navigation'
    )
);
Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
smartcoderx
  • 1,021
  • 2
  • 14
  • 32

1 Answers1

6
  1. According to the Symfony documentation and KnpMenuBundle documentation,you may set the translation domain (menu in my snippets), while adding menu items in your MenuBuilder class:

    $menu->addChild('Home', array('route' => 'homepage'))
         ->setExtra('translation_domain', 'menu');
    
  2. You may better want to add the translation domain into the whole menu instead:

    $menu = $this->factory->createItem('root')
                 ->setExtra('translation_domain', 'menu');
    
  3. Then create a file named knp_menu.html.twig in:

    app/Resources/views/menu/
    
  4. and put this in it:

    {% extends 'knp_menu.html.twig' %}
    
    {% block label %}
        {% if options.allow_safe_labels and item.getExtra('safe_label', false) %}
            {{ item.label | raw | trans(item.getExtra('translation_params', {}), item.getExtra('translation_domain', 'menu'))}}
        {% else %}
            {{ item.label | trans(item.getExtra('translation_params', {}), item.getExtra('translation_domain', 'menu')) }}
        {% endif %}
    {% endblock %}
    

    (If the file already exists, just replace the {% block label %}{% endblock %} part)

  5. Be carefull to have translation files (.xliff or whatever) naming strategy like:

    {translation_domain}.{locale}.{extenstion}
    

    for example:

    menu.fa.xliff
    

    In this path:

    app/Resources/translations/
    
  6. The last part is:

    # app/config/config.yml
    knp_menu:
        twig:
            template: knp_menu.html.twig
    
  7. Up-Vote this nice tutorial.
  8. Have fun!
Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100