44 lines
1.3 KiB
TypeScript
44 lines
1.3 KiB
TypeScript
import { createI18n } from 'vue-i18n';
|
|
|
|
// Start with English synchronously to avoid FOUC; other locales lazy-loaded
|
|
async function loadLocaleMessages(locale: string) {
|
|
const norm = locale.replace('-', '_');
|
|
switch (norm) {
|
|
case 'de':
|
|
return (await import('./de.json')).default;
|
|
case 'es':
|
|
return (await import('./es.json')).default;
|
|
case 'pt_BR':
|
|
case 'pt-br':
|
|
return (await import('./pt_BR.json')).default;
|
|
case 'tr':
|
|
return (await import('./tr.json')).default;
|
|
case 'pl':
|
|
return (await import('./pl.json')).default;
|
|
default:
|
|
return (await import('./en.json')).default;
|
|
}
|
|
}
|
|
|
|
export const i18n = createI18n({
|
|
legacy: false,
|
|
locale: 'en',
|
|
fallbackLocale: 'en',
|
|
messages: {},
|
|
});
|
|
|
|
export async function initI18n(startLocale: string) {
|
|
const msgs = await loadLocaleMessages(startLocale || 'en');
|
|
i18n.global.setLocaleMessage(startLocale || 'en', msgs);
|
|
i18n.global.locale.value = startLocale || 'en';
|
|
}
|
|
|
|
export async function setLocale(locale: string) {
|
|
const norm = locale.replace('-', '_');
|
|
if (!i18n.global.getLocaleMessage(norm) || Object.keys(i18n.global.getLocaleMessage(norm)).length === 0) {
|
|
const msgs = await loadLocaleMessages(norm);
|
|
i18n.global.setLocaleMessage(norm, msgs);
|
|
}
|
|
i18n.global.locale.value = norm;
|
|
}
|