I have one simple project with Play Framework 2.4.6 that have internationalization configured as documentation on Play describe. My files are that: 1) Controller:
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) Message resource file:
application.name = Orthoclinic
welcome = Welcome to play!
3) Main template:
@(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) Index template:
@(message: String)
@main("Welcome to Play") {
@play20.welcome(message)
}
5) routes file:
# 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) Application conf file:
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")
I have followed the documentation for i18n last version on Play Documentation here . What's the problem on that implementation?
You just need to add the implicit messages
argument to your index.scala.html
template:
@(message: String)(implicit messages: Messages)
@main("Welcome to Play") {
@play20.welcome(message)
}
An implicit Messages
instance needs to be in-scope whenever you use i18n via @Messages("my.key")
, which has a corresponding implicit messages
argument that will be supplied by the compiler (see the signature here ).
Also , you may want to get rid of import play.api.i18n.Messages.Implicits._
, since it shouldn't be required if your controller extends I18NSupport
, and indeed may cause an error concerning ambiguous implicit values.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.