简体   繁体   中英

Play framework 2.5 scala i18n

I've followed the documentation in https://www.playframework.com/documentation/2.5.x/ScalaI18N and translations inside controllers works fine. However, I need translations in Twirl templates. With implicit messages and/or using Messages("test.testing") I get the following error:

could not find implicit value for parameter messages: play.api.i18n.Messages

My controller:

class HomeController @Inject()(val messagesApi: MessagesApi) extends Controller with I18nSupport {

  def updateLocale(lang: String) = Action { implicit request =>
    printf(request.headers.get("Accept-Language").getOrElse("niente"))
    printf(Messages("test.testing"))
    Ok(views.html.index(Messages("test.testing")))
  }

This also makes me doubt how can I allow changing the language in the application...

What am I missing here?

Thanks

Replace Messages with injected messagesApi :

class HomeController @Inject()(val messagesApi: MessagesApi)
                   extends Controller with I18nSupport {

      def updateLocale(lang: String): EssentialAction = Action {
        implicit request =>
          printf(request.headers.get("Accept-Language").getOrElse("niente"))
          printf(messagesApi("test.testing"))
          Ok(views.html.index(messagesApi("test.testing")))
      }
    }

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM