在play framework 2.1.1中更改模板中的文本语言

Tol*_*lsi 2 templates scala internationalization web playframework-2.1

我希望应用程序的用户可以更改我的play2(播放2.1.1,scala 2.10.1)Web应用程序中的语言.我在i18n的模板中使用了@ Messages.get(...).

我有

application.langs="en,ru"
Run Code Online (Sandbox Code Playgroud)

在application.conf中.我将"en"或"ru"传递给该方法:

def index = Action {
   Ok(views.html.index())
}

def changeLanguage(lang:String) = Action {
  implicit request =>
    Logger.logger.debug("Change user lang to : " + lang)
    val referrer = request.headers.get(REFERER).getOrElse(HOME_URL)
    Redirect(referrer).withLang(Lang(lang))
}
Run Code Online (Sandbox Code Playgroud)

路线:

GET     /                           controllers.Application.index
GET     /index                      controllers.Application.changeLanguage(lang ?= "ru")
Run Code Online (Sandbox Code Playgroud)

模板束(views.html.index):

@()(implicit l: Lang)

@import play.i18n.Messages

...

<a href="/about">@Messages.get("about")</li>

...

<a href="index?lang=ru" id="ru"></a>
<a href="index?lang=en" id="en"></a>
...
Run Code Online (Sandbox Code Playgroud)

重定向页面后,我用同一种语言看到它.:(

我读了许多旧答案:我的模板中的隐式语言参数不起作用,重定向或带有withLang(...)方法调用的操作也是如此.这么长时间没有好的解决方案?

Tol*_*lsi 5

我做到了,所以有我的改变.在应用程序代码中(没有请求实例播放不知道在哪里获取使用该语言的cookie?):

def index = Action {
 implicit request=>
   Ok(views.html.index())
}
Run Code Online (Sandbox Code Playgroud)

并在模板中(play.api.i18n自动导入):

@()(implicit l: Lang)

...

<a href="/about">@Messages("about")</li>

...

<a href="index?lang=ru" id="ru"></a>
<a href="index?lang=en" id="en"></a>
...
Run Code Online (Sandbox Code Playgroud)