Skip to content

Instantly share code, notes, and snippets.

@simon04
Created August 12, 2020 15:21
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 simon04/efcfd18be80f90b5ef008086aa0e7350 to your computer and use it in GitHub Desktop.
Save simon04/efcfd18be80f90b5ef008086aa0e7350 to your computer and use it in GitHub Desktop.
Minimalistic i18n for JavaScript
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