diff --git a/js/module.js b/js/module.js index 457b9772..0d51e559 100644 --- a/js/module.js +++ b/js/module.js @@ -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) diff --git a/js/translator.js b/js/translator.js index d715b383..b82a739e 100644 --- a/js/translator.js +++ b/js/translator.js @@ -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; diff --git a/modules/default/calendar/calendar.js b/modules/default/calendar/calendar.js index fa77c94a..9657ece4 100644 --- a/modules/default/calendar/calendar.js +++ b/modules/default/calendar/calendar.js @@ -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'); diff --git a/translations/fi.json b/translations/fi.json index 08bc4060..c013e310 100644 --- a/translations/fi.json +++ b/translations/fi.json @@ -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",