3

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

Henrique Gonçalves
  • 1,572
  • 3
  • 16
  • 27

1 Answers1

3

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")))
      }
    }
Andrzej Jozwik
  • 14,331
  • 3
  • 59
  • 68
  • After some time I realized I was adding implicit messages in the wrong template. I've also changed my controller from Messages to messagesApi as you mentioned. I've accepted this answer. Thank you – Henrique Gonçalves Apr 06 '16 at 06:45