我有一个简单的项目与Play Framework 2.4.6,其国际化配置为Play describe上的文档。我的文件是: 1)控制器:
package controllers
import play.api._
import play.api.mvc._
import play.api.i18n.{I18nSupport,MessagesApi,Messages,Lang}
import play.api.i18n.Messages.Implicits._
import play.api.Play.current
import javax.inject.Inject
class Application @Inject()(val messagesApi: MessagesApi) extends Controller with I18nSupport {
def index = Action { implicit request =>
Ok(views.html.index("Your new application is ready."))
}
}
2)消息资源文件:
application.name = Orthoclinic
welcome = Welcome to play!
3)主模板:
@(title: String)(content: Html)(implicit messages: Messages)
<!DOCTYPE html>
<html lang="en">
<head>
<title>@title</title>
<link rel="stylesheet" media="screen" href="@routes.Assets.versioned("/assets/stylesheets/main.css")">
<link rel="shortcut icon" type="image/png" href="@routes.Assets.versioned("/assets/images/favicon.png")">
<script src="@routes.Assets.versioned("/assets/javascripts/hello.js")" type="text/javascript"></script>
</head>
<body>
@Messages("application.name")
@content
</body>
</html>
4)索引模板:
@(message: String)
@main("Welcome to Play") {
@play20.welcome(message)
}
5)路由文件:
# Home page
GET / controllers.Application.index
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)
6)应用程序配置文件:
play.crypto.secret = "k1mIneaPlay_pRoJ"
play.i18n.langs = [ "en", "en-us", "pt-br", "es" ]
Calling ./activator reload compile run my result is that:
[error] /home/josenildo/scala-ide/workspace/orthoclinic-app/app/views/main.scala.html:13: could not find implicit value for para
meter messages: play.api.i18n.Messages
[error] @Messages("application.name")
我已经在Play documentation here上阅读了i18n最新版本的文档。在这个实现上有什么问题?
发布于 2016-02-09 13:41:30
您只需将隐式messages
参数添加到index.scala.html
模板中:
@(message: String)(implicit messages: Messages)
@main("Welcome to Play") {
@play20.welcome(message)
}
每当通过@Messages("my.key")
使用i18n时,隐式Messages
实例都需要在作用域内,它有一个对应的隐式messages
参数,该参数将由编译器提供(请参阅签名here。
也是,你可能想要去掉import play.api.i18n.Messages.Implicits._
,因为如果你的控制器扩展了I18NSupport
,就不应该需要它,而且确实可能会导致关于不明确隐含值的错误。
https://stackoverflow.com/questions/35291988
复制相似问题