PluginProbe
seQura / 3.1.1
seQura v3.1.1
4.3.4 4.3.3 4.3.2 4.3.1 trunk 2.0.0 2.0.10 2.0.11 2.0.12 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 3.0.0 3.0.2 3.0.5 3.0.6 3.0.7 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 4.0.0 All 30 releases
sequra / assets / js / src / core / TranslationService.js

TranslationService.js in seQura 3.1.1, at assets/js/src/core/TranslationService.js

127 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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