Zend Framework 2 – translating routes

Posted on

Zend Framework 2 – translating routes – Here in this article, we will share some of the most common and frequently asked about PHP problem in programming with detailed answers and code samples. There’s nothing quite so frustrating as being faced with PHP errors and being unable to figure out what is preventing your website from functioning as it should like php and zend-framework2 . If you have an existing PHP-based website or application that is experiencing performance issues, let’s get thinking about Zend Framework 2 – translating routes.

I am wondering if it is possible to use translational tools for routes/uris in zf2. I want for example the route en.domain.tld/article/show/1 to translate for example to de.domain.tld/artikel/anzeigen/1. I don’t think regex is the way to go here, because it could result in something like en.domain.tld/artikel/show/1. Also I want to avoid creating routes for every language, because it is going to get quite messy as the system scales.

Solution :

I was able to get it working!

First, add a 'router_class' => 'ZendMvcRouterHttpTranslatorAwareTreeRouteStack', your module.config.php like this:

return array (
    'router' => array (
        'router_class' => 'ZendMvcRouterHttpTranslatorAwareTreeRouteStack',
        'routes' => array(),
    )
);

Second, you must provide a translator (preferably in your module.php) as well as a translation file:

class Module
{
    public function onBootstrap(MvcEvent $e)
    {
        // Load translator
       $translator = $e->getApplication()->getServiceManager()->get('translator');
       $translator->setLocale('de_DE');        

           // setup the translation file. you can use .mo files or whatever, check the translator api
           $translator->addTranslationFile('PhpArray', __DIR__.'/language/routes/de_DE.php', 'default', 'de_DE');

       $app      = $e->getTarget();

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

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

now, you should be able to use translations in your route definitions like the following:

return array (
    'router' => array (
        'routes' => array(
            'login' => array (
            'type' => 'ZendMvcRouterHttpSegment',
            'may_terminate' => true,
            'options' => array (
                'route' => '/{login}',
                'defaults' => Array(
                    'controller' => '...',
                ) 
            ),
        ),
    )
);

create the translation (in this example a phpArray located in module/language/routes/de_DE.php):

<?php
return array(
    'login' => 'anmelden',
);

If I didn’t forget anything, you should be good to go. I got it working in my case, so if it doesn’t with the instructions above, don’t hesitate to comment and I’ll sort things out.

There is a implementation already which you will find starting ZF 2.2.0. As far as i can tell there is no Documentation for this feature, however when looking at the unit tests you should be able to give this a shot:

I’ll try to get a working example setup sometime today, but can’t make any promises – the test should get you started tho!

As an addition to the onPreRoute callback above:

You may need to add:

$serviceManager->get(‘router’)->setTranslatorTextDomain(‘TEXT_DOMAIN_HERE‘);

Leave a Reply

Your email address will not be published. Required fields are marked *