-
-
Save simon04/efcfd18be80f90b5ef008086aa0e7350 to your computer and use it in GitHub Desktop.
Minimalistic i18n for JavaScript
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
var i18nMessages = {}; | |
["en", "de", "it"].forEach(function (lang) { | |
jQuery.ajax({ | |
url: "./i18n/" + lang + ".json", | |
dataType: 'json', | |
async: false, | |
success: function (data) { | |
i18nMessages[lang] = data; | |
} | |
}) | |
}) | |
/** | |
* Returns the translation for the given message key. | |
* @param {string} key the message key | |
* @param {*} argument an argument to substitue $1 in the message for | |
*/ | |
function t(key, argument) { | |
var messages = i18nMessages[srcLang] || i18nMessages.en; | |
var message = messages[key] || i18nMessages.en[key] || key; | |
return argument !== undefined ? message.replace(/\$1/g, argument) : message; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment