-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcube-i18n-behavior.js
331 lines (297 loc) · 8.98 KB
/
cube-i18n-behavior.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import "@polymer/iron-ajax/iron-ajax.js";
import {CubeIteratorBehavior} from "./cube-iterator-behavior.js";
import IntlMessageFormat from "intl-messageformat/src/main.js";
import {dedupingMixin} from "@polymer/polymer/lib/utils/mixin";
/** @polymerBehavior */
export const CubeI18nBehavior = dedupingMixin(superClass => {
class CubeI18nBehavior extends CubeIteratorBehavior(superClass) {
static get properties()
{
return {
languages: {
type: Array,
value: function () {return Array.prototype.slice.call(navigator.languages)}
},
/**
* The dictionary of localized messages, for each of the languages that
* are going to be used. See http://formatjs.io/guides/message-syntax/ for
* more information on the message syntax.
*
* For example, a valid dictionary would be:
* this.resources = {
* 'en': { 'greeting': 'Hello!' }, 'fr' : { 'greeting': 'Bonjour!' }
* }
*/
resources: {
type: Object
},
/**
* The path to the dictionary of localized messages. The format is the
* same as the `resources` array, only saved as an external json file.
* Note that using a path will populate the `resources` property, and override
* the previous data.
*/
pathToResources: {
type: String
},
/**
* Optional dictionary of user defined formats, as explained here:
* http://formatjs.io/guides/message-syntax/#custom-formats
*
* For example, a valid dictionary of formats would be:
* this.formats = {
* number: { USD: { style: 'currency', currency: 'USD' } }
* }
*/
formats: {
type: Object,
value: function () { return {} }
},
/**
* Translates a string to the current `language`. Any parameters to the
* string should be passed in order, as follows:
* `i18n(stringKey, param1Name, param1Value, param2Name, param2Value)`
*/
i18n: {
type: Function,
computed: '__computeI18n(languages, resources, formats)'
},
/**
* Translates a string to the current `language`. Any parameters to the
* string should be passed in order, as follows:
* `i18nResource(resources, param1Name, param1Value, param2Name, param2Value)`
*/
i18nResource: {
type: Function,
computed: '__computeI18nResource(languages, formats)'
},
/**
* Translates a string to the current `language`. Any parameters to the
* string should be passed in an object, as follows:
* `i18n(stringKey, keyValue)`
*/
i18nData: {
type: Function,
computed: '__computeI18nData(i18n)'
},
/**
* Translates a string to the current `language`. Any parameters to the
* string should be passed in an object, as follows:
* `i18n(resources, keyValue)`
*/
i18nResourceData: {
type: Function,
computed: '__computeI18nData(i18nResource)'
},
__localizationCache: {
type: Object,
value: {
requests: {}, /* One iron-request per unique resources path. */
messages: {}, /* Unique localized strings. Invalidated when the language, formats or resources change. */
ajax: null /* Global iron-ajax object used to request resource files. */
}
}
}
}
loadResources(path)
{
// If the global ajax object has not been initialized, initialize and cache it.
let ajax = this.__localizationCache.ajax;
if(!ajax)
{
//noinspection JSValidateTypes
ajax = this.__localizationCache.ajax = document.createElement('iron-ajax');
}
let request = this.__localizationCache.requests[path];
if(!request)
{
ajax.url = path;
request = ajax.generateRequest();
request.completes.then(
this.__onRequestResponse.bind(this),
this.__onRequestError.bind(this)
);
// Cache the instance so that it can be reused if the same path is loaded.
this.__localizationCache.requests[path] = request;
}
else
{
request.completes.then(
this.__onRequestResponse.bind(this),
this.__onRequestError.bind(this)
);
}
}
__getTranslation(passedArguments, resources, languages, formats, cache)
{
let
self = this,
keys = passedArguments[0];
if(!keys || !resources || !languages)
{
return;
}
if(languages.indexOf('en') === -1)
{
languages.push('en');
}
keys = typeof keys === 'string' ? [keys] : keys;
let keyResult = self.iterate(
keys, function (key) {
let result = self.iterate(
languages, function (lang) {
if(resources.hasOwnProperty(lang))
{
let inKeys = resources[lang];
if(inKeys.hasOwnProperty(key))
{
return {text: inKeys[key], language: lang};
}
}
}
);
if(result !== undefined)
{
return [key, result];
}
}
);
let key, result = null;
if(keyResult !== undefined)
{
key = keyResult[0];
result = keyResult[1];
}
else
{
key = keys.shift();
}
if(result === null)
{
console.debug(key + " is not currently supported in your element");
return key;
}
// Cache the key/value pairs for the same language, so that we don't
// do extra work if we're just reusing strings across an application.
let messageKey = key + result.text, msg;
if(cache)
{
msg = cache.messages[messageKey];
}
if(!msg)
{
msg = new IntlMessageFormat(result.text, result.language, formats);
if(cache)
{
cache.messages[messageKey] = msg;
}
}
let args = {};
for(let i = 1; i < passedArguments.length; i += 2)
{
args[passedArguments[i]] = passedArguments[i + 1];
}
return msg.format(args);
}
/**
* Returns a computed `i18n` method, based on the browser languages.
*/
__computeI18n(languages, resources, formats)
{
let
self = this;
// Everytime any of the parameters change, invalidate the strings cache.
this.__localizationCache.messages = {};
return function () {
return self.__getTranslation(
arguments, resources, languages, formats, this.__localizationCache
)
};
}
__computeI18nResource(languages, formats)
{
let self = this;
return function () {
let args = [];
for(let i in arguments)
{
if(arguments.hasOwnProperty(i))
{
args.push(arguments[i]);
}
}
let resources = args.shift();
args.unshift('_cube_res_key_');
let newResources = {};
for(let i in resources)
{
if(resources.hasOwnProperty(i))
{
newResources[i] = {};
newResources[i]['_cube_res_key_'] = resources[i];
}
}
return self.__getTranslation(args, newResources, languages, formats);
};
}
__computeI18nData(i18n)
{
return function () {
if(!i18n)
{
return;
}
let stringKey = arguments[0];
let args = [];
for(let i in arguments[1])
{
if(arguments[1].hasOwnProperty(i))
{
args.push(i);
args.push(arguments[1][i]);
}
}
args.unshift(stringKey);
return i18n.apply(this, args);
}
}
__onRequestResponse(event)
{
this.resources = event.response;
this.dispatchEvent(new CustomEvent('cube-i18n-resources-loaded'));
}
__onRequestError(event)
{
this.dispatchEvent(new CustomEvent('cube-i18n-resources-error'));
}
hydrateResources(data, clear)
{
let
self = this,
resources = clear ? {} : self.resources;
if(data === Object(data))
{
self.iterate(
data, function (translations, key) {
if(translations !== Object(translations))
{
translations = {'en': translations};
}
self.iterate(
translations, function (translation, language) {
resources[language] = resources[language] || {};
resources[language][key] = translation;
}
);
}
);
}
if(Object.keys(resources).length === 0)
{
resources = undefined;
}
this.resources = resources;
}
}
return CubeI18nBehavior;
});