Play framework 2.4 internationalization

293 views
Skip to first unread message

Dominik Sienkiewicz

unread,
Jun 15, 2015, 3:17:48 PM6/15/15
to play-fr...@googlegroups.com
I have problem with internationalization on play.

My code looks like this.
application.conf

# The application languages
# ~~~~~
play.i18n.langs = [ "en", "pl", "fr" ]

Application.scala

import play.api.mvc._

class Application extends Controller {

def index = Action { request =>
val s: String = "Languages: " + request.acceptLanguages.map(_.code).mkString(", ")
val striner: String = play.i18n.Messages.get("title") + ">>>" + s
Ok(views.html.index("[[" + striner + "]]"))
}

}

messages.en

test = en
title = title en
page.lang = en


test = "pl"
title = title pl
page.lang = pl

index.scala.html

@(message: String)

@main("Welcome to Play") {

@play.i18n.Messages.get("test")
<br />
@message

}

main.scala.lang

@(title: String)(content: Html)

<!DOCTYPE html>

<html lang="@play.i18n.Messages.get("page.lang")">
<head>
<title>@title</title>
<link rel="stylesheet" media="screen" href="@routes.Assets.versioned("stylesheets/main.css")">
<link rel="shortcut icon" type="image/png" href="@routes.Assets.versioned("images/favicon.png")">
<script src="@routes.Assets.versioned("javascripts/hello.js")" type="text/javascript"></script>
</head>
<body>
@content
</body>
</html>

And every time when i start this app on my laptop (http://localhost:9000) I have this

"pl" 
[[title pl>>>Languages: en, pl, en-US]]


He see polish even when I set english in chrome.
What am I doing wrong ?


Thanks for help

Christian Kaps

unread,
Jun 16, 2015, 2:21:07 AM6/16/15
to play-fr...@googlegroups.com
Hi,

If you have a RequestHeader in the implicit scope, it will use the preferred language extracted from the Accept-Language header and matching one of theMessagesApi supported languages. You should add a Messages implicit parameter to your template like this: @()(implicit messages: Messages).


Your controller should look like:
class Application @Inject() (val messagesApi: MessagesApi) extends Controller with I18nSupport {

 
def index = Action { implicit request =>
   
Ok(views.html.index())
 
}
}


Then in your template you can write:
@()(implicit messages: Messages)

@Messages("your.key")


Dominik Sienkiewicz

unread,
Jun 16, 2015, 3:06:47 AM6/16/15
to play-fr...@googlegroups.com
Hi

I do something like this:

Application.scala

package controllers

import com.google.inject.Inject
import play.api.i18n.{MessagesApi, I18nSupport}
import play.api.mvc._


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

  def index = Action { request =>
val s: String = "Languages: " + request.acceptLanguages.map(_.code).mkString(", ")
    val striner: String = messagesApi("title") + ">>>" + s

Ok(views.html.index("[[" + striner + "]]"))
}

}

index.scala.html

@(message: String)(implicit messages: Messages)


@main("Welcome to Play") {

    @Messages("test")
<br />
@message

}

main.scala.html

@(title: String)(content: Html)(implicit messages: Messages)

<!DOCTYPE html>

<html lang="@Messages("page.lang")">
    <head>
<title>@title</title>
<link rel="stylesheet" media="screen" href="@routes.Assets.versioned("stylesheets/main.css")">
<link rel="shortcut icon" type="image/png" href="@routes.Assets.versioned("images/favicon.png")">
<script src="@routes.Assets.versioned("javascripts/hello.js")" type="text/javascript"></script>
</head>
<body>
@content
</body>
</html>

But still I get this:

               "pl"
[[title pl>>>Languages: en-US, en]]          



--
Best Regards
Dominik Sienkiewicz
e-mail: dominik.s...@gmail.com

--
You received this message because you are subscribed to a topic in the Google Groups "play-framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/play-framework/tjNZehtyofI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to play-framewor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/8897b8f5-872a-41fe-b3c8-b11421b24fb3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Christian Kaps

unread,
Jun 16, 2015, 3:42:46 AM6/16/15
to play-fr...@googlegroups.com
Hi,

The I18nSupport trait has an implicit `request2Messages` method, which gives you the `Messages` (containing the `MessagesApi` and the `Lang`) based on your preferred lang. The `MessagesApi` you inject cannot have the preferred language from your browser because this is contained in the request.


There is also an example in the linked source.

Best regards,
Christian

Dominik Sienkiewicz

unread,
Jun 16, 2015, 6:41:39 AM6/16/15
to play-fr...@googlegroups.com
Do you have any example project for play 2.4 with internationalization based on browser (request) language?

--
Pozdrawiam
Dominik Sienkiewicz 
Wysłane z iPhone'a

Christian Kaps

unread,
Jun 16, 2015, 6:50:42 AM6/16/15
to play-fr...@googlegroups.com
Does it not work with my suggested changes?

Dominik Sienkiewicz

unread,
Jun 16, 2015, 7:04:28 AM6/16/15
to play-fr...@googlegroups.com
Could You show my how my controller and view should looks like?

--
Best regards 
Dominik Sienkiewicz 
Send from my iPhone'a

Christian Kaps

unread,
Jun 16, 2015, 7:15:24 AM6/16/15
to play-fr...@googlegroups.com
Please could you post the output from the println() statement.

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

 
def index = Action { implicit request =>

    println
(request2Messages.lang)
   
Ok
 
}
}

Dominik Sienkiewicz

unread,
Jun 16, 2015, 7:37:44 AM6/16/15
to play-fr...@googlegroups.com

Lang(en,)


--
Best Regards
Dominik Sienkiewicz
e-mail: dominik.s...@gmail.com

Christian Kaps

unread,
Jun 16, 2015, 7:50:21 AM6/16/15
to play-fr...@googlegroups.com
This was your expected lang, wasn't it? Then your controller and your templates must look like as suggested in: https://groups.google.com/d/msg/play-framework/tjNZehtyofI/tOkL76oujZoJ

Dominik Sienkiewicz

unread,
Jun 16, 2015, 8:05:47 AM6/16/15
to play-fr...@googlegroups.com
Great 

Now it works.
Many thanks Christian


--
Pozdrawiam
Dominik Sienkiewicz 
Wysłane z iPhone'a
Reply all
Reply to author
Forward
0 new messages