|
Hi,
when I use the dateFormat() view helper it expects to get a locale passed as fourth parameter. Otherwise the default from the Locale class is used. Since I don't want to pass the locale each time I use dateFormat(), I want to set it somewhere. In the module.config.php of the SkeletonApplication it is easy to change the locale for the translator. But I did not finde anything for setting the locale generally. In the end I added this to module/Application/Module::onBootstrap() Locale::setDefault('de_DE'); This helps but seems a bit ugly. As well when I want to change the locale depending on the routing this doesn't seem the right way. So, what is the best way to set the locale in general and after routing? Thanks and best regards, Ralf -- Ralf Eggert Geschäftsführer Travello GmbH An der Eiche 15 25421 Pinneberg Geschäftsführer: Ralf Eggert Amtsgericht Pinneberg HRB 6112 PI Tel: 04101/8529401 Fax: 04101/8529402 E-Mail: [hidden email] Web: http://www.travello.de |
|
2012/8/17 Ralf Eggert <[hidden email]> -- In the module.config.php of the SkeletonApplication it is easy to change I did some testing to set the route based on the HTTP Accept-Language header. It's a small snippet you can put in your onBootstrap(), for example inside your application module.
It supports a set of known locale and a default locale. The Accept-Language header must contain a known locale to be set, else the default one is used. I think you can use something similar if you look at the request path and grab the first segment of the path. Then the logic to check it against a known list or else a default will be set, is the same.
Jurian Sluiman |
|
Hi Jurian,
> Read it here: > http://juriansluiman.nl/en/article/118/auto-detect-user-locale-with-zend-http-request-headers-and-ext-intl That fits perfectly and I will be able to amend it to my needs. Though I am also curious if this can somehow be separated and reuable class. Thanks and best regards, Ralf |
|
2012/8/18 Ralf Eggert <[hidden email]> Hi Jurian, These kind of questions pass by on IRC also more often. There is still a pet project for me to get started with a locale detection module, which would suit a variety of strategies:
A mixin of strategies would allow for example to say user profile > cookie > path > http header. Also a list of aliases ("nl" is "nl_NL") should be supported. Last, there can be a list of supported locales and a default locale when none is found.
But I think it will take a while to find time to get started... -- Jurian Sluiman |
|
Hi Jurian,
> These kind of questions pass by on IRC also more often. There is still a > pet project for me to get started with a locale detection module, which > would suit a variety of strategies: Such an module sounds interesting. But I wonder if module with such a limited scope don't blow up the module bootstrap process I dozens of them in my application. I would prefer to add this to my Application module which is already in use. I could add in to the onBootstrap() method. But I wonder if there is anything like the ressource plugins from ZF1 which can used in the ZF2 Module.php classes. Best regards, Ralf |
|
In reply to this post by Jurian Sluiman-3
Hi Jurian,
> Read it here: > http://juriansluiman.nl/en/article/118/auto-detect-user-locale-with-zend-http-request-headers-and-ext-intl Now I finally had some time to implement your solution, but I don't manage to amend it to use the params from the routing. In ZF1 I would do something like ->getRequest()->getParams() and there I would expect module, controller and action. But this obviously does not work in ZF2. So how can I access the params after the routing? Is the onBootstrap() method really the right place or is the routing processed after the bootstrapping? I found Robs article here, but still I don't now how to solve this problem: http://akrabat.com/zend-framework-2/a-list-of-zf2-events/ Thanks for your help. Best regards, Ralf |
|
In reply to this post by Ralf Eggert
2012/8/20 Ralf Eggert <[hidden email]> --
You have to ask Evan about the exact impact of a module, but modules in general are very lightweight. The cost of detecting a locale (or any other business logic you implement) is much higher than the cost of instantiating a module. The benefit of self-contained modules are for me more than the benefit of micro optimizations.
If later on you might think the amount of modules is a bottleneck for your application, you can always use the classes in the module and call those from your own existing module. So you do not turn on the module in your application.config.php, but you load the files and use them on your own.
Jurian Sluiman |
|
In reply to this post by Ralf Eggert
2012/8/20 Ralf Eggert <[hidden email]> --
The bootstrap event is an early event. Prior to the "run" state of the application, all the code is initialized during bootstrap. Bootstrap is something that always happen and is independent of routing or dispatching.
If you need to wait after routing took place, you can create a listener which listens late to the routing event. The route took place at normal priority, so if you attach a listener at low priority, the routing has been done. The result of routing is a (potential) route match object (NB. No route match object means a page-not-found error). Then you are able to look at the route match parameters and then detect your locale.
You can create the late route listener during bootstrap. So in your onBootstrap method: use Locale; use Zend\Mvc\MvcEvent; public function onBootstrap($event) { $app = $event->getApplication(); $em = $app->getEventManager(); $em->attach(MvcEvent::EVENT_ROUTE, function($e) { // Route has taken place now
$routeMatch = $e->getRouteMatch(); $locale = $routeMatch->getParam('locale', null); Locale::setDefault($locale); }, -100);
} Now if you have a route /:locale/something/else and you request en_US/something/else your locale will be set to en_US. Jurian Sluiman |
|
Hi Jurian,
thanks for your explanations. Makes sense to me now. I added the code and it worked as expected. But I was not satiesfied since I still have no "class" which does the job. Then I stumpled upon the EventManager::attachAggregate() and thought that that might be an even better solution. I ended up with my first listener class: ------------------------------------------------------------------------ namespace Application\Listener; use Zend\EventManager\EventManagerInterface; use Zend\EventManager\ListenerAggregateInterface; use Zend\EventManager\EventCollection; use Zend\Mvc\MvcEvent; use Locale; class RouteListener implements ListenerAggregateInterface { public function attach(EventManagerInterface $events) { $events->attach('route', array($this, 'language'), -100); } public function detach(EventManagerInterface $events) { $events->detach($this); } public function language($e) { $routeMatch = $e->getRouteMatch(); $locale = $routeMatch->getParam('lang', null); Locale::setDefault($locale); } } ------------------------------------------------------------------------ That works perfectly and with this listener class I got what I wanted. One question I still have: a ListenerAggregateInterface::detach() method is needed, but it does not seem to be called automatically. Could I just leave it empty? Best regards, Ralf |
|
Hello Ralph, 2012/8/20 Ralf Eggert <[hidden email]> -- One question I still have: a ListenerAggregateInterface::detach() method Typically to save the callback handler which is registered as listener. When you want to detach the aggregate, you loop through all listeners and detach them again. An example how this can be done is found here: https://github.com/juriansluiman/SlmLocale/blob/master/src/SlmLocale/Strategy/AbstractStrategy.php#L55-81
This directly let's me say you might check out SlmLocale as well: https://github.com/juriansluiman/SlmLocale. It's a small module (for now) which handles the various strategies I wrote down in some posts earlier. At this moment I only have the HTTP Accept-Language header as working prototype, the others still need to be implemented. The only issue now for route match based locales is the moment where I start looking for a locale. I prefer to do this as early as possible, so at bootstrap for now. All listed strategies can be implemented on bootstrap, only the route match strategy not, since it requires routing first. However, you can use a UriPath strategy as well, leaving your original routes intact (and locale independent).
Jurian Sluiman |
|
Hi Jurian,
thanks again. After I sent my message yesterday I looked into the ModuleRouteListener and found the same implementation for detach, which I use myself now. https://github.com/zendframework/zf2/blob/master/library/Zend/Mvc/ModuleRouteListener.php I also updated the example at the bottom of this page and sent a pull request for it, which is still waiting for acceptance. http://zf2.readthedocs.org/en/latest/modules/zend.event-manager.event-manager.html > This directly let's me say you might check out SlmLocale as well: > https://github.com/juriansluiman/SlmLocale. That looks really promising and I will definitely keep an eye on this. For my current project the locale detection is only needed for the route, so my RouteListener will do the job. But in future projects I definetely will need a more flexible solution like yours. Regards, Ralf |
| Powered by Nabble | Edit this page |
