Add better support for translations with backwards compatibility

This commit is contained in:
Mikko Tapionlinna 2017-04-25 23:15:34 +03:00
parent a6485b61a4
commit ee88897b18
4 changed files with 38 additions and 14 deletions

View File

@ -272,14 +272,18 @@ var Module = Class.extend({
}
},
/* translate(key, defaultValue)
* Request the translation for a given key.
/* translate(key, defaultValueOrVariables, defaultValue)
* Request the translation for a given key with optional variables and default value.
*
* argument key string - The key of the string to translage
* argument defaultValue string - The default value if no translation was found. (Optional)
* argument key string - The key of the string to translate
* argument defaultValueOrVariables string/object - The default value or variables for translating. (Optional)
* argument defaultValue string - The default value with variables. (Optional)
*/
translate: function (key, defaultValue) {
return Translator.translate(this, key) || defaultValue || "";
translate: function (key, defaultValueOrVariables, defaultValue) {
if(typeof defaultValueOrVariables === "object") {
return Translator.translate(this, key, defaultValueOrVariables) || defaultValue || "";
}
return Translator.translate(this, key) || defaultValueOrVariables || "";
},
/* updateDom(speed)

View File

@ -111,32 +111,47 @@ var Translator = (function() {
translations: {},
translationsFallback: {},
/* translate(module, key)
/* translate(module, key, variables)
* Load a translation for a given key for a given module.
*
* argument module Module - The module to load the translation for.
* argument key string - The key of the text to translate.
* argument variables - The variables to use within the translation template (optional)
*/
translate: function(module, key) {
translate: function(module, key, variables) {
variables = variables || {}; //Empty object by default
// Combines template and variables like:
// template: "Please wait for {timeToWait} before continuing with {work}."
// variables: {timeToWait: "2 hours", work: "painting"}
// to: "Please wait for 2 hours before continuing with painting."
function createStringFromTemplate(template, variables) {
if(variables.fallback && !template.match(new RegExp("\{.+\}"))) {
template = variables.fallback;
}
return template.replace(new RegExp("\{([^\}]+)\}", "g"), function(_unused, varName){
return variables[varName] || "{"+varName+"}";
});
}
if(this.translations[module.name] && key in this.translations[module.name]) {
// Log.log("Got translation for " + key + " from module translation: ");
return this.translations[module.name][key];
return createStringFromTemplate(this.translations[module.name][key], variables);
}
if (key in this.coreTranslations) {
// Log.log("Got translation for " + key + " from core translation.");
return this.coreTranslations[key];
return createStringFromTemplate(this.coreTranslations[key], variables);
}
if (this.translationsFallback[module.name] && key in this.translationsFallback[module.name]) {
// Log.log("Got translation for " + key + " from module translation fallback.");
return this.translationsFallback[module.name][key];
return createStringFromTemplate(this.translationsFallback[module.name][key], variables);
}
if (key in this.coreTranslationsFallback) {
// Log.log("Got translation for " + key + " from core translation fallback.");
return this.coreTranslationsFallback[key];
return createStringFromTemplate(this.coreTranslationsFallback[key], variables);
}
return key;

View File

@ -265,7 +265,12 @@ Module.register("calendar", {
}
}
} else {
timeWrapper.innerHTML = this.capFirst(this.translate("RUNNING")) + " " + moment(event.endDate, "x").fromNow(true);
timeWrapper.innerHTML = this.capFirst(
this.translate("RUNNING", {
fallback: this.translate("RUNNING") + " {timeUntilEnd}",
timeUntilEnd: moment(event.endDate, "x").fromNow(true)
})
);
}
}
//timeWrapper.innerHTML += ' - '+ moment(event.startDate,'x').format('lll');

View File

@ -4,7 +4,7 @@
"TODAY": "Tänään",
"TOMORROW": "Huomenna",
"DAYAFTERTOMORROW": "Ylihuomenna",
"RUNNING": "Meneillään",
"RUNNING": "Päättyy {timeUntilEnd} päästä",
"EMPTY": "Ei tulevia tapahtumia.",
"N": "P",