Wednesday, March 2, 2016

Messages API. Unable to switch the language

Assume you are using java in the controller, and the default template engine.  It will work perfectly until you want to switch the language.

Let’s look at the guide of java internationalization JavaI18N
public Result index() {
  ctx().changeLang("fr");
  return ok(hellotemplate.render()); // "bonjour"
}
Well, it will only work in the case if you use java version of the messages API in the templates i.e.
@import play.i18n._
@Messages.get("hello")
If you prefer to use shorter scala version, like
@Messages("hello")
Then switched language will not be used in the current request. Scala Messages API will use the old one – default or taken from the request cookie.  I need to admit that ctx().changeLang(“fr”) will change the PLAY_LANG cookie, so in the next request (for example after refreshing the page)  scala will take this cookie and will show the “switched” language.

No comments:

Post a Comment