这是我的<p:add-attribute match="/c:request" attribute-name="href">
<p:with-option name="attribute-value" select="concat('http://localhost:8000/myRESTendpoint?uri=/mydb/', /*/@name, /*/@number,'.xml')"/>
</p:add-attribute>
文件:
routes
请注意,所有这些都以GET /:lang controller.Application.index(lang: String)
GET /:lang/news controller.Application.news(lang: String)
开头。
目前,我将/:lang
写为
Application.scala
通过这种方式,我必须尽可能多地编写def index(lang: String) = Action {
implicit val messages: Messages = play.api.i18n.Messages.Implicits.applicationMessages(
Lang(lang), play.api.Play.current)
Ok(views.html.index("title"))
}
作为implicit Messages
。有没有更好的解决方案呢?
答案 0 :(得分:1)
仅传递Lang
是一个更简单的选项:
def lang(lang: String) = Action {
Ok(views.html.index("play")(Lang(lang)))
}
//template
@(text: String)(implicit lang: play.api.i18n.Lang)
@Messages("hello")
您可以使用操作组合重用某些代码,定义包装的请求和操作:
case class LocalizedRequest(val lang: Lang, request: Request[AnyContent]) extends WrappedRequest(request)
def LocalizedAction(lang: String)(f: LocalizedRequest => Result) = {
Action{ request =>
f(LocalizedRequest(Lang(lang), request))
}
}
现在您可以像这样重用LocalizedAction
:
//template
@(text: String)(implicit request: controllers.LocalizedRequest)
@Messages("hello")
//controller
def lang(lang: String) = LocalizedAction(lang){implicit request =>
Ok(views.html.index("play"))
}
答案 1 :(得分:0)
最后,我通过以下方式解决了这个问题。
正如@Infinity建议的那样,我将包装的请求和操作定义为:
case class LocalizedRequest(messages: Messages,
request: Request[AnyContent])
extends WrappedRequest(request)
object Actions {
def LocalizedAction(lang: String)(f: LocalizedRequest => Result) = {
Action { request =>
f(LocalizedRequest(applicationMessages(Lang(lang), current), request))
}
}
object Implicits {
implicit def localizedRequest2Messages(implicit request: LocalizedRequest): Messages = request.messages
}
}
现在我可以像这样使用LocalizedAction
:
def lang(lang: String) = LocalizedAction(lang) { implicit request =>
Ok(views.html.index("play"))
}
但是,为了省略implicit
的{{1}}参数,Messages
应该是play.api.i18n.Messages
,我在模板中添加了一行:
@import controllers.Actions.Implicits._