-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18next.js
636 lines (565 loc) · 19.7 KB
/
i18next.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
import baseLogger from './logger.js'
import { getDefaults } from './defaults.js'
import { run, runAsyncLater } from './hooks.js'
import { isIE10, wait, deepFind, looksLikeObjectPath, deepExtend } from './utils.js'
import EventEmitter from './EventEmitter.js'
import ResourceStore from './ResourceStore.js'
import throwIf from './throwIf.js'
import defaultStack from './defaultStack.js'
// binds the member functions of the given class instance so that they can be destructured.
// this way you can for example just use t() instead of i18n.t()
function bindMemberFunctions (inst) {
const mems = Object.getOwnPropertyNames(Object.getPrototypeOf(inst))
mems.forEach((mem) => {
if (typeof inst[mem] === 'function') {
inst[mem] = inst[mem].bind(inst)
}
})
}
function setResolvedLanguage (self, lng) {
self.resolvedLanguage = undefined
if (['cimode', 'dev'].indexOf(lng) > -1) return
for (let li = 0; li < self.languages.length; li++) {
const lngInLngs = self.languages[li]
if (['cimode', 'dev'].indexOf(lngInLngs) > -1) continue
if (self.store.hasLanguageSomeTranslations(lngInLngs)) {
self.resolvedLanguage = lngInLngs
break
}
}
}
class I18next extends EventEmitter {
constructor (options = {}) {
super()
if (isIE10) EventEmitter.call(this) // <=IE10 fix (unable to call parent constructor)
this.isInitialized = false
const defOpt = getDefaults()
this.loading = {}
this.hookNames = [
'extendOptions',
'initializing',
'loadResources',
'read',
'resolve',
'detectLanguage',
'cacheLanguage',
'bestMatchFromCodes',
'fallbackCodes',
'resolveHierarchy',
'translated'
]
this.hooks = {}
if (!options.defaultNS && options.ns) {
if (typeof options.ns === 'string') {
options.defaultNS = options.ns
} else {
options.defaultNS = options.ns[0]
}
}
this.options = { ...defOpt, ...options }
if (options.keySeparator !== undefined) {
this.options.userDefinedKeySeparator = options.keySeparator
}
if (options.nsSeparator !== undefined) {
this.options.userDefinedNsSeparator = options.nsSeparator
}
this.language = this.options.lng
if (this.language) this.languages = [this.language]
if (!this.language && this.options.fallbackLng) this.languages = Array.isArray(this.options.fallbackLng) ? [...this.options.fallbackLng] : [this.options.fallbackLng]
this.store = new ResourceStore({}, this.options)
// append api
const storeApi = [
'getResource',
'hasResourceBundle',
'getResourceBundle',
'getDataByLanguage'
]
storeApi.forEach(fcName => {
this[fcName] = (...args) => this.store[fcName](...args)
})
const storeApiChained = [
'addResource',
'addResources',
'addResourceBundle',
'removeResourceBundle'
]
storeApiChained.forEach(fcName => {
this[fcName] = (...args) => {
this.store[fcName](...args)
return this
}
})
bindMemberFunctions(this)
}
/**
* public api
*/
use (module) {
if (!module) throw new Error('You are passing an undefined module! Please check the object you are passing to i18next.use()')
if (!module.register && module.log && module.warn && module.error) {
this._logger = module
return this
}
if (module.type) throw new Error('You are probably passing an old module! Please check the object you are passing to i18next.use()')
if (typeof module.register !== 'function') throw new Error('You are passing a wrong module! Please check the object you are passing to i18next.use()')
if (module.hooks && module.hooks.length > 0) {
module.hooks.forEach((h) => {
if (this.hookNames.indexOf(h) < 0) this.hookNames.push(h)
})
}
module.register(this)
return this
}
addHook (name, type, hook) {
if (!hook) {
hook = type
type = undefined
}
if (!hook || typeof hook !== 'function') throw new Error(`${name ? `"${name}"` : 'This'} is not a valid hook!`)
throwIf.alreadyInitializedFn(this)(`addHook(${name})`)
if (type) {
this.hooks[name] = this.hooks[name] || {}
this.hooks[name][type] = hook
} else {
this.hooks[name] = this.hooks[name] || []
this.hooks[name].push(hook)
}
return this
}
async init () {
throwIf.alreadyInitializedFn('Already initialized!')
baseLogger.init(this._logger, this.options)
this.logger = baseLogger
this.use(defaultStack)
if (!this.hooks.resolve || this.hooks.resolve.length === 0) {
this.addHook('resolve', (key, data, options) => {
const { ns, lng } = this.extractFromKey(typeof key === 'string' ? key : key[key.length - 1], options)
return deepFind((data && data[lng] && data[lng][ns]) || {}, key)
})
}
Object.keys(this.hooks).forEach((ah) => {
if (this.hookNames.indexOf(ah) < 0) throw new Error(`Hook ${ah} is not a valid hook name!`)
})
const { sync: syncOptions, async: asyncOptions } = runAsyncLater(this.hooks.extendOptions, [{ ...this.options }])
syncOptions.forEach((opt) => {
this.options = deepExtend(opt, this.options)
})
if (asyncOptions.length > 0) {
if (this.options.initImmediate === false) {
const msg = 'You set initImmediate to false but are using an asynchronous extendOptions hook'
this.logger.error(msg)
throw new Error(msg)
}
(await Promise.all(asyncOptions)).forEach((opt) => {
this.options = deepExtend(opt, this.options)
})
}
const { async: asyncInit } = runAsyncLater(this.hooks.initializing, [this.options])
if (asyncInit.length > 0) {
if (this.options.initImmediate === false) {
const msg = 'You set initImmediate to false but are using an asynchronous initializing hook'
this.logger.error(msg)
throw new Error(msg)
}
await Promise.all(asyncInit)
}
this.language = this.options.lng
if (!this.language && this.options.fallbackLng) this.languages = [this.options.fallbackLng]
const hasLanguageDetection = this.hooks.detectLanguage && this.hooks.detectLanguage.length > 0
if (this.options.fallbackLng && !this.language && !hasLanguageDetection) {
const codes = this.getFallbackCodes(this.options.fallbackLng)
if (codes.length > 0 && codes[0] !== 'dev') this.language = codes[0]
}
const { sync: syncRes, async: asyncRes } = runAsyncLater(this.hooks.loadResources, [this.options])
let resources = syncRes.reduce((prev, curr) => ({ ...prev, ...curr }), {})
if (asyncRes.length > 0) {
if (this.options.initImmediate === false) {
const msg = 'You set initImmediate to false but are using an asynchronous loadResources hook'
this.logger.error(msg)
throw new Error(msg)
}
resources = (await Promise.all(asyncRes)).reduce((prev, curr) => ({ ...prev, ...curr }), resources)
}
if (Object.keys(resources).length > 0) {
this.store.setData(resources)
this.logger.log('set data', resources)
}
if (this.language) this.languages = this.toResolveHierarchy(this.language)
if (this.language && this.options.preload.indexOf(this.language) < 0) this.options.preload.unshift(this.language)
this.isInitialized = true
if (this.options.preload.length > 0 && this.hooks.read && this.hooks.read.length > 0) {
const toLoad = this.options.preload.reduce((prev, curr) => {
prev[curr] = this.store.getSeenNamespaces()
return prev
}, {})
for (const hook of this.hooks.read) {
let read = hook(toLoad)
if (read && typeof read.then === 'function') {
if (this.options.initImmediate === false) {
const msg = `You set initImmediate to false but are using an asynchronous read hook (${this.hooks.read.indexOf(hook) + 1}. hook)`
this.logger.error(msg, hook.toString())
throw new Error(msg)
} else {
read = await read
}
}
if (!read) continue
Object.keys(read).forEach((lng) => {
Object.keys(read[lng]).forEach((ns) => {
this.store.addResourceBundle(lng, ns, read[lng][ns])
this.logger.log(`loaded namespace ${ns} for language ${lng}`, read[lng][ns])
})
})
this.emit('loaded', toLoad)
}
}
setResolvedLanguage(this, this.language)
if (!this.language && !hasLanguageDetection) this.logger.warn('init: no lng is defined and no languageDetector is used')
const hasLanguageCaching = this.hooks.cacheLanguage && this.hooks.cacheLanguage.length > 0
if (this.options.initImmediate === false || (!hasLanguageDetection && !hasLanguageCaching)) this.changeLanguage(this.language)
else await this.changeLanguage(this.language)
if (!this.options.isClone) this.logger.log('initialized', this.options)
this.emit('initialized', this)
return this
}
async load (toLoad, tried = 0, delay = 350) {
throwIf.notInitializedFn(this)('load', true)
if (!this.hooks.read) return
Object.keys(toLoad).forEach((lng) => {
toLoad[lng].forEach((ns) => {
this.loading[lng] = this.loading[lng] || []
if (this.loading[lng].indexOf(ns) > -1) {
if (toLoad[lng]) {
toLoad[lng].splice(toLoad[lng].indexOf(ns), 1)
if (toLoad[lng].length === 0) delete toLoad[lng]
}
this.logger.log(`already loading namespace "${ns}" for language "${lng}", so will ignore the request to load again`)
} else {
this.loading[lng].push(ns)
}
})
})
if (Object.keys(toLoad).length === 0) return
for (const hook of this.hooks.read) {
let read
let shouldRetry = false
try {
const ret = hook(toLoad)
read = await (ret && typeof ret.then === 'function' ? ret : Promise.resolve(ret))
} catch (err) {
if (!err.retry || tried > 5) {
Object.keys(toLoad).forEach((lng) => {
toLoad[lng].forEach((ns) => {
this.store.addResourceBundle(lng, ns, {}, { silent: true })
})
})
throw err
}
shouldRetry = true
} finally {
Object.keys(toLoad).forEach((lng) => {
toLoad[lng].forEach((ns) => {
this.loading[lng] = this.loading[lng] || []
if (this.loading[lng].indexOf(ns) > -1) this.loading[lng].splice(this.loading[lng].indexOf(ns), 1)
})
})
}
if (shouldRetry) {
await wait(delay)
return this.load(toLoad, tried + 1, delay * 2)
}
if (!read) continue
Object.keys(read).forEach((lng) => {
Object.keys(read[lng]).forEach((ns) => {
this.store.addResourceBundle(lng, ns, read[lng][ns])
this.logger.log(`loaded namespace "${ns}" for language "${lng}"`, read[lng][ns])
})
})
this.emit('loaded', toLoad)
return
}
}
async loadLanguages (lngs) {
throwIf.notInitializedFn(this)('loadLanguages', true)
if (typeof lngs === 'string') lngs = [lngs]
const newLngs = lngs.filter((lng) => typeof lng === 'string' && this.options.preload.indexOf(lng) < 0)
// Exit early if all given languages are already preloaded
if (!newLngs.length) return
this.options.preload = this.options.preload.concat(newLngs)
const ns = this.store.getSeenNamespaces()
const toLoad = newLngs.reduce((prev, curr) => {
prev[curr] = ns
return prev
}, {})
try {
await this.load(toLoad)
} catch (err) {
const nsPart = ns.length === 1 ? `loading namespace "${ns[0]}"` : `loading namespaces "${ns.join(',')}"`
const lngPart = lngs.length === 1 ? `for language "${lngs[0]}"` : `for languages "${lngs.join(',')}"`
this.logger.warn(`${nsPart} ${lngPart}`, err)
// throw err
}
}
async loadLanguage (lng) {
return this.loadLanguages(lng)
}
async loadNamespaces (ns, lng) {
throwIf.notInitializedFn(this)('loadNamespaces', true)
if (typeof ns === 'string') ns = [ns]
if (lng && typeof lng !== 'string') lng = this.language
if (!lng) lng = this.language
let toLoad
if (lng) {
toLoad = {
[lng]: ns
}
const lngs = this.toResolveHierarchy(lng)
lngs.forEach(l => {
if (!toLoad[l]) toLoad[l] = ns
})
} else {
// at least load fallbacks in this case
const fallbacks = this.getFallbackCodes(this.options.fallbackLng)
toLoad = fallbacks.reduce((prev, curr) => {
prev[curr] = ns
return prev
}, {})
}
try {
await this.load(toLoad)
} catch (err) {
const nsPart = ns.length === 1 ? `loading namespace "${ns[0]}"` : `loading namespaces "${ns.join(',')}"`
this.logger.warn(`${nsPart} for language "${lng}" failed`, err)
// throw err
}
}
async loadNamespace (ns, lng) {
return this.loadNamespaces(ns, lng)
}
isLanguageLoaded (lng) {
// we're in cimode so this shall pass
if (!lng && this.language === 'cimode') lng = this.language
if (lng && lng.toLowerCase() === 'cimode') return true
if (!this.hooks.read || this.hooks.read.length === 0) return true
return this.store.hasResourceBundle(lng)
}
isNamespaceLoaded (ns, lng) {
// we're in cimode so this shall pass
if (!lng && this.language === 'cimode') lng = this.language
if (lng && lng.toLowerCase() === 'cimode') return true
if (!this.hooks.read || this.hooks.read.length === 0) return true
return this.store.hasResourceBundle(lng, ns)
}
dir (lng) {
if (!lng) lng = this.resolvedLanguage || (this.languages && this.languages.length > 0 ? this.languages[0] : this.language)
if (!lng) return 'rtl'
const rtlLngs = [
'ar',
'shu',
'sqr',
'ssh',
'xaa',
'yhd',
'yud',
'aao',
'abh',
'abv',
'acm',
'acq',
'acw',
'acx',
'acy',
'adf',
'ads',
'aeb',
'aec',
'afb',
'ajp',
'apc',
'apd',
'arb',
'arq',
'ars',
'ary',
'arz',
'auz',
'avl',
'ayh',
'ayl',
'ayn',
'ayp',
'bbz',
'pga',
'he',
'iw',
'ps',
'pbt',
'pbu',
'pst',
'prp',
'prd',
'ug',
'ur',
'ydd',
'yds',
'yih',
'ji',
'yi',
'hbo',
'men',
'xmn',
'fa',
'jpr',
'peo',
'pes',
'prs',
'dv',
'sam',
'ckb'
]
return rtlLngs.find((l) => lng.toLowerCase().indexOf(l) > -1) > -1 || lng.toLowerCase().indexOf('-arab') > 1
? 'rtl'
: 'ltr'
}
getBestMatchFromCodes (lngs) {
return run(this).bestMatchFromCodes(lngs)
}
getFallbackCodes (fallbackLng, lng) {
return run(this).fallbackCodes(fallbackLng, lng)
}
toResolveHierarchy (lng, fallbackLng) {
return run(this).resolveHierarchy(lng, fallbackLng)
}
async cacheLanguage (lng) {
return run(this).cacheLanguage(lng)
}
async detectLanguage (...args) {
return run(this).detectLanguage.apply(this, args)
}
async changeLanguage (lng) {
throwIf.notInitializedFn(this)('changeLanguage')
if (!lng) lng = await this.detectLanguage()
if (typeof lng !== 'string' && !Array.isArray(lng)) lng = undefined
lng = typeof lng === 'string' ? lng : this.getBestMatchFromCodes(lng)
if (!lng) return
if (lng === this.language) return
this.emit('languageChanging', lng)
await this.loadLanguage(lng)
this.language = lng
this.languages = this.toResolveHierarchy(this.language)
setResolvedLanguage(this, lng)
await this.cacheLanguage(this.language)
this.emit('languageChanged', lng)
this.logger.log('languageChanged', lng)
}
setDefaultNamespace (ns) {
this.options.defaultNS = ns
}
extractFromKey (key, options = {}) {
let namespaces = options.ns || this.options.defaultNS
const nsSeparator = options.nsSeparator !== undefined ? options.nsSeparator : this.options.nsSeparator
const keySeparator = options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator
const wouldCheckForNsInKey = nsSeparator && key.indexOf(nsSeparator) > -1
const seemsNaturalLanguage =
!this.options.userDefinedKeySeparator &&
!options.keySeparator &&
!this.options.userDefinedNsSeparator &&
!options.nsSeparator &&
!looksLikeObjectPath(key, nsSeparator, keySeparator)
if (wouldCheckForNsInKey && !seemsNaturalLanguage) {
const parts = key.split(nsSeparator)
if (nsSeparator !== keySeparator || (nsSeparator === keySeparator && this.options.ns && this.options.ns.indexOf(parts[0]) > -1)) {
namespaces = parts.shift()
}
key = parts.join(keySeparator || '')
}
if (typeof namespaces === 'string') namespaces = [namespaces]
const lng = options.lng || this.language
return { key, namespaces, ns: namespaces[namespaces.length - 1], lng }
}
exists (key, options = {}) {
if (key === undefined || key === null) return false
return !!run(this).resolve(key, options)
}
t (key, options = {}) {
throwIf.notInitializedFn(this)('t', true)
if (this.options.overloadTranslationOptionHandler) {
options = { ...options, ...this.options.overloadTranslationOptionHandler(arguments) }
}
// non valid keys handling
if (key === undefined || key === null) {
this.logger.warn(`Key "${key}" not valid!`)
return
}
// get namespace(s)
const { key: k, ns, lng } = this.extractFromKey(typeof key === 'string' ? key : key[key.length - 1], options)
// if (!lng) throw new Error('There is no language defined!')
// if (!ns) throw new Error('There is no namespace defined!')
// if (!this.isLanguageLoaded(lng)) {
// this.logger.warn(`Language "${lng}" not loaded!`)
// return
// }
// if (!this.isNamespaceLoaded(ns, lng)) {
// this.logger.warn(`Namespace "${ns}" for language "${lng}" not loaded!`)
// return
// }
// return key on CIMode
const appendNamespaceToCIMode = options.appendNamespaceToCIMode || this.options.appendNamespaceToCIMode
if (lng && lng.toLowerCase() === 'cimode') {
const nsSeparator = options.nsSeparator || this.options.nsSeparator
if (appendNamespaceToCIMode && nsSeparator) return ns + nsSeparator + k
return k
}
let resolved = run(this).resolve(key, options)
if (typeof resolved !== 'object') resolved = { res: resolved, usedKey: key, exactUsedKey: key, usedLng: lng }
return run(this).translated(resolved.res, key, resolved, options)
}
getFixedT (lng, ns) {
const fixedT = (key, opts, ...rest) => {
let options
if (typeof opts !== 'object') {
options = this.options.overloadTranslationOptionHandler([key, opts].concat(rest))
} else {
options = { ...opts }
}
options.lng = options.lng || fixedT.lng
options.lngs = options.lngs || fixedT.lngs
options.ns = options.ns || fixedT.ns
return this.t(key, options)
}
if (typeof lng === 'string') {
fixedT.lng = lng
} else {
fixedT.lngs = lng
}
fixedT.ns = ns
return fixedT
}
clone (options = {}) {
const mergedOptions = { ...this.options, ...options, ...{ isClone: true } }
mergedOptions.lng = mergedOptions.lng || this.language
const clone = new I18next(mergedOptions)
const membersToCopy = ['store', 'languages']
membersToCopy.forEach((m) => {
clone[m] = this[m]
})
if (mergedOptions.initImmediate === false) {
clone.init()
return clone
} else {
return clone.init()
}
}
toJSON () {
return {
options: this.options,
store: this.store,
language: this.language,
languages: this.languages,
resolvedLanguage: this.resolvedLanguage
}
}
}
export default function (options) {
return new I18next(options)
}