Skip to content

Instantly share code, notes, and snippets.

@simon04
Created Aug 12, 2020
Embed
What would you like to do?
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