Grails - Server-side internationalization for Backbone and Handlebars

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I m working on a Grails / Backbone / Handlebars application that s a front end to a much larger legacy Java system in which (for historical & customizability reasons) internationalization messages are deep in a database hidden behind a couple of SOAP services which are in turn hidden behind various internal Java libraries. Getting at these messages from the Grails layer is easy and works fine.

What I m wondering, though, is how to get (for instance) internationalized labels into my Handlebars templates.

Right now, I m using GSP fragments to generate the templates, including a custom tag that gets the message I m interested in, something like:

<li><myTags:message msgKey="title"/> {{title}}</li>

https://github.com/fnando/i18n-js https://github.com/fnando/i18n-js

So far the best thing I can think of is to wedge the labels into the Backbone model as well, so I d end up with something like

<li>{{titleLabel}} {{title}}</li>

However, this really gets away from the ideal of building the Backbone models on top of a nice clean RESTful JSON API -- either the JSON returned by the RESTful service is cluttered up with presentation data (i.e., localized labels), or I have to do additional work to inject the labels into the Backbone model -- and cluttering up the Backbone model with presentation data seems wrong as well.

I think what I d like to do, in terms of clean data and clean APIs, is write another RESTful service that takes a list of message keys and similar, and returns a JSON data structure containing all the localized messages. However, questions remain:

    • What s the best way to indicate (probably in the template) what message keys are needed for a given view?
    • What s the right format for the data?
    • How do I get the localized messages into the Backbone views?
    • Are there any existing Javascript libraries that will help, or should I just start making stuff up?
    • Is there a better / more standard alternative approach?

Answers

http://i18next.com http://i18next.com

i18next can be configured to load resources dynamically. It supports JSON in a gettext format (supporting plural and context variants). Example from their page on how to load remote resources:

var option = { 
  resGetPath:  resources.json?lng=__lng__&ns=__ns__ ,
  dynamicLoad: true 
};

i18n.init(option);

(You will of course need more configuration like setting the language, the fallback language etc.)

You can then configure a Handlebars helper that calls i18next on the provided variable (simplest version, no plural, no context):

// namespace: "translation" (default)
Handlebars.registerHelper( _ , function (i18n_key) {
    i18n_key = Handlebars.compile(i18n_key)(this);
    var result = i18n.t(i18n_key);
    if (!result) {
        console.log("ERROR : Handlebars-Helpers : no translation result for " + i18n_key);
    }
    return new Handlebars.SafeString(result);
});

And in your template you can either provide a dynamic variable that expands to the key:

<li>{{_ titleLabeli18nKey}} {{title}}</li>

or specify the key directly:

<li>{{_ "page.fancy.title"}} {{title}}</li>

http://momentjs.com http://momentjs.com

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/14206586/server-side-internationalization-for-backbone-and-handlebars

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils