在Play框架2.1.1中更改模板中文本的语言 [英] Change language of text in template in play framework 2.1.1
问题描述
我希望应用程序的用户可以更改我的play2(播放2.1.1,scala 2.10.1)Web应用程序中的语言.我在i18n的模板中使用@ Messages.get(...).
I want the user of the application can change the language in my play2 (play 2.1.1, scala 2.10.1) web application. I use @Messages.get(...) in my templates for i18n.
我有
application.langs="en,ru"
在application.conf中.我将"en"或"ru"传递给该方法:
in application.conf. I pass "en" or "ru" to that method:
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))
}
路线:
GET / controllers.Application.index
GET /index controllers.Application.changeLanguage(lang ?= "ru")
模板串(views.html.index):
the template bunch (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>
...
重定向页面后,我用相同的语言看到它. :(
After redirecting the page, I see it on the same language. :(
有很多老答案:我的模板中的隐式语言参数不起作用,也无法通过withLang(...)方法调用进行重定向或操作.这么长时间没有一个好的解决方案?
I was read many old answers: implicit language parameter in my template does not work, redirect or action with withLang(...) method call too. Did not have a good solution so long time?
推荐答案
我成功了,所以做了一些更改.在应用程序代码中(没有请求实例,播放不知道从何处获取该语言的Cookie?):
I made it work, so there are my changes. In app code (without an request instance play does not know where to get the cookie with the language?):
def index = Action {
implicit request=>
Ok(views.html.index())
}
并在模板中(play.api.i18n自动导入):
And in the template (play.api.i18n imports automatically):
@()(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>
...
这篇关于在Play框架2.1.1中更改模板中文本的语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!