6

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"
Thomas Kremmel
  • 14,575
  • 26
  • 108
  • 177

1 Answers1

10

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

Community
  • 1
  • 1
adis
  • 5,901
  • 7
  • 51
  • 71
  • My play framework does not know the Messages.get("msg.key") method. It throws the error: cannot find symbol [symbol: method get(java.lang.String)] [location: class play.api.i18n.Messages] - Maybe I m using the wrong Play Source? Actually the console says "play! 2.0," . Maybe I selected while creating my app to "1 - Create a simple Scale application" instead of "2 - Create a simple Java application". Might this be a reason for this behaviour? – Thomas Kremmel May 04 '12 at 06:08
  • hmm... I would wonder that I have selected "1 - simple scala app", but I will give it a try later this day. And I dont think that this is the reason because all the template files created in my app are in .java ;-) – Thomas Kremmel May 04 '12 at 07:08
  • hmm.. I m importing "import play.api.i18n.Messages;". How does the framework decides whether it uses the Java or the Scala class? Is this a configuration somewhere? It obviously uses the Scala class. – Thomas Kremmel May 04 '12 at 08:06
  • Java and Scala are both JVM languages, (i did not used Scala) but I think they can share APIs.. The template file cannot be java, they are scala! Main app language can be set in the build.scala: mainLang = JAVA – adis May 04 '12 at 13:02
  • 2
    No simply in Play2 the exact way is `Messages("your key")` without the `get` method. That was Play1. – i.am.michiel Jul 07 '12 at 15:53
  • 1
    @i.am.michiel: even in the code? In the view you are right, but in the Java code I have to use: `Messages.get("key")` as stated in the docs (for java): https://github.com/playframework/Play20/wiki/JavaI18N – adis Jul 07 '12 at 20:50
  • The HTML formatting in your example has a security hole: the name won't be HTML escaped, so some hacker can name himself as ' – Tamas Jan 17 '13 at 10:08