* Load a translation for a given key for a given module. * @param {Module} module The module to load the translation for. * @param {string} key The key of the text to translate. * @param {object} variables The variables to use within the translation template (optional) * @returns {string
(module, key, variables = {})
| 37 | * @returns {string} the translated key |
| 38 | */ |
| 39 | translate (module, key, variables = {}) { |
| 40 | |
| 41 | /** |
| 42 | * Combines template and variables like: |
| 43 | * template: "Please wait for {timeToWait} before continuing with {work}." |
| 44 | * variables: {timeToWait: "2 hours", work: "painting"} |
| 45 | * to: "Please wait for 2 hours before continuing with painting." |
| 46 | * @param {string} template Text with placeholder |
| 47 | * @param {object} variables Variables for the placeholder |
| 48 | * @returns {string} the template filled with the variables |
| 49 | */ |
| 50 | function createStringFromTemplate (template, variables) { |
| 51 | if (Object.prototype.toString.call(template) !== "[object String]") { |
| 52 | return template; |
| 53 | } |
| 54 | let templateToUse = template; |
| 55 | if (variables.fallback && !template.match(new RegExp("{.+}"))) { |
| 56 | templateToUse = variables.fallback; |
| 57 | } |
| 58 | return templateToUse.replace(new RegExp("{([^}]+)}", "g"), function (_unused, varName) { |
| 59 | return varName in variables ? variables[varName] : `{${varName}}`; |
| 60 | }); |
| 61 | } |
| 62 | |
| 63 | if (this.translations[module.name] && key in this.translations[module.name]) { |
| 64 | return createStringFromTemplate(this.translations[module.name][key], variables); |
| 65 | } |
| 66 | |
| 67 | if (key in this.coreTranslations) { |
| 68 | return createStringFromTemplate(this.coreTranslations[key], variables); |
| 69 | } |
| 70 | |
| 71 | if (this.translationsFallback[module.name] && key in this.translationsFallback[module.name]) { |
| 72 | return createStringFromTemplate(this.translationsFallback[module.name][key], variables); |
| 73 | } |
| 74 | |
| 75 | if (key in this.coreTranslationsFallback) { |
| 76 | return createStringFromTemplate(this.coreTranslationsFallback[key], variables); |
| 77 | } |
| 78 | |
| 79 | return key; |
| 80 | }, |
| 81 | |
| 82 | /** |
| 83 | * Load a translation file (json) and remember the data. |
nothing calls this directly
no test coverage detected