How to allow user to switch language in playframework 2

In my 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 2.x, but I got the impression that the functionality is no longer available. This is what I still have

  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()) ) } 
+6
source share
2 answers

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

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

You must have a conf / messages.fr file, so the application can reference it for a message. You can start with the messages.default file and post your own messages.

+2
source

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

This question has already been in the Google Play group.

+4
source

Source: https://habr.com/ru/post/915898/


All Articles