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