简体   繁体   English

播放更改语言动态

[英]Play change Language dynamallicy

Fairly new to Play trying to change the language dynamically. 尝试动态更改语言的Play新手。

route 路线

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

Tried so far (but none of them work) 到目前为止已尝试过 (但没有一个起作用)

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

view 视图

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

Both not working.. 都不能用..

application.config application.config

在此处输入图片说明

messages 消息

在此处输入图片说明

Method with some logging 有日志记录的 方法

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"));
}

Result 结果

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

尝试这个:

ctx().changeLang(language);

You need to delete the application.langs="nl" from the configuration. 您需要从配置中删除application.langs="nl" It's deprecated and replaced by the play.i18n.langs . 它已弃用,并由play.i18n.langs取代。

You must leave only play.i18n.langs=["en","nl"] 您只能play.i18n.langs=["en","nl"]

You code does not work because Play reads the application.langs="nl" and ignore play.i18n.langs=["en","nl"] (because langs already read from the application.langs ), so it suggest your application use only "nl" language and, of course could not set it to "en", so ctx().changeLang(language) method return false 您的代码无效,因为Play读取了application.langs="nl"并忽略了play.i18n.langs=["en","nl"] (因为langs已经从application.langs读取了),因此它建议您使用应用程序仅使用“ nl”语言,当然不能将其设置为“ en”,因此ctx().changeLang(language)方法返回false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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