diff options
Diffstat (limited to 'src/i18n.js')
-rw-r--r-- | src/i18n.js | 109 |
1 files changed, 88 insertions, 21 deletions
diff --git a/src/i18n.js b/src/i18n.js index 434ec1048..65c7d1fe3 100644 --- a/src/i18n.js +++ b/src/i18n.js @@ -1,27 +1,94 @@ import { createI18n } from 'vue-i18n'; +import { deepMerge } from './utilities/objectUtils'; -import en_us from './locales/en-US.json'; -import ru_ru from './locales/ru-RU.json'; - -function loadLocaleMessages() { - const messages = { - 'en-US': en_us, - 'ru-RU': ru_ru, - }; +export function loadBaseLocaleMessages() { + const context = require.context( + './locales', + true, + /[A-Za-z0-9-_,\s]+\.json$/i, + ); + const messages = {}; + context.keys().forEach((key) => { + const match = key.match(/([A-Za-z0-9-_]+)\.json$/i); + if (!match) return; + const locale = match[1]; + const mod = context(key); + messages[locale] = mod && mod.default ? mod.default : mod; + }); return messages; } -const i18n = createI18n({ - // Get default locale from local storage - locale: window.localStorage.getItem('storedLanguage'), - // Locales that don't exist will fallback to English - fallbackLocale: 'en-US', - // Falling back to fallbackLocale generates two console warnings - // Silent fallback suppresses console warnings when using fallback - silentFallbackWarn: true, - messages: loadLocaleMessages(), - globalInjection: false, - legacy: false, -}); +export function loadEnvLocaleMessages(envName) { + if (!envName) return {}; + const envMessages = {}; + const envLocales = require.context( + './env/locales', + true, + /[A-Za-z0-9-_,\s]+\.json$/i, + ); + const vendorRoot = String(envName).split('-')[0]; + const candidates = + vendorRoot && vendorRoot !== envName ? [vendorRoot, envName] : [envName]; + candidates.forEach((candidate) => { + envLocales.keys().forEach((key) => { + if (!key.includes(`/${candidate}/`)) return; + const localeMatch = key.match(/([A-Za-z0-9-_]+)\.json$/i); + if (!localeMatch) return; + const locale = localeMatch[1]; + const mod = envLocales(key); + const bundle = mod && mod.default ? mod.default : mod; + envMessages[locale] = deepMerge(envMessages[locale] || {}, bundle); + }); + }); + return envMessages; +} + +export function createI18nInstance( + envName, + locale, + loadEnv = loadEnvLocaleMessages, + loadBase = loadBaseLocaleMessages, +) { + const base = loadBase(); + const env = loadEnv(envName); + const messages = { ...base }; + Object.keys(env).forEach((loc) => { + messages[loc] = deepMerge(base[loc] || {}, env[loc]); + }); + + const addAlias = (alias, target) => { + if (!messages[alias] && messages[target]) + messages[alias] = messages[target]; + }; + addAlias('en', 'en-US'); + addAlias('ru', 'ru-RU'); + addAlias('zh', 'zh-CN'); + addAlias('ka', 'ka-GE'); + + const normalize = (val) => { + if (!val) return undefined; + const s = String(val); + if (s === 'en') return 'en-US'; + if (s === 'ru') return 'ru-RU'; + if (s === 'zh') return 'zh-CN'; + if (s === 'ka') return 'ka-GE'; + return s; + }; + + return createI18n({ + locale: normalize(locale), + // Locales that don't exist will fallback to English + fallbackLocale: 'en-US', + // Falling back to fallbackLocale generates two console warnings + // Silent fallback suppresses console warnings when using fallback + silentFallbackWarn: true, + messages, + globalInjection: false, + legacy: false, + }); +} -export default i18n; +const envName = process.env.VUE_APP_ENV_NAME; +// Get default locale from local storage +const stored = window.localStorage.getItem('storedLanguage'); +export default createI18nInstance(envName, stored); |