forked from iNavFlight/inav-configurator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocalization.js
255 lines (197 loc) · 7.7 KB
/
localization.js
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
'use strict';
// Thanks to Betaflight :)
window.$ = window.jQuery = require('jquery');
const { app } = require('@electron/remote');
const path = require('path');
const i18next = require('i18next');
const i18nextXHRBackend = require('i18next-xhr-backend');
const Store = require('electron-store');
const store = new Store();
const availableLanguages = ['en', 'ja', 'uk','zh_CN'];
const i18n = {};
i18n.init = function (callback) {
i18next.use(i18nextXHRBackend);
i18next.init({
lng: store.get('userLanguage', app.getLocale()),
getAsync: false,
debug: true,
ns: ['messages'],
defaultNS:['messages'],
fallbackLng: 'en',
backend: {
loadPath: path.join(__dirname, "./../locale/{{lng}}/{{ns}}.json"),
parse: i18n.parseInputFile,
},
}, function(err) {
if (err !== undefined) {
console.error(`Error loading i18n: ${err}`);
} else {
console.log('i18n system loaded');
const detectedLanguage = i18n.getMessage(`language_${i18n.getValidLocale("DEFAULT")}`);
i18next.addResourceBundle('en', 'messages', {"detectedLanguage": detectedLanguage }, true, true);
i18next.on('languageChanged', function () {
i18n.localize(true);
});
}
if (callback) {
callback();
}
});
}
i18n.parseInputFile = function (data) {
// Remove the $n interpolate of Chrome $1, $2, ... -> {{1}}, {{2}}, ...
const REGEXP_CHROME = /\$([1-9])/g;
const dataChrome = data.replace(REGEXP_CHROME, '{{$1}}');
// Remove the .message of the nesting $t(xxxxx.message) -> $t(xxxxx)
const REGEXP_NESTING = /\$t\(([^\)]*).message\)/g;
const dataNesting = dataChrome.replace(REGEXP_NESTING, '$t($1)');
// Move the .message of the json object to root xxxxx.message -> xxxxx
const jsonData = JSON.parse(dataNesting);
Object.entries(jsonData).forEach(([key, value]) => {
jsonData[key] = value.message;
});
return jsonData;
}
i18n.getValidLocale = function(userLocale) {
let validUserLocale = userLocale;
if (validUserLocale === 'DEFAULT') {
validUserLocale = app.getLocale();
console.log(`Detected locale ${validUserLocale}`);
}
return validUserLocale;
}
i18n.getMessage = function(messageID, parameters) {
let parametersObject;
if (!i18next.exists(messageID)) {
return false;
}
// Option 1, no parameters or Object as parameters (i18Next type parameters)
if ((parameters === undefined) || ((parameters.constructor !== Array) && (parameters instanceof Object))) {
parametersObject = parameters;
// Option 2: parameters as $1, $2, etc.
// (deprecated, from the old Chrome i18n
} else {
// Convert the input to an array
let parametersArray = parameters;
if (parametersArray.constructor !== Array) {
parametersArray = [parameters];
}
parametersObject = {};
parametersArray.forEach(function(parameter, index) {
parametersObject[index + 1] = parameter;
});
}
return i18next.t(messageID, parametersObject);
};
i18n.getCurrentLanguage = function() {
return i18next.language;
};
i18n.getLanguages = function() {
return availableLanguages;
}
i18n.changeLanguage = function(languageSelected) {
store.set('userLanguage', languageSelected);
i18next.changeLanguage(i18n.getValidLocale(languageSelected));
//i18n.selectedLanguage = languageSelected;
};
i18n.localize = function (reTranslate = false) {
let localized = 0;
const translate = function(messageID) {
localized++;
return i18n.getMessage(messageID);
};
if (reTranslate) {
$('[i18n]').each(function() {
const element = $(this);
element.html(translate(element.attr('i18n')));
});
$('[data-i18n]').each(function() {
const element = $(this);
const translated = translate(element.data('i18n'));
element.html(translated);
if (element.attr("title") !== "") {
element.attr("title", translated);
}
});
$('[i18n_title]').each(function() {
const element = $(this);
element.attr('title', translate(element.attr('i18n_title')));
});
$('[data-i18n_title]').each(function() {
const element = $(this);
element.attr('title', translate(element.data('i18n_title')));
});
$('[i18n_label]').each(function() {
const element = $(this);
element.attr('label', translate(element.attr('i18n_label')));
});
$('[data-i18n_label]').each(function() {
const element = $(this);
element.attr('label', translate(element.data('i18n_label')));
});
$('[i18n_value]').each(function() {
const element = $(this);
element.val(translate(element.attr('i18n_value')));
});
$('[i18n_placeholder]').each(function() {
const element = $(this);
element.attr('placeholder', translate(element.attr('i18n_placeholder')));
});
$('[i18n_lang]').each(function() {
const element = $(this);
element.attr('lang', translate(element.attr('i18n_lang')));
});
} else {
$('[i18n]:not(.i18n-replaced)').each(function() {
const element = $(this);
element.html(translate(element.attr('i18n')));
element.addClass('i18n-replaced');
});
$('[data-i18n]:not(.i18n-replaced)').each(function() {
const element = $(this);
const translated = translate(element.data('i18n'));
element.html(translated);
element.addClass('i18n-replaced');
if (element.attr("title") !== "") {
element.attr("title", translated);
}
});
$('[i18n_title]:not(.i18n_title-replaced)').each(function() {
const element = $(this);
element.attr('title', translate(element.attr('i18n_title')));
element.addClass('i18n_title-replaced');
});
$('[data-i18n_title]:not(.i18n_title-replaced)').each(function() {
const element = $(this);
element.attr('title', translate(element.data('i18n_title')));
element.addClass('i18n_title-replaced');
});
$('[i18n_label]:not(.i18n_label-replaced)').each(function() {
const element = $(this);
element.attr('label', translate(element.attr('i18n_label')));
element.addClass('i18n_label-replaced');
});
$('[data-i18n_label]:not(.i18n_label-replaced)').each(function() {
const element = $(this);
element.attr('label', translate(element.data('i18n_label')));
element.addClass('i18n_label-replaced');
});
$('[i18n_value]:not(.i18n_value-replaced)').each(function() {
const element = $(this);""
element.val(translate(element.attr('i18n_value')));
element.addClass('i18n_value-replaced');
});
$('[i18n_placeholder]:not(.i18n_placeholder-replaced)').each(function() {
const element = $(this);
element.attr('placeholder', translate(element.attr('i18n_placeholder')));
element.addClass('i18n_placeholder-replaced');
});
$('[i18n_lang]:not(.i18n_lang-replaced)').each(function() {
const element = $(this);
element.attr('lang', translate(element.attr('i18n_lang')));
element.addClass('i18n_lang-replaced');
});
}
return localized;
}
module.exports = i18n;