I'm devising an i18n helper for JavaScript and would like some input on the implementation.
In the basic scenario it should provide some sort of lookup functionality based on a locale object: (with support for variables)
var locales = {
en : {
pages : {
myPage1 : {
title : "A title with <em>highlighted %s</em>",
}
}
}
}
function _(path) {
//lookup and return translation
}
_(pages.myPage1.title, "text") => "A title with <em>highlighted text</em>"
Later, I'd like to implement support for plural/singular etc:
var locales = {
en : {
pages : {
myPage1 : {
title : "A title with <em>%s text</em>",
tickets : {
_0 : "0 tickets left",
_1 : "1 ticket left",
_1+ : "%s tickets left"
}
}
}
}
}
function _n(path, count) {
//lookup and return translation
}
_n(pages.myPage1.ticketsLeft,5) => "5 tickets left"
... And also integrate this with a templating engine:
<h1>{{ _(pages.myPage1.title) }}</h1>
<p>{{ _n(pages.myPage1.ticketsLeft, {{ variable }} ) }}</p>
Questions:
What pitfalls are there in basic i18n and what does the implementation above lack that should be considered from the beginning?
How would you code the actual methods _(), _n() so that they work with an object as the one given?
Any ideas on the template integration - how should it be done?
Are there any "translation crowd-sourcing services" that work well together with JavaScript?