Question

In ZF2 I have two languages English and Chinese. Every route starts with language like:

  'about' => array(
      'type'    => 'Zend\Mvc\Router\Http\Segment',
      'options' => array(
           'route'       => '/[:lang]/about',
           'constraints' => array(
                'lang' => '(en|zh)?',
           ),
           'defaults' => array(
               'controller' => 'Application\Controller\About',
               'action'     => 'index',
               'lang'       => 'en',
           ),
       ),
   ),

I have already translated label with setTranslator, but I do not know how I can add language parameter to the route. It seems to me like this problem Translation of URI segments in ZF2

'navigation' => array(
        'default' => array(
            array(
                'label' => 'About',
                'route' => 'about',
                'class' => 'top-level',
            ),
         ),

Or is there better way how to handle this for example prepend language to every url like this How to prepend language to every url in Zend Framework

Was it helpful?

Solution

In general, there is a params key in the navigation to supply route parameters. It must contain an array of all values used in the route.

An example, for the route foo[/:bar] you can have this navigation configuration:

array(
    'label'  => 'Foo',
    'route'  => 'foo-bar',
    'params' => array('bar => 'baz'),
),

In your translation case, provide a params key called lang and than one will be used.

However you probably want to use the route match parameter for the language. If you are on the page from language zh then the parameter is automatically zh. Then you can use the use_route_match.

array(
    'label' => 'About',
    'route' => 'about',
    'use_route_match' => true,
),
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top