2016-09-25 43 views
1

嘗試動態更改語言的播放相當新穎。播放更改語言dynamallicy

路線

GET  /language/:lang    controllers.Index.setLanguage(lang: String) 

試過到目前爲止(但他們沒有工作)

Lang.apply(language); 
Lang.change(language); // <-- doesn't even compile 
Lang.apply(language); 
ctx().changeLang(language); 

視圖

@import play.i18n.Messages 
... 
@Messages.get("message") 
@messages.at("message") 
... 

兩個不工作..

application.config

enter image description here

消息

enter image description here

方法一些記錄

public Result setLanguage(String language) { 
    Http.Context context = Http.Context.current(); 
    String langFromHttpContext = context.lang().language(); 
    String langFromCtx = ctx().lang().language(); 
    String playLangCookieVal = request().cookies().get("PLAY_LANG").value(); 
    boolean changed = ctx().changeLang(language); 

    Logger.info("Request param: " + language); 
    Logger.info("Http context language: " + langFromHttpContext); 
    Logger.info("ctx language: " + langFromHttpContext); 
    Logger.info("PLAY_LANG cookie value: " + langFromCtx); 
    Logger.info("Changed: " + changed); 
    return ok(index.render("Index")); 
} 

結果

application - Request param: en 
application - Http context language: nl 
application - ctx language: nl 
application - PLAY_LANG cookie value: nl 
application - Changed: false 

回答

1

您需要從配置中刪除application.langs="nl"。它已棄用,由play.i18n.langs替代。

你必須只留下play.i18n.langs=["en","nl"]

您的代碼不起作用,因爲播放讀取application.langs="nl"而忽略play.i18n.langs=["en","nl"](因爲從application.langs已經閱讀LANGS),所以建議你的應用程序只使用「NL」語言中,當然不能將它設置爲「en」,所以ctx().changeLang(language)方法返回false

+0

哇,這沒有工作。非常感謝你 –

1

試試這個:

ctx().changeLang(language); 
+0

這是行不通的。我不明白。似乎沒有什麼工作:') –

+0

你玩哪個版本? –

+0

目前在2.5.8 –