2

I want the user of the application can change the language in my play2 (play 2.1.1, scala 2.10.1) web application. I use @Messages.get(...) in my templates for i18n.

I have

application.langs="en,ru"

in application.conf. I pass "en" or "ru" to that method:

def index = Action {
   Ok(views.html.index())
}

def changeLanguage(lang:String) = Action {
  implicit request =>
    Logger.logger.debug("Change user lang to : " + lang)
    val referrer = request.headers.get(REFERER).getOrElse(HOME_URL)
    Redirect(referrer).withLang(Lang(lang))
}

routes:

GET     /                           controllers.Application.index
GET     /index                      controllers.Application.changeLanguage(lang ?= "ru")

the template bunch (views.html.index):

@()(implicit l: Lang)

@import play.i18n.Messages

...

<a href="/about">@Messages.get("about")</li>

...

<a href="index?lang=ru" id="ru"></a>
<a href="index?lang=en" id="en"></a>
...

After redirecting the page, I see it on the same language. :(

I was read many old answers: implicit language parameter in my template does not work, redirect or action with withLang(...) method call too. Did not have a good solution so long time?

2
  • It should work with the .withLang(...) method. Could you try and change (implicit l: Lang) to (implicit l:play.api.i18n.Lang) and @import play.i18n.Messages to @import play.api.i18n.Messages?
    – EECOLOR
    Apr 9, 2013 at 20:02
  • @EECOLOR, it did not work
    – Tolsi
    Apr 10, 2013 at 7:10

2 Answers 2

5

I made it work, so there are my changes. In app code (without an request instance play does not know where to get the cookie with the language?):

def index = Action {
 implicit request=>
   Ok(views.html.index())
}

And in the template (play.api.i18n imports automatically):

@()(implicit l: Lang)

...

<a href="/about">@Messages("about")</li>

...

<a href="index?lang=ru" id="ru"></a>
<a href="index?lang=en" id="en"></a>
...
2
  • Nice job! Then, here is a more generic solution: you can define you implicit request on every actions with less code. Apr 10, 2013 at 13:34
  • Is it possible to avoid to write the implicit l: Lang part in each template and only define it in the main maybe? Jul 29, 2013 at 20:57
1

I had the same issue and added my own message-resolution class over the play.i18n one.

For message resolution, you can have a example here (in Java): https://github.com/adericbourg/proto-poll/blob/dev/app/util/user/message/Messages.java#L76

And my controller changeLang's method calls this: https://github.com/adericbourg/proto-poll/blob/dev/app/util/security/CurrentUser.java#L71

It do not believe it is a good solution (it requires more code and I'm a lazy guy) but it works. Hope this can help...

1
  • I found a simpler solution
    – Tolsi
    Apr 10, 2013 at 7:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.