[2.0-java] use i18n/translated messages from within JavaScript (getting all messages, getting the current language)

1,214 views
Skip to first unread message

Joscha Feth

unread,
Apr 30, 2012, 8:45:33 PM4/30/12
to play-fr...@googlegroups.com
Hello,

I am planning to (re-)use my translations within the JavaScript part of my play application, therefore I somehow need to get the language bundle onto the client side.
My idea was to write the bundle for the current language into a JSON object and either directly embed it in the HTML markup (to save that request) or load it separately via XHR (in case language switching is needed).

 I have two question related to this:

  1. In Play 1.2.4 there was a possibility to get all messages from a specified language: http://www.playframework.org/documentation/api/1.2.4/play/i18n/Messages.html#all%28java.lang.String%29 - this functionality seems to be removed in play 2.0? Is there any other way I can easily achieve the same functionality?
  2. How do I get the current language used for Messages.get in play 2.0?

Regards,
Joscha

Julien Richard-Foy

unread,
May 1, 2012, 3:50:41 AM5/1/12
to play-fr...@googlegroups.com
1. I wrote a module doing that: https://github.com/julienrf/play-jsmessages
2. There is a `lang()` method defined in the Controller class:
http://www.playframework.org/documentation/api/2.0/java/play/mvc/Controller.html#lang()

Ben McCann

unread,
May 1, 2012, 10:57:13 AM5/1/12
to play-fr...@googlegroups.com
You might look into using the Closure Compiler's support for i18n:

Joscha Feth

unread,
May 1, 2012, 1:58:55 PM5/1/12
to play-fr...@googlegroups.com
Hi Julien,

this is exactly what I was searching for, however I want to use it from a Java play application instead from a Scala one - I seem to have problems to properly call the plugin - I tried it like this:

    public static Result jsMessages() {
        return ok(play.libs.Json.toJson(JsMessages.apply("bla", ... , lang())));
    }

however at the postion of the ... an object of type play.api.Application is expected - do you have any idea on how to get this in a Java-based controller? I can see that the play.Application object wraps it and this is available within a the Global class extending GlobalSettings, but that does not seem to help.

Regards,
Joscha

Joscha Feth

unread,
May 1, 2012, 2:07:11 PM5/1/12
to play-fr...@googlegroups.com
Just read this:

The API available in the play.api package (such as play.api.mvc) is reserved for Scala developers. As a Java developer, look at play.mvc.

at https://github.com/playframework/Play20/wiki/JavaHome - so is it even possible to use a Scala extension from a Java project? Because this means I would need to somehow manage to create a play.api.Application object from my existing play.Application object...

Joscha Feth

unread,
May 1, 2012, 2:25:48 PM5/1/12
to play-fr...@googlegroups.com
Seems to work like this:

    public static Result jsMessages() {
        return ok(JsMessages.apply("Messages", play.api.Play.current() , lang())).as("application/javascript");
    }

I will add a note to the docs of the plugin and send you a pull request!

GereMora

unread,
Mar 24, 2013, 11:38:56 AM3/24/13
to play-fr...@googlegroups.com

You can take a look at this small example I did. Internalization in Scala templates and JS files.

https://github.com/geremora/play2-i18n-sample

Gere.
Reply all
Reply to author
Forward
0 new messages