Search code examples
zend-frameworkzend-translate

Zend Translate - Set Locale per User


I'm developing a multi-lingual site.

I've put a Zend_Translate object in the Zend_Registry and I use it to translate all static texts to the selected locale.

My question is how do I change the locale when the user chooses another language?

If I get the translate object from the registry, call setLocale , and put it again in the registry , will it hold only for that user or for the whole application?

Thanks.


Solution

  • You will need to use a plugin for that.

    In short: create your plugin and add the routeStartup() and routeShutdown() methods to it. In the former one, set the locale string in a request param and, if needed, also set the requesr URI to contain the locale string. Correct locale string detection is up to you - you can either use a cookie, URL parametre, browser's accepted language header or any combination of the three. In the latter one, create your Zend_Translate object based on the locale you have set earlier.

    Here is how it looks like in my CMS.

    Here is a tutorial regarding the use of the language parametre in the URL. You may also check how I do it in my CMS (see the _initRouter method).

    I hope this info is helpful.