Play Framework 2.4.0 with I18n: two languages in the same page

207 Views Asked by At

How can I use Messages("home.title") with IT and EN in the same page? Before the dependency injection I did:

Messages("home.title")(Lang("IT")) 
Messages("home.title")(Lang("EN"))

but now it doesn't work.

1

There are 1 best solutions below

0
On

If your controller looks like this

class MyController extends Controller { def doSomething = Action {...} }

and not like this

class MyController (val messagesApi: MessagesApi) extends Controller with I18nSupport { def doSomething = Action {...} }

then you may use the following approach inside of your template:

@import play.i18n._
<html>
    ...
    <h1>@Messages.get(Lang.forCode("IT"), "home.title")</h1>
    ...
    <h1>@Messages.get(Lang.forCode("EN"), "home.title")</h1>
    ...
</html>