Joined December 2013
·

nono1971

France
·
·

@aimfeld: Thanks for your answers :)

Infact, I do translate for multiple language, but not with this code ;)
=> this was my configuration for the default language of my application :) ( but you're almost right :I didn't implement it yet when I posted last night ;) )

but I did inbeetween :)
=> I only implemented multiple languages for registred users.

so I integrated in my ACL :
As I check the user level, I also get his favorite language (set on registration)
in my ACL : module/MyAclLib/module.php :

public function onBootstrap(\Zend\EventManager\EventInterface $e) 
{
        $application = $e->getApplication();
        $em = $application->getEventManager();
        $em->attach('route', array($this, 'onRoute'), -100);
}

public function onRoute(\Zend\EventManager\EventInterface $e) 
{
    $application = $e->getApplication();
    $sm = $application->getServiceManager();
    $auth = $sm->get('Zend\Authentication\AuthenticationService');
    if ($auth->hasIdentity()) {
            $usr = $auth->getIdentity();
            $translator= $sm->get('translator')->setLocale($usr->language_file);
    // and the rest of the acl... but that is not the point
    }
}

of course, it is not enough : the patch for navigation breadcrumbs still translate in french (default language of my app).
to fix it, I make the same chek in config/autoload/navigation.global.php like this :

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

$auth = new \Zend\Authentication\AuthenticationService();
if ($auth->hasIdentity()){
    $user = $auth->getIdentity();
    $translator->setLocale($user->language_file);
}

evenything works perfectly for me like this now :)

Hi !
When I tried your method, I had issue because no translator was specified... poedit regognized it... navigation was translated... but not beadcrumbs... (don't ask me why).

But I found here a great part of the solution !
Instead of instanciating an empty translator, I called the factory() method wich returns a fully operationnal translator :) the only problem was to inject options

=> Before that, I write the translator's options in a separate autoload file (only one language folder for my entire application) :
config/autoload/translator.global.php

and now it works :

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

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

notice that I also put all the navigation in config/autoload/navigation.global.php file

Achievements
1 Karma
0 Total ProTip Views