0

I have one question, about ZF2 Translator, in the specific case in the costruction the link for change langauge dunamically when user click on flag or menu link.

In My Application/config/module.config.php i have this code:

   'service_manager' => array(
        'factories' => array(
            'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
            'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
        ),

    ),

and my route is:

return array(
    'router' => array(
        'routes' => array(

            'home' => array(
                'type' => 'Segment',
                'options' => array(
                    'route'    => '/[:lang[/:action]]',
                    'constraints' => array(
                        'lang'   => '[a-zA-Z]*',
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    ),
                    'defaults' => array(
                        'controller' => 'Application\Controller\Index',
                        'action'     => 'index',
                    ),
                ),
            ),

And in my Application/Module.php i set this code on bootstrap:

public function onBootstrap(MvcEvent $e)
    {
        $sm = $e->getApplication()->getServiceManager();

        $router = $sm->get('router');
        $request = $sm->get('request');
        $matchedRoute = $router->match($request);
        $params = $matchedRoute->getParams();

        if(isset($params['lang']) && $params['lang'] !== '') {
            $translator = $e->getApplication()->getServiceManager()->get('translator');
            //or
            //$translator = $e->getApplication()->getServiceManager()->get('MvcTranslator');

            if($params['lang'] == 'en') {
                $translator->setLocale('en_US');
            }
        }

        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);
    }

now my route http://www.xxxx.com/it/index or http://www.xxxx.com/it/company but i can't create a link in my view for change language in my application...

How do i proceed ?

Thanks

dastan
  • 45
  • 6
  • thanks for yours answer ... I resole it with this method: [How to pass variables to layout.phtml globally in ZF2][1] [1]: http://stackoverflow.com/questions/18439364/how-to-pass-variables-to-layout-phtml-globally-in-zf2/18549487#18549487 – dastan Nov 08 '14 at 17:57

2 Answers2

2

You can create a link to the same page you are in by changing only the lang parameter like the following:

$this->url(null, array('lang' => $anotherLang), array(), true)
Mohammad ZeinEddin
  • 1,608
  • 14
  • 17
  • Hi, thanks for your help and answer, but i can't set the action on my url. In my main layout I have two link for change itself page in english or italian, but when run script I get only www.xxxx.com/it while I would www.xxx.com/it/company and www.xxx.com/en/company. I have something in my controller ? – – dastan Oct 21 '14 at 12:30
1

Just do this in your Views :

<a href="<?= $this->url($this->route, array('lang' => 'en'));?>"> English </a>

This will give the same URL as the current one but in different language (here it's English).

blackbishop
  • 30,945
  • 11
  • 55
  • 76
  • Hi Thanks for your answer, I try this is solution but i can't intercepet the action and set in the link. I should set also controller ? – dastan Oct 21 '14 at 12:21
  • Hi, thanks for your help and answer, but i can't set the action on my url. In my main layout I have two link for change itself page in english or italian, but when run script I get only www.xxxx.com/it while I would www.xxx.com/it/company and www.xxx.com/en/company. I have something in my controller ? – dastan Oct 21 '14 at 12:29
  • Just add `'action'=>'company'` in the url. – blackbishop Oct 21 '14 at 12:39