* 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." * @param {string} template Text with placeholder
(template, variables)
| 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); |