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 { 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"