Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to let the user switch language in playframework 2

In my play 1.x controller I had this:

public static void language(final String language){
    Lang.change(language);
    Header referer = request.headers.get("referer");
    if(referer == null){
        index();
    }else{
        redirect(referer.value());
    }
}

I would like to do the same in play 2.x but I have the impression that functionality is not available any more. This is what I have so far

  def language(language:String) = Action { implicit request =>

    // TODO change language

    val referer = request.headers.get("referer")
    referer.map{ referer =>
      Redirect(referer, FOUND);
    }getOrElse(
      Ok(views.html.index())
    )
  }
like image 352
Somatik Avatar asked May 16 '12 10:05

Somatik


2 Answers

You can store the language in the user session. You can find an example here

This question has already been asked on the Play Google group

like image 106
kheraud Avatar answered Nov 09 '22 15:11

kheraud


According to the documentation, in Play 2.4, inside the controller you can do

ctx().changeLang(new Lang(Lang.forCode("fr")));

You need to have a file conf/messages.fr so the application can refer to it for the message. You can start from the messages.default file and put your own messages.

like image 29
Eva M Avatar answered Nov 09 '22 15:11

Eva M