zendframework / zend-router

Standalone routing implementation for HTTP and console requests
BSD 3-Clause "New" or "Revised" License
32 stars 20 forks source link

Multilingual site in ZF3 #55

Open willemserik opened 5 years ago

willemserik commented 5 years ago

When i set the locale parameter to another value than the default value that has been defined in module.config.php, the routing does not take this into account and uses the default value of locale instead.

Code to reproduce the issue

route definition of module.config.php:

'router' => [
        'router_class' => TranslatorAwareTreeRouteStack::class,
        'routes' => [
            'home' => [
                'type' => Segment::class,
                'options' => [
                    'route'    => '/',
                    'constraints' => [
                        'amp' => 'amp'
                    ],
                    'defaults' => [
                        'locale' => 'nl',
                        'controller' => WebsiteController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
            'website' => [
                'type' => Segment::class,
                'options' => [
                    'route'    => '/:locale/{home}[/:amp]',
                    'constraints' => [
                        'amp' => 'amp'
                    ],
                    'defaults' => [
                        'locale' => 'nl',
                        'controller' => WebsiteController::class,
                        'action'     => 'index',
                    ],
                ],
            ],

            'faq' => [
                'type' => Segment::class,
                'options' => [
                    'route'    => '/:locale/{faq}[/:amp]',
                    'constraints' => [
                        'amp' => 'amp'
                    ],
                    'defaults' => [
                        'locale' => 'nl',
                        'controller' => WebsiteController::class,
                        'action'     => 'faq',
                    ],
                ],
            ],

layout in which you can choose another language: website.phml I have hard coded the value to 'en':

<li>
<a href="<?= $this->url('home', ['locale' => 'en']); ?>"<?= ($this->plugin('translate')->getTranslator()->getLocale() == 'en') ? ' class="active"' : null; ?>>
<img src="<?= $this->basepath($language['data'][11]); ?>" alt="<?= $language['data'][3]; ?>" width="54" height="37" />
</a>
</li>

Expected results

I should expect that ZF knows that locale should be set to 'en' without having to send the 'locale' parameter with every link you create on the website. When I go to the faq, i would expect that te site goes to http://mydomain.com/**en**/faq

Actual results

When I switch to en, the routing uses the default value 'nl' instead So when I go to the faq now, the site goes to http://mydomain.com/**nl**/faq , when I have switched the language to en.

kokspflanze commented 5 years ago

@willemserik check https://github.com/basz/SlmLocale develop, maybe this help you

weierophinney commented 4 years ago

This repository has been closed and moved to laminas/laminas-router; a new issue has been opened at https://github.com/laminas/laminas-router/issues/2.