laminas / laminas-router

Flexible routing system for HTTP and console applications
https://docs.laminas.dev/laminas-router/
BSD 3-Clause "New" or "Revised" License
34 stars 15 forks source link

Multilingual site in ZF3 #2

Open weierophinney opened 4 years ago

weierophinney commented 4 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.


Originally posted by @willemserik at https://github.com/zendframework/zend-router/issues/55

weierophinney commented 4 years ago

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


Originally posted by @kokspflanze at https://github.com/zendframework/zend-router/issues/55#issuecomment-483429589