简体   繁体   中英

Change language with button on play framework

First sorry for my english...

I was searching for all internet but I can't find the answer of my question. I tried everything, looked in documentation, tutorials, videos, etc...

I put two buttons in the top of my page for the user can change the language, but I can't catch the value on my controller, I did everything but never can handle. I'm new on play :( please help...!!

I have this on my view:

<`form method="GET" action="@{Application.language("value")}"/`>
    <`input> name="language" type="submit" value="en" title="@Messages("button.en")" </`>
    <`input> name="language" type="submit" value="es" title="@Messages("button.es")" </`>
<`/form`>

And this on my controller:

public static void language(String idiom) {
    String key = null;
    key = idiom;

    if(key.isEmpty()){

    } else {
        Lang.apply(idiom);
    }
}

But when I try to catch the value on my controller always I received this message:

[RuntimeException: Unrecognized language: value]

Your HTML looks a little suspect, can you clean it up and repost along with your controller and route?

In the meanwhile, this is roughly what I'd expect to see to make sure your parameters get passed in properly:

Routes:

GET     /language  @controllers.LanguageController.index(language: String)

Controller:

LanguageController {
...
  public Result index(String language) {
        if(language != null && !language.isEmpty()){
            Lang.apply(idiom);
        }   
        ... return
    }
 }

To make the setting stick in Play 2, see this post playframework 2.2 java: how to set language (i18n) from subdomain

I did it a little modification with your comment below and this is how I have now.

Route:

POST    /   @controllers.LanguageController.changeLanguage(language: String)

View:

<form method="POST" action="changeLanguage("value")"/>
    <input name="language" type="submit" value="en" title="English" </>
    <input name="language" type="submit" value="es" title="Spanish" </>
</form>

Controller:

public class LanguageController extends Controller{

public Result changeLanguage(String language)
{
    if(language != null && !language.isEmpty())
    {
        Lang.apply("en");
    } 
    else
    {
        String idiom = language;
        Lang.apply(idiom);
    }
    return ok(index.render(""));
}

Now I have this message error:

For request 'POST /changeLanguage(value)'

And the page error shows the route of the LanguageController this:

POST/@controllers.LanguageController@.changeLanguage(language:String)

You have messages.{lang} ( like messages.es or messages.en) in conf folder right?

And in application.conf valid langs should exist like;

application.langs="en,es"

If you have these so in any class which extends Controller you can run this method;

changeLang("es");

But in your case it seems that value of the idiom in your function is "value" So if it's fine for you just replace the form header as;

< form method="GET" action="/language"/ >

(assuming that /language will route to your method)

and replace the names of the html inputs as "idiom" so you will be passing the right value of the input.

In play 2.8, Http.Context was deprecated which lead to changes in the interaction with the response object.

To change the language you need to do the following:

  • Inject an instance of MessagesApi into your controller
  • Create an instance of Lang object with the language you intend to use
  • Use method .withLang(Locale locale, MessagesApi messagesApi) on your result object

Code to illustrate:

    private final MessagesApi messagesApi;

    @Inject
    public LoginController(MessagesApi messagesApi) {
        this.messagesApi = messagesApi;
    }

    // ... now in the method invoked by the router

    Lang lang = Lang.apply("en"); //english language
    return ok().withLang(lang.toLocale(), messagesApi);

This changes the language throughout the rest of the session, since play will store the language in the cookie PLAY_SESSION. If you want to change only for one particular request, you must change the request object instead of the result object.

Reference here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM