Last active
October 11, 2015 03:08
-
-
Save tzengerink/3793350 to your computer and use it in GitHub Desktop.
I18N.js
This file contains 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
/*! | |
* I18N.js | |
* ------- | |
* | |
* Add internationalisation to your JavaScript application. | |
* | |
* // Get current language | |
* I18N.lang(); // en | |
* | |
* // Set current language | |
* I18N.lang("nl"); | |
* | |
* // Set all language tables | |
* I18N.set({ | |
* "es" : { "Hello World" : "Hola Mundo"}, | |
* "de" : { "Hello World" : "Hallo Welt"} | |
* }); | |
* | |
* // Set single language table | |
* I18N.set("nl", { | |
* "Hello World" : "Hallo Wereld", | |
* "{{count}} new messages" : "{{count}} nieuwe berichten" | |
* }); | |
* | |
* // Get a translation | |
* I18N.get("Hello World"); // Hallo Wereld | |
* I18N.get("Hello World", "es"); // Hola Mundo | |
* | |
* // Get a translation using the shorthand function | |
* __("Hello World", "de"); // Hallo Welt | |
* __("{{count}} new messages", { "count" : 12 }); // 12 nieuwe berichten | |
* __("{{count}} new messages", { "count" : 25 }, "es"); // 25 new messages | |
* | |
* // Change single configuration element | |
* I18N.config("rDelim", "%%"); | |
* | |
* // Change multiple configuration elements | |
* I18N.config({ | |
* lDelim : "[[", | |
* rDelim : "]]" | |
* }); | |
* | |
* Copyright (c) 2013, T. Zengerink | |
* Licensed under MIT License | |
* See: https://gist.github.com/raw/3151357/6806e68cb9cc0042b265f25be9bc25dd39f75267/LICENSE.md | |
*/ | |
var I18N = (function(win){ | |
var I18N = {}, | |
config = { | |
language : "en", | |
lDelim : "{{", | |
rDelim : "}}" | |
}, | |
tables = {}; | |
// PRIVATE | |
// ------- | |
// Load language table | |
var load = function( lang ){ | |
return (lang in tables) ? tables[lang] : {}; | |
}; | |
// Parse string with values | |
var parse = function( str, values ){ | |
for (key in values) { | |
str = str.replace(config.lDelim + key + config.rDelim, values[key], "g"); | |
} | |
return str; | |
}; | |
// PUBLIC | |
// ------ | |
// Get/set configuration | |
I18N.config = function( conf, value ) { | |
if (typeof conf == "object") { | |
for (key in conf) { | |
config[key] = conf[key]; | |
} | |
} else if (typeof conf == "string") { | |
config[conf] = value; | |
} | |
return config; | |
}; | |
// Get a translation | |
I18N.get = function( str, values, lang ){ | |
var table, | |
translation; | |
if (typeof values != "object") { | |
lang = values; | |
values = {}; | |
} | |
if (typeof lang != "string") { | |
lang = config.language; | |
} | |
table = load(lang); | |
if (str in table) { | |
return parse(table[str], values); | |
} | |
return parse(str, values); | |
} | |
// Get/set current language | |
I18N.lang = function( lang ){ | |
if (typeof lang == "string") { | |
config.language = lang; | |
} | |
return config.language; | |
}; | |
// Get/set language tables | |
I18N.set = function( lang, table ) { | |
if (typeof lang == "object") { | |
for (l in lang) { | |
tables[l] = lang[l]; | |
} | |
} else if (typeof lang == "string") { | |
tables[lang] = table; | |
} | |
return tables; | |
}; | |
// SETUP | |
// ----- | |
if (typeof win.__ == "undefined") { | |
win.__ = function( str, values, lang ){ | |
return I18N.get(str, values, lang); | |
}; | |
} | |
return I18N; | |
})(window); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment