Chaining language with default route

      18 Comments on Chaining language with default route

There are several ways how to include language id in default route of Zend Framework. However, generally you end up with the solution not quite elegant and likely not totally trouble-free. I have seen people overwriting the default route by new one which mimics module route with additional language id. There is no need to throw the default module route away to do this. To get it right chain the plain language route with default route.

I think that the code above has enough comments to clarify it, so lets forward to the controller plugin. If you are not familiar with chaining routes, refer to reference guide Zend_Controller_Router_Route_Chain.

In Language controller plugin we have to take care of actions, which have to be done if language has been changed. Setting the language id as a global router parameter is the most important. The another common task is to check, if there is available translation for the selected language.

In our Language plugin, we check if user selected language is available. In spite of its availability, we have valid language id in $lang, which is set as a global param. To achieve a better user experience you may want to store the user selected language in to session to be retrieved on later visits.

Update (2011-03-08): Updated the plugin to change the method “routeStartup” to “routeShutdown”, as the intention is to update the route with the default language when unmatched.