Play Framework 2.0.x JavaScript Internationalization

Play framework provides a very neat way of producing internationalized sites using the Messages plugin, but, it only supports native (Scala/Java) code or HTML templates, this means that JavaScript code can't be made i18n compliant.

Since this was getting on my nerves I started looking into alternatives for JavaScript i18n support. My first try was with i18next but the JSON file format used wasn't something that fit with us since we already have all our i18n text in standard Play form: in conf/messages.xx

Luckily, the jquery-i18n-properties jQuery plugin uses standard Java properties file format and that's what we're going to use here.

So, first of all, download the plugin and add it to your HTML template (right below jquery) and use the following code:



With this the plugin get's loaded and an alert is loaded to test everything works ok. Next, create a controller like:



Basically what we're doing here is providing a path to the plugin and using the standard Play language implementation, load the messages file and return it as plain text.

4 comentarios:

  1. I didn't know the jquery-i18n-properties plugin, it's really useful!

    There is just a problem for locating the "messages" file when the application is started from a packaged version (when using play "dist" or "stage" commands). The messages file can not be found

    If it can help someone else, to resolve this problem I just replaced the line 10 by the following code :

    InputStream in = JavaScriptController.class.getResourceAsStream("/messages." + l.code())

    ResponderEliminar
  2. I'm also having a similar issue with play 2.1-rc1.
    I've put all my messages in default file "message" and when i run play locally everithing works just fine but when i generate a package(.zip) for production play is creating a jar that also have a message file that is "overriding" mine.

    ResponderEliminar
  3. @Kemp that's weird, are you sure there are no message files in your conf folder in your Play project?

    ResponderEliminar
  4. I'm sure. If i remove that messages file that is created at the packaging process in the jar("play.play_2.10-2.1-RC1.jar") my messages file is loaded correctly.

    ResponderEliminar