2
votes

I'd like to know how to translate an entire route in ZF2. I've already tried: Zend\Mvc\Router\Http\TranslatorAwareTreeRouteStack

It fixed partially my problem, I still can't translate an action or a controller and that's what I want to know.

That's what I have so far, I can translate "{new}", but not the ":action"

        'news' => array(
            'type' => 'Segment',
            'options' => array(
                'route' => '/[:locale/]{news}[/page/:page][/:action[/:id]]',
                'defaults' => array(
                    'controller' => 'Application\Controller\News',
                    'action' => 'news'
                ),
            ),
        ),

The traduction are in an array in a PHP file called "fr.php":

 return array(
    'news' => 'nouvelles',
    'details' => 'dd'
);

"details" are my action I want to translate.

My Module class:

namespace Application;

use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Zend\Config\Reader\Xml as Xml;

class Module
{
    public function onBootstrap(MvcEvent $e)
    {
    $eventManager        = $e->getApplication()->getEventManager();
    $moduleRouteListener = new ModuleRouteListener();
    $moduleRouteListener->attach($eventManager);

    // Réglage de la langue
    $routeCallback = function ($e) {
        $availableLanguages = array ('fr', 'en');
        $defaultLanguage = 'fr';

        $fromRoute = false;
        //see if language could be find in url
        if ($e->getRouteMatch()->getParam('locale')) {
            $language = $e->getRouteMatch()->getParam('locale');
            $fromRoute = true;

            //or use language from http accept
        } else {
            $headers = $e->getApplication()->getRequest()->getHeaders();
            if ($headers->has('Accept-Language')) {
                $headerLocale = $headers->get('Accept-Language')->getPrioritized();
                $language = substr($headerLocale[0]->getLanguage(), 0,2);
            }
        }
        if(!in_array($language, $availableLanguages) ) {
            $e->getResponse()->setStatusCode(404);
            return;
        }
        $e->getApplication()->getServiceManager()->get('translator')->setLocale($language);

    };

    $eventManager->attach(\Zend\Mvc\MvcEvent::EVENT_ROUTE, $routeCallback);

    // Traduction du routeur
    // Load translator
   $translator = $e->getApplication()->getServiceManager()->get('translator');
   // setup the translation file. you can use .mo files or whatever, check the translator api
   $language = (preg_match('/\/en\//', $_SERVER["REQUEST_URI"])) ?'en' :'fr';
   $translator->addTranslationFile('PhpArray', __DIR__.'/language/fr.php', 'default', $language); 
   $app = $e->getTarget();

   // Route translator
   $app->getEventManager()->attach('route', array($this, 'onPreRoute'), 100);

   // Application Configuration
    $serviceManager = $e->getApplication()->getServiceManager();
    $viewModel = $e->getApplication()->getMvcEvent()->getViewModel();
    $reader = new Xml();
    $appConfig   = $reader->fromFile('public/ApplicationConfiguration.xml');
    $viewModel->appConfig = $appConfig;

    $viewModel->translator = $e->getApplication()->getServiceManager()->get('translator');

}

public function onPreRoute($e){
    $app      = $e->getTarget();
    $serviceManager       = $app->getServiceManager();
    $serviceManager->get('router')->setTranslator($serviceManager->get('translator'));
}

public function getConfig()
{
    return include __DIR__ . '/config/module.config.php';
}

public function getAutoloaderConfig()
{
    return array(
        'Zend\Loader\StandardAutoloader' => array(
            'namespaces' => array(
                __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
            ),
        ),
    );
}

}

How can I translate a controller and an action?

2

2 Answers

0
votes

Just in case you don't find a proper solution, the way I do it is I define a route like

    'news_fr' => array(
        'type' => 'Segment',
        'options' => array(
            'route' => '/[:locale/]{news}[/page/:page][/:action[/:id]]',
            'defaults' => array(
                'controller' => 'Application\Controller\News',
                'action' => 'notice'
            ),
        ),
    ),


    'news_eng' => array(
        'type' => 'Segment',
        'options' => array(
            'route' => '/[:locale/]{news}[/page/:page][/:action[/:id]]',
            'defaults' => array(
                'controller' => 'Application\Controller\News',
                'action' => 'news'
            ),
        ),
    ),

and then on the view/layout I do

<?= $this->url('news_'.$currentLanguage, array('par1',...)); ?>
0
votes

Just simple, in routes config

return array(
    'router' => array(
        'router_class' => 'Zend\Mvc\Router\Http\TranslatorAwareTreeRouteStack',
        'routes' => array(...)
    ),
    ...
);

In function onBootstrap in Module.php

class Module
{
    public function onBootstrap(MvcEvent $e)
    {
        $eventManager = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $eventManager->attach('route', array($this, 'onPreRoute'), 100);
        $moduleRouteListener->attach($eventManager);
    }
    public function onPreRoute($e)
    {
        $app = $e->getTarget();
        $serviceManager = $app->getServiceManager();
        $serviceManager->get('router')->setTranslator($serviceManager->get('translator'));
    }
    ...
}