ZF2 navigation translate navigation labels and titles

1.2k Views Asked by At

I'm trying to find a way to translate the labels and titles. According to he ZF2 Manual all I have to do this:

The navigation helpers support translation of page labels and titles. You can set a translator of type Zend\I18n\Translator in the helper using $helper->setTranslator($translator).

So my navigation looks like this in config file

return array(
    'navigation' => array(
        'default' => array(
            array(
                'label' => 'Home',
                'route' => 'home',
            ),
            array(
                'label' => 'Users',
                'route' => 'tar-users',
            ),
        ),
    ),
);

This is in global.php

return array(
    'service_manager' => array(
        'factories' => array(
            'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
        ),
    ),

    //...
);

And in my layout I have:

<?php echo $this->navigation('navigation')
        ->setTranslator($this->plugin('translate')->getTranslator())
        ->menu()
        ->setUlClass('nav navbar-nav')
        ->setMaxDepth(0)
    ?>

The navigation is working but is not translating. I'm sure I missed something or I did something wrong. I'd like to know what. I don't what you to write the code just some tips.

Thank you.

6

There are 6 best solutions below

1
Stefano Corallo On

do you setup the translator service and locale in your module.config.php? something like this:

'translator' => array(
    'locale' => 'it_IT',
    'translation_files' => array(
        array(
            'type'     => 'phpArray',
            'filename' => __DIR__ . '/../language/Zend_Captcha.php',
            'text_domain' => 'default'
        ),
        array(
            'type'     => 'phpArray',
            'filename' => __DIR__ . '/../language/Zend_Validate.php',
            'text_domain' => 'default'
        ),
    ),
    'translation_file_patterns' => array(
        array(
            'type'     => 'gettext',
            'base_dir' => __DIR__ . '/../language',
            'pattern'  => '%s.mo',
        ),
    ),
),
0
Saeven On

I've run into a puzzling circumstance where I had also set everything up correctly, but the translation wouldn't execute...because the proper locale wasn't installed on the OS. I was trying to translate en_US into fr_CA. This quickly fixed things for me:

sudo locale-gen fr_CA sudo locale-gen fr_CA.UTF-8

3
Danijel On

Zend uses .po files in Application/language folder, and you need to use poedit to edit gettext translations. Poedit scans the .php ( or .phtml files in this case ) for phrases to translate, more precisely, seeks for the translate function name ( translate in this case ). Check HERE how to configure poedit for Zend, and HERE is simple solution for translating menu labels. I have just started learning Zend, i'm not sure if this is the right approach, but it is the simplest solution that I encountered. You just need to add label values as $translator->translate( ... ) in Application/config/module.config.php, an configure translator ( in skeleton application, en_US locale, and gettext type ). After that open .po file and refresh catalog, new lines should be added for translation ( also, you can't, and should not add new translations that are not found automatically by the program ). I have tested the above, and menu is translated automatically without setTranslator( ... ) call.

0
Allan Mineiro On

I had the same problem, and here is my solution. (Using gettext)

Example:

module.config.php

array (
    'label' => _('Home'),
    'route' => 'home'
),

In the View, you will use something like this:

$this->translate(HERE_COMES_THE_ITENS_OF_NAVIGATION)

In my case I use this:

foreach ( $this->container as $page ) {
   $this->translate($page->getlabel ());
}

In the POEDIT program, you will insert two sources keywords

1 - translate
2 - _

The "_" is to map the keyword by POEDIT, and ZEND uses the "translate" to replace it by the value you put inside the POEDIT.

0
Sudo On

Zend comes with translation on navigation but not before rendering. This snippet will do the trick. (Zend 3)

$sm = $event->getApplication()->getServiceManager();
$vhm = $sm->get('ViewHelperManager');
$translator = $sm->get('translator');
$vhm->get('navigation')->setTranslator($translator);
0
Bigmwaj On

Review the script in your module.config.php

// module.config.php
fufunction _(string $msg){return $msg}

function _(string $msg){return $msg}

return array(
    'navigation' => array(
        'default' => array(
            array(
                'label' => _('Home'),
                'route' => 'home',
            ),
            array(
                'label' => _('Users'),
                'route' => 'tar-users',
            ),
        ),
    ),
    'view_helpers' => [
        'aliases' => [
            '_'                 => Translate::class,
            'numberFormat'      => NumberFormat::class,
            'formLabel'         => FormLabel::class,
            'formSubmit'        => FormSubmit::class,
            'menu'              => Navigation::class,
        ],

        'factories' => [

            Translate::class => function($container) {
                $instance = new Translate();
                $instance->setTranslator($container->get(Translator::class));
                return $instance;
            },

            FormLabel::class => function($container) {
                $instance = new FormLabel();
                $instance->setTranslator($container->get(Translator::class));
                return $instance;
            },

            FormSubmit::class => function($container) {
                $instance = new FormSubmit();
                $instance->setTranslator($container->get(Translator::class));
                return $instance;
            },

            Navigation::class => function($container) {
                $instance = new Navigation();
                $instance->setTranslator($container->get(Translator::class));
                $instance->setServiceLocator($container);
                return $instance;
            },
        ]
);