blob: 65c7d1fe3cbf246ebbd41ece6d92fce50c61a6ef [file] [log] [blame]
Ed Tanous7d6b44c2024-03-23 14:56:34 -07001import { createI18n } from 'vue-i18n';
jason westoverd2483622025-08-18 09:26:41 -05002import { deepMerge } from './utilities/objectUtils';
Dixsie Wolmerscbcd2132020-01-30 20:58:37 -06003
jason westoverd2483622025-08-18 09:26:41 -05004export function loadBaseLocaleMessages() {
5 const context = require.context(
6 './locales',
7 true,
8 /[A-Za-z0-9-_,\s]+\.json$/i,
9 );
10 const messages = {};
11 context.keys().forEach((key) => {
12 const match = key.match(/([A-Za-z0-9-_]+)\.json$/i);
13 if (!match) return;
14 const locale = match[1];
15 const mod = context(key);
16 messages[locale] = mod && mod.default ? mod.default : mod;
17 });
Dixsie Wolmerscbcd2132020-01-30 20:58:37 -060018 return messages;
19}
20
jason westoverd2483622025-08-18 09:26:41 -050021export function loadEnvLocaleMessages(envName) {
22 if (!envName) return {};
23 const envMessages = {};
24 const envLocales = require.context(
25 './env/locales',
26 true,
27 /[A-Za-z0-9-_,\s]+\.json$/i,
28 );
29 const vendorRoot = String(envName).split('-')[0];
30 const candidates =
31 vendorRoot && vendorRoot !== envName ? [vendorRoot, envName] : [envName];
32 candidates.forEach((candidate) => {
33 envLocales.keys().forEach((key) => {
34 if (!key.includes(`/${candidate}/`)) return;
35 const localeMatch = key.match(/([A-Za-z0-9-_]+)\.json$/i);
36 if (!localeMatch) return;
37 const locale = localeMatch[1];
38 const mod = envLocales(key);
39 const bundle = mod && mod.default ? mod.default : mod;
40 envMessages[locale] = deepMerge(envMessages[locale] || {}, bundle);
41 });
42 });
43 return envMessages;
44}
Ed Tanous7d6b44c2024-03-23 14:56:34 -070045
jason westoverd2483622025-08-18 09:26:41 -050046export function createI18nInstance(
47 envName,
48 locale,
49 loadEnv = loadEnvLocaleMessages,
50 loadBase = loadBaseLocaleMessages,
51) {
52 const base = loadBase();
53 const env = loadEnv(envName);
54 const messages = { ...base };
55 Object.keys(env).forEach((loc) => {
56 messages[loc] = deepMerge(base[loc] || {}, env[loc]);
57 });
58
59 const addAlias = (alias, target) => {
60 if (!messages[alias] && messages[target])
61 messages[alias] = messages[target];
62 };
63 addAlias('en', 'en-US');
64 addAlias('ru', 'ru-RU');
65 addAlias('zh', 'zh-CN');
66 addAlias('ka', 'ka-GE');
67
68 const normalize = (val) => {
69 if (!val) return undefined;
70 const s = String(val);
71 if (s === 'en') return 'en-US';
72 if (s === 'ru') return 'ru-RU';
73 if (s === 'zh') return 'zh-CN';
74 if (s === 'ka') return 'ka-GE';
75 return s;
76 };
77
78 return createI18n({
79 locale: normalize(locale),
80 // Locales that don't exist will fallback to English
81 fallbackLocale: 'en-US',
82 // Falling back to fallbackLocale generates two console warnings
83 // Silent fallback suppresses console warnings when using fallback
84 silentFallbackWarn: true,
85 messages,
86 globalInjection: false,
87 legacy: false,
88 });
89}
90
91const envName = process.env.VUE_APP_ENV_NAME;
92// Get default locale from local storage
93const stored = window.localStorage.getItem('storedLanguage');
94export default createI18nInstance(envName, stored);