Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

play framework 2.0 - internationalization - how to translate a message

First question: how can I retrieve the translation of a text in a controller?

Second question: how can I retrieve the translation of a text in a template?

The api says that there is a .get method that translates a message:

http://www.playframework.org/documentation/api/2.0/java/play/i18n/Messages.html

However my application does not recognize this method. Opening in eclipse the Message.class shows that there is an .apply method in it, written in Scala and Java!?

object Messages {

  /**
   * Translates a message.
   *
   * Uses `java.text.MessageFormat` internally to format the message.
   *
   * @param key the message key
   * @param args the message arguments
   * @return the formatted message or a default rendering if the key wasn’t defined
   */
  def apply(key: String, args: Any*)(implicit lang: Lang): String = {
    Play.maybeApplication.flatMap { app =>
      app.plugin[MessagesPlugin].map(_.api.translate(key, args)).getOrElse(throw new Exception("this plugin was not registered or disabled"))
    }.getOrElse(noMatch(key, args))
  }

Now eclipse tells me that I can invoke this method like this:

> String play.api.i18n.Messages.apply(String arg0, Seq<Object> arg1,
> Lang arg2)

But what should I enter as the "Seq" argument?

--The solution--

The problem was that I imported play.api.i18n.Messages instead of play.i18n.Messages ...

Having defined two message files (messages.de-DE and messages.en-UK) and using the following code everything works fine:

Controller:

    import play.i18n.Messages;
    import play.api.i18n.Lang;

    Lang en = new Lang("en","GB");
    play.i18n.Lang en_lang = new play.i18n.Lang(en);

    Lang de = new Lang("de", "DE");
    play.i18n.Lang de_lang = new play.i18n.Lang(de);

    Logger.info(Messages.get("home.title"));
    Logger.info(Messages.get(en_lang, "home.title"));
    Logger.info(Messages.get(de_lang, "home.title"));

application.conf

    application.langs="en-GB,de-DE"
like image 961
Thomas Kremmel Avatar asked May 03 '12 17:05

Thomas Kremmel


People also ask

What are the languages used by the Play framework?

Play Framework is an open-source web application framework which follows the model–view–controller (MVC) architectural pattern. It is written in Scala and usable from other programming languages that are compiled to JVM bytecode, e.g. Java.

What is the use of context in play?

Context uses a thread local to capture and access the current request, but it gives the impression that the current request can be accessed from any place, which is not currently true if you are using Actors or a custom thread pool. Regarding the API modeling, there are some duplicated concepts (like play.

Which property is used to specify supported language of the play application?

To ensure that languages are resolved correctly, specify the languages your app supports using the resConfigs property in the module-level build.


1 Answers

Getting the translation inside the controller:

// in messages file
msg.key=Hello Translation

// in you controller
Messages.get("msg.key");

You can even pass parameters:

// in messages file
msg.key=Hello {0}, here is your translation

//in controller
Messages.get("msg.key", User.firstName);

From the view you can use: Messages("msg.key")

You can even apply HTML formatting (only applicable for views of course):

// in messages file
msg.key=Hello <strong>{0}</strong>, here is your translation

// in controller
Messages.get("msg.key", User.firstName);

//in view
@Html(objectInView)

Please note the following: Currently it is not possible to define the language explicitly, see bug report: https://play.lighthouseapp.com/projects/82401/tickets/174-20-i18n-add-ability-to-define-implicit-lang-for-java-api

Similar question was asked before: Access translated i18n messages from Scala templates (Play! Internationalization)

i18n error: controller and templates uses different implicit languages

like image 115
adis Avatar answered Sep 30 '22 19:09

adis