I am working on a Grails / Backbone / Handlebars application, which is the interface for a much older Java system, in which (for historical and custom reasons) internationalization messages deep in the database, hidden behind several SOAP services, are in turn hidden behind various internal Java libraries. Receiving these messages from the Grails level is easy and great.
I am wondering how to get (for example) internationalized shortcuts in Handlebars templates.
I am currently using GSP snippets to create templates, including a special tag that receives a message that interests me, for example:
<li><myTags:message msgKey="title"/> {{title}}</li>
However, for performance and code reasons, I want to get away from GSP templates and get them in direct HTML. I looked a bit at client-side internationalization options such as i18n.js , but they seem to depend on the existence of the message file I did not receive. (I could create it, perhaps, but it would be ginormous and expensive.)
So far, the best thing I can think of is to wedge the shortcuts into the Backbone model, so that I get something like
<li>{{titleLabel}} {{title}}</li>
However, this does away with the ideal of building Backbone models on top of a clean, clean RESTful JSON API - either the JSON returned by the RESTful service is clogged with presentation data (i.e. localized labels), or I have to do extra work to insert labels into the Backbone model, and also clutter The Backbone model with presentation data also seems to be incorrect.
I think what I would like to do in terms of clean data and clean APIs is to write another RESTful service that accepts a list of message keys and the like, and returns a JSON data structure containing all localized messages. However, questions remain:
- What is the best way to indicate (possibly in a template) which message keys are needed for a given view?
- What is the correct format for the data?
- How to get localized messages in Backbone views?
- Are there any existing Javascript libraries that will help, or should I just start creating files?
- Is there a more standard alternative approach?