| 1 |
if (!window.SequraFE) { |
| 2 |
window.SequraFE = {}; |
| 3 |
} |
| 4 |
|
| 5 |
(function () { |
| 6 |
/** |
| 7 |
* A Translation service. This class turns an input key and params to the translated text. |
| 8 |
* The translations are used from the global SequraFE.translations object. It expects two keys in this object: |
| 9 |
* 'current' and 'default', where 'current' holds the translations for the current language, |
| 10 |
* and 'default' holds the translations in the default language. The 'default' will be used as a fallback if |
| 11 |
* the 'current' object does not have the given entry. Both properties should be objects with the "section - key" |
| 12 |
* format. For example: |
| 13 |
* current: { |
| 14 |
* login: { |
| 15 |
* title: 'The title', |
| 16 |
* subtitle: 'This is the subtitle of the %s app.' |
| 17 |
* }, |
| 18 |
* secondPage: { |
| 19 |
* title: 'The second page title', |
| 20 |
* description: 'Use this page to set the second thing.' |
| 21 |
* } |
| 22 |
* } |
| 23 |
* |
| 24 |
* With this in mind, the translation keys are in format "section.key", for example "login.title". |
| 25 |
* |
| 26 |
* @constructor |
| 27 |
*/ |
| 28 |
function TranslationService() { |
| 29 |
/** |
| 30 |
* Gets the translation from the dictionary if exists. |
| 31 |
* |
| 32 |
* @param {'default' | 'current'} type |
| 33 |
* @param {string} group |
| 34 |
* @param {string | string[]} key |
| 35 |
* @returns {null|string} |
| 36 |
*/ |
| 37 |
const getTranslation = (type, group, key) => { |
| 38 |
// debugger |
| 39 |
if (SequraFE.translations[type][group] && SequraFE.translations[type][group]) { |
| 40 |
let value = SequraFE.translations[type][group]; |
| 41 |
if (Array.isArray(key)) { |
| 42 |
return key.reduce((value, key) => { |
| 43 |
if (value && value.hasOwnProperty(key)) { |
| 44 |
return value[key]; |
| 45 |
} |
| 46 |
|
| 47 |
return null; |
| 48 |
}, value); |
| 49 |
} |
| 50 |
|
| 51 |
if (value && value.hasOwnProperty(key)) { |
| 52 |
return value[key]; |
| 53 |
} |
| 54 |
|
| 55 |
return null; |
| 56 |
} |
| 57 |
|
| 58 |
return null; |
| 59 |
}; |
| 60 |
|
| 61 |
/** |
| 62 |
* Replaces the parameters in the given text, if any. |
| 63 |
* |
| 64 |
* @param {string} text |
| 65 |
* @param {[]} params |
| 66 |
* @return {string} |
| 67 |
*/ |
| 68 |
const replaceParams = (text, params) => { |
| 69 |
if (!params) { |
| 70 |
return text; |
| 71 |
} |
| 72 |
|
| 73 |
let i = 0; |
| 74 |
return text.replace(/%s/g, function () { |
| 75 |
const param = params[i] !== undefined ? params[i] : '%s'; |
| 76 |
i++; |
| 77 |
|
| 78 |
return param; |
| 79 |
}); |
| 80 |
}; |
| 81 |
|
| 82 |
/** |
| 83 |
* Returns a translated string based on the input key and given parameters. If the string to translate |
| 84 |
* has parameters, the placeholder is "%s". For example: Input key %s is not valid. This method will |
| 85 |
* replace parameters in the order given in the params array, if any. |
| 86 |
* |
| 87 |
* @param {string} key The translation key in format "group.key". |
| 88 |
* @param {[]} [params] An array of parameters to be replaced in the output string. |
| 89 |
* |
| 90 |
* @return {string} |
| 91 |
*/ |
| 92 |
this.translate = (key, params) => { |
| 93 |
const [group, ...keys] = key.split('.'); |
| 94 |
|
| 95 |
const result = getTranslation('current', group, keys) || getTranslation('default', group, keys); |
| 96 |
if (result !== null) { |
| 97 |
return replaceParams(result, params); |
| 98 |
} |
| 99 |
|
| 100 |
return replaceParams(key, params); |
| 101 |
}; |
| 102 |
|
| 103 |
/** |
| 104 |
* Replaces the translations in the given HTML code. |
| 105 |
* |
| 106 |
* @param {string} html |
| 107 |
* @return {string} The updated HTML. |
| 108 |
*/ |
| 109 |
this.translateHtml = (html) => { |
| 110 |
// Replace the placeholders for translations. They are in the format {$key|param1|param2}. |
| 111 |
let format = /{\$[.\-_A-Za-z|]+}/g; |
| 112 |
const me = this; |
| 113 |
|
| 114 |
return html.replace(format, (key) => { |
| 115 |
// remove the placeholder characters to get "key|param1|param2" |
| 116 |
key = key.substring(2, key.length - 1); |
| 117 |
// split parameters |
| 118 |
let params = key.split('|'); |
| 119 |
|
| 120 |
return me.translate(params[0], params.slice(1)) || key; |
| 121 |
}); |
| 122 |
}; |
| 123 |
} |
| 124 |
|
| 125 |
SequraFE.translationService = new TranslationService(); |
| 126 |
})(); |
| 127 |
|