Created
January 17, 2017 14:13
-
-
Save CyanoFresh/d1bcd6be7bb99d9a3611bfad1ddf214b to your computer and use it in GitHub Desktop.
JS Simple i18n key => value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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