0

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.

FranMercaes
  • 151
  • 1
  • 1
  • 12
  • I found something that is solving my problem for now until I find a more elegant way to do it. Take a look at the @Remi Tomas answer. [How to translate form labels in Zend Framework 2?](http://stackoverflow.com/questions/15961628/how-to-translate-form-labels-in-zend-framework-2) – FranMercaes Mar 25 '15 at 16:20
  • Do You have translations for words "Home" and "Users" in translation file ? – newage Mar 25 '15 at 16:52
  • poedit didn't recognize the navigation labels – FranMercaes Mar 25 '15 at 17:18

6 Answers6

1

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.

Danijel
  • 12,408
  • 5
  • 38
  • 54
0

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',
        ),
    ),
),
Stefano Corallo
  • 321
  • 2
  • 3
0

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

Saeven
  • 2,280
  • 1
  • 20
  • 33
0

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.

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
0

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);
Sudo
  • 95
  • 1
  • 8
0

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;
            },
        ]
);

Bigmwaj
  • 361
  • 2
  • 7