|
@@ -7,8 +7,11 @@
|
|
// sed -i -e "s/'//gm" -e 's/"/\\"/gm' -re 's/^( +)(.+?): ((.+?))?(,?)(\{?)$/\1"\2": "\4"/gm' -e 's/\"\{\"/{/g' -e 's/,"$/",/g' file.json
|
|
// sed -i -e "s/'//gm" -e 's/"/\\"/gm' -re 's/^( +)(.+?): ((.+?))?(,?)(\{?)$/\1"\2": "\4"/gm' -e 's/\"\{\"/{/g' -e 's/,"$/",/g' file.json
|
|
// There's only problem that apostrophe character ' gets replaced by \\ so you have to fix it manually, sorry.
|
|
// There's only problem that apostrophe character ' gets replaced by \\ so you have to fix it manually, sorry.
|
|
|
|
|
|
|
|
+import { isEqual } from 'lodash'
|
|
import { languages, langCodeToJsonName } from './languages.js'
|
|
import { languages, langCodeToJsonName } from './languages.js'
|
|
|
|
|
|
|
|
+const ULTIMATE_FALLBACK_LOCALE = 'en'
|
|
|
|
+
|
|
const hasLanguageFile = (code) => languages.includes(code)
|
|
const hasLanguageFile = (code) => languages.includes(code)
|
|
|
|
|
|
const loadLanguageFile = (code) => {
|
|
const loadLanguageFile = (code) => {
|
|
@@ -25,11 +28,26 @@ const messages = {
|
|
en: require('./en.json').default
|
|
en: require('./en.json').default
|
|
},
|
|
},
|
|
setLanguage: async (i18n, language) => {
|
|
setLanguage: async (i18n, language) => {
|
|
- if (hasLanguageFile(language)) {
|
|
|
|
- const messages = await loadLanguageFile(language)
|
|
|
|
- i18n.setLocaleMessage(language, messages.default)
|
|
|
|
|
|
+ const languages = (Array.isArray(language) ? language : [language]).filter(k => k)
|
|
|
|
+
|
|
|
|
+ if (!languages.includes(ULTIMATE_FALLBACK_LOCALE)) {
|
|
|
|
+ languages.push(ULTIMATE_FALLBACK_LOCALE)
|
|
|
|
+ }
|
|
|
|
+ const [first, ...rest] = languages
|
|
|
|
+
|
|
|
|
+ if (first === i18n.locale && isEqual(rest, i18n.fallbackLocale)) {
|
|
|
|
+ return
|
|
}
|
|
}
|
|
- i18n.locale = language
|
|
|
|
|
|
+
|
|
|
|
+ for (const lang of languages) {
|
|
|
|
+ if (hasLanguageFile(lang)) {
|
|
|
|
+ const messages = await loadLanguageFile(lang)
|
|
|
|
+ i18n.setLocaleMessage(lang, messages.default)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ i18n.fallbackLocale = rest
|
|
|
|
+ i18n.locale = first
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|