As SodaHead extended over the ocean and opened a new website in germany we were faced with a massive translation integration. We are using Jinja2 on the backend, i18n is part of the batteries. JavaScript on the other side, doesn't provide anything to handle i18n so I had to come up with my own gettext lib.
JavaScript gettext _
Here is a solution we use at SodaHead to handle translation on the client side while being compatible with gettext translation files.
function _(x,v){
x = SH.i18n && SH.i18n._[x] || x;
for(var k in v){
x=x.replace(RegExp('%\\('+k+'\\)(s|d)', "g"), v[k]);
}
return x;
}
Here is my locale dictionary generated by gettext
SH.i18n._ = {
'You have %(num)d dollars in %(account)s.': 'Votre compte %(account)s dispose de %(num)d dollars.'
};
Example
<script>
_('You have %(num)d dollars in %(account)s.', {num:123, account:'AB547F'});
</script>