zendframework / zend-i18n

I18n component from Zend Framework
BSD 3-Clause "New" or "Revised" License
65 stars 49 forks source link

Localized router #67

Closed Itach1Uchixa closed 7 years ago

Itach1Uchixa commented 7 years ago

It is just a concept, but it would be great if such feature would be added https://gist.github.com/Itach1Uchixa/6ec75b8f2af47a0b63e3f52fa0285a24

weierophinney commented 7 years ago

It already exists in the zendframework/zend-mvc-i18n package, via the TranslatorAwareTreeRouteStack:

Itach1Uchixa commented 7 years ago

I knew existence of TranslatorAwareTreeRouteStack but my idea is to get locale from route, not a route translation. Please read and check solution first. I think that you @weierophinney just read the title and made your decision. Because I am offering: /ru-ru/hello/world not: /привет/мир or /privet/mir

TranslatorAwareTreeRouteStack and my solution could complement each other like: /ru-ru/privet/mir /en-us/hello/world

P.S: Sorry for wrong and unclear class naming

MatthiasKuehneEllerhold commented 7 years ago

You can do that with a root route that has the locale as variable in it. For example:

return [
    'router' => [
        'i18n' => [
            'type'    => Segment::class,
            'options' => [
                'route'    => '/[:locale/]',
                'constraints' => array(
                    // @see https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
                    'locale'  => '[a-z]{2}'
                ),
                'defaults' => [
                    'locale' => 'de',
                ],
                'may_terminate' => false,
            ],
            'child_routes' => [
                'home' => [
                    'type'    => Segment::class,
                    'options' => [
                        'route'    => '',
                        'defaults' => [
                            'controller' => IndexController::class,
                            'action'     => 'index',
                        ],
                    ],
                ],
            ],
        ],
    ],
];

Although your solution is more formulated and clear.

Itach1Uchixa commented 7 years ago

@MatthiasKuehneEllerhold In my old projects I did that, but problem is that the root route negatively affects modular architecture. For example, I can't just download and plug my modules that don't support i18n. In my case you will have to just configure your Application to support it and of course add translation files wherever you want and it will be enough. My solution will add it even for modules that doesn't support i18n. The main idea is to separate modules from that kind of attachment. Please try it yourself to understand what I mean.

Itach1Uchixa commented 7 years ago

@weierophinney please reconsider. It's a very useful solution! I understand that it needs refactoring I will refactor and optimize it.

MatthiasKuehneEllerhold commented 7 years ago

If the zend-team doesnt want to include it into zend-i18n you could publish it as an extra module. We've gone the way I described above but I admit your solution sounds better for us too.

weierophinney commented 7 years ago

I have a few comments:

I look forward to evaluating the pull request with this feature on the zend-mvc-i18n repository. :smile: