devanerd
devanerd

Reputation: 365

Translating Breadcrumbs (View Helper) on Zend Framework 2

I´m working in a Zend Framework 2 project and I´m using the View Helper (Breadcrumbs) to inject this navigation component into my views.

To render my Breadcrumbs the following code is used:

<?php echo $this->navigation('Navigation')
                ->breadcrumbs()
                ->setLinkLast(false)               // link last page
                ->setMaxDepth(7)                   // stop at level 7
                ->setMinDepth(0)                   // start at level 0
                ->setSeparator(' »' . PHP_EOL);    // separator with newline
?>

I´ve been translating most of the project´s content with the following code

<?php echo $this->translate("Text to translate here", $textDomain); ?>

So applying the same logic to the existing code:

<?php echo $this->translate($this->navigation('Navigation')
        ->breadcrumbs()
        ->setLinkLast(false)               // link last page
        ->setMaxDepth(7)                   // stop at level 7
        ->setMinDepth(0)                   // start at level 0
        ->setSeparator(' »' . PHP_EOL), "navigation");    // separator with newline
?> 

Is this the most efficient and/or correct way to translate the breadcrumbs? The text domain here set as "navigation" is where this translation lives. Without being set it defaults to the value "default".

Upvotes: 1

Views: 1451

Answers (2)

Nono HERON
Nono HERON

Reputation: 114

To fix the translation of the breadcrumb, I did this : Cut/paste all the translator configuration into a new file : config/autoload/translator.global.php I use only one translation file for the entire application and not one by module... but anyway, if you do have one translation file by module, just choose one for your navigation ;) I modify the base_dir option to the module/application/language :

<?php
return array(
    'translator' => array(
        'locale' => 'fr_FR',
        'translation_file_patterns' => array(
            array(
                'type'     => 'gettext',
                'base_dir' => __DIR__ . '/../../module/Application/language',
                'pattern'  => '%s.mo',
            ),
        ),
    ),
);

I also put my navigation config array into one autoload file : config/autoload/navigation.global.php => I add the new path in the poedit file used to translate my navigation (the only one for me) for automatic detection ;)

In this file, I instanciate a specific translator using the translator's factory method, and I just have to call the translate() method :) :

<?php
$config = require 'translator.global.php';
$options = (array)$config['translator'];
$translator = \Zend\I18n\Translator\Translator::factory($options);

return array(
    'navigation' => array(
        'default' => array(
            array(
                'label' => $translator->translate('Home'),
                'route' => 'home',
            ),
       [...]

And everything works great now (even breadcrumbs) :)

I had the same translation problem with my forms, and the first trick I found still work great without having to create a new translator for each forrm : the only trouble was poedit autodetection :

create an underscore function in public/index.php (not used by zend but regognized by poedit)

function _($str){
    return $str;
}

and then only have to declare form labels like that :

'label' => _('My Label'),

poedit detects it and translate labels without anything else than a little translation works in poedit for all theses new words ;)

EDIT : I had a bug in my beadcrumb partial phtml file : didn't call for the translate viewhelper... (just copied it from the zend tutorial and forgot it... stupid isn't it ? :D)

That fixed, the underscore function works as well for the form labels and for navigation... even breadcrumbs ! All my heavy stuff, creating a new translator to call it in the navigation config array is useless :)

I found back where I got this trick inbeetween : => How to translate form labels in Zend Framework 2? (in the last comment... that should be more voted than the actual 0 ;))

Upvotes: 1

Andreas Linden
Andreas Linden

Reputation: 12721

you cannot really do it like this, you're passing html to the translator. simply use these methods on the viewhelper:

$navHelper->setTranslator($yourTranslator);
$navHelper->setTranslatorTextDomain('de_DE');
$navHelper->setTranslatorEnabled(true); // (default)
$navHelper->setInjectTranslator(true); // to pass the translator down to menu/breadcrumbs etc. (default)

Upvotes: 1

Related Questions