Skip to content

Instantly share code, notes, and snippets.

@CyanoFresh
Created January 17, 2017 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CyanoFresh/d1bcd6be7bb99d9a3611bfad1ddf214b to your computer and use it in GitHub Desktop.
Save CyanoFresh/d1bcd6be7bb99d9a3611bfad1ddf214b to your computer and use it in GitHub Desktop.
JS Simple i18n key => value
// Array with translations
var translations = {
'first': 'Hello, {{name}}!'
};
function t(key, values) {
var str = translations[key];
if (!values) {
return str;
}
for (var value in values) {
str = str.replace(RegExp("\\{{" + value + "\\}}", "gi"), values[value]);
}
return str;
}
// Usage
t('first', {name:"test"});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment