-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
i18n.js
95 lines (93 loc) · 2.69 KB
/
i18n.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
const i18nReady = new ReactiveVar(false)
let _i18n = {}
let _debug = true
let _lang = ''
const weekDaysMin = new ReactiveVar([])
const months = new ReactiveVar([])
const setup = (language, lang, debugMode) => {
_i18n = language
_debug = debugMode
_lang = lang
if (_debug) {
console.log(`Loading i18n for language ${lang}`)
}
}
const getLanguage = () => _lang
const t = (key) => {
if (!key) {
return false
}
const keys = key.split('.')
let translatedValue = _i18n
keys.forEach((innerKey) => {
translatedValue = translatedValue[innerKey] !== undefined ? translatedValue[innerKey] : key
})
if (translatedValue === undefined || translatedValue === key) {
if (_debug) {
console.warn(`Translation for key ${key} not found.`)
}
translatedValue = key
}
return translatedValue
}
let fallbackLocale = {}
import(`dayjs/locale/en-gb`).then((locale) => {
fallbackLocale = locale
})
const loadLanguage = (language, i18nextDebugMode) => {
// Meteor.js is not very smart
// eslint-disable-next no-constant-condition
if (false) {
import('../ui/translations/en.json')
import('dayjs/locale/en')
import('../ui/translations/de.json')
import('dayjs/locale/de')
import('../ui/translations/fr.json')
import('dayjs/locale/fr')
import('../ui/translations/zh.json')
import('dayjs/locale/zh')
import('../ui/translations/ru.json')
import('dayjs/locale/ru')
import('../ui/translations/uk.json')
import('dayjs/locale/uk')
import('../ui/translations/es.json')
import('dayjs/locale/es')
}
import(`/imports/ui/translations/${language}.json`).then((lang) => {
i18nReady.set(false)
setup(lang.default, language, i18nextDebugMode)
import(`dayjs/locale/${language}`).then((locale) => {
if (!locale.weekdaysMin) {
weekDaysMin.set(fallbackLocale.weekdaysMin)
} else {
weekDaysMin.set(locale.weekdaysMin)
}
months.set(locale.months)
import('dayjs').then((dayjs) => {
dayjs.locale(language)
})
})
i18nReady.set(true)
$('html').attr('lang', language)
}).catch(() => {
import('../ui/translations/en.json').then((lang) => {
if (i18nextDebugMode) {
console.warn('Language not found, using default language en')
}
i18nReady.set(false)
setup(lang.default, 'en', i18nextDebugMode)
import('dayjs/locale/en-gb').then((locale) => {
weekDaysMin.set(locale.weekdaysMin)
months.set(locale.months)
import('dayjs').then((dayjs) => {
dayjs.locale('en')
})
})
i18nReady.set(true)
$('html').attr('lang', 'en')
})
})
}
export {
t, setup, getLanguage, i18nReady, loadLanguage, weekDaysMin, months,
}