Play framework 2.0 - internationalization - how to translate a message

First question: how can I get text translation in the controller?

Second question: how can I get the translation of text into a template?

Api says there is a .get method that converts the 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 Message.class shows that it has a .apply method 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 call this method as follows:

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

But what should I introduce as an argument to "Seq"?

- Decision -

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" 
+6
source share
1 answer

Receiving a transfer 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 point of view you can use: Messages("msg.key")

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

 // 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 explicitly determine the language, see Error Report: https://play.lighthouseapp.com/projects/82401/tickets/174-20-i18n-add-ability-to-define-implicit -lang-for-java-api

A similar question was asked before: Access to i18n transmitted messages from Scala templates (Play! Internationalization)

Error i18n: controller and templates use different implicit languages

+10
source

Source: https://habr.com/ru/post/914829/


All Articles