понедельник, 23 апреля 2012 г.

Locale in Grails. How to use it with cookies.

If you are developing multilingual project, you are definately know about i18n plugin in grails. It provides an interface to spring-i18n.
In the project root you can see grails-app/i18n folder and there are "messages.properties" files. What are they for? Every file is a standard Java Property file with message code as a key and localized message as a value. Grails picks messages accroding to locale. For example, if current locale are set to "pl", grails will use text from messages_pl.properties. By default grails analyze Accept-Language HTTP header and render response according it. But what should you do if you want to store the locale in cookies? In the Internationalization chapter in grails docs you can find these words:
You can provide users the capability to switch locales by simply passing a parameter called lang to Grails as a request parameter:/book/list?lang=esGrails will automatically switch the user's locale and store it in a cookie so subsequent requests will have the new header.
Yeah, that's great but when you try to do this, you will realize that there grails don't store locale in cookie. There is an open issue 8655 with this problem. On grails 2.0.3 this issue isn't solved. And now I will show you how to resolve this issue using custom Spring bean.

Постоянные читатели