Skip to content

Instantly share code, notes, and snippets.

Created February 24, 2012 14:34
Show Gist options
  • Save anonymous/1901267 to your computer and use it in GitHub Desktop.
Save anonymous/1901267 to your computer and use it in GitHub Desktop.
localization discussion
var completeString = _('I am {role} and {nationality}', 'domain', {
role: 'monkey',
nationality: 'norwegian'
});
var featureList = '<ul>' + _('#info', 'domain').join('</li><li>') + '</li>';
function _(string, domain, values) {
var processedValues = {};
for (var key in values) {
var value = values[key];
// var translatedValues = _(value, domain);
var translatedValues = _(key, domain);
if (typeof(translatedValues[value] == 'undefined')) {
// don't have a translation, return the value instead...
processedValues[key] = value;
// TODO; error log warning 'whooopa, forgot to translate something there or invalid key!';
}
// found it, keep the value
var translatedValue = translatedValues[value];
processedValues[key] = translatedValue;
string.replace(/['{', key, '}'].join('')/, processedValues[key]);
}
return string;
// _validate('I am {role} and {nationality}', 'domain', 'role', role);
}
Translation file:
'current_strength' : 'Current password strength',
'#info' : [
'localized line #1',
'localized line #2'
],
'I am {#phoneno} and {nationality}': 'Jeg er {phoneno} og {nationality}',
'I am {role} and {nationality:common}': 'Jeg er {role} og {nationality}',
'role' : {
'administrator': 'administrator',
'developer': 'utvikler'
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment