2022-12-29 12:41:59 +00:00
|
|
|
import { nextTick } from 'vue';
|
2022-05-16 19:18:48 +00:00
|
|
|
import { createI18n } from 'vue-i18n';
|
|
|
|
|
2022-06-19 05:31:16 +00:00
|
|
|
import { getUserLanguage } from '~/utils/locale';
|
|
|
|
|
2023-08-03 17:25:12 +00:00
|
|
|
import { loadTimeAgoLocale } from './useTimeAgo';
|
|
|
|
|
2022-12-29 12:41:59 +00:00
|
|
|
const userLanguage = getUserLanguage();
|
|
|
|
const fallbackLocale = 'en';
|
2022-05-16 19:18:48 +00:00
|
|
|
export const i18n = createI18n({
|
2022-12-29 12:41:59 +00:00
|
|
|
locale: userLanguage,
|
2022-05-16 19:18:48 +00:00
|
|
|
legacy: false,
|
|
|
|
globalInjection: true,
|
2022-12-29 12:41:59 +00:00
|
|
|
fallbackLocale,
|
2022-05-16 19:18:48 +00:00
|
|
|
});
|
2022-12-29 12:41:59 +00:00
|
|
|
|
|
|
|
export const loadLocaleMessages = async (locale: string) => {
|
|
|
|
const { default: messages } = await import(`~/assets/locales/${locale}.json`);
|
|
|
|
|
|
|
|
i18n.global.setLocaleMessage(locale, messages);
|
|
|
|
|
2023-08-03 17:25:12 +00:00
|
|
|
loadTimeAgoLocale(locale);
|
|
|
|
|
2022-12-29 12:41:59 +00:00
|
|
|
return nextTick();
|
|
|
|
};
|
|
|
|
|
|
|
|
export const setI18nLanguage = async (lang: string): Promise<void> => {
|
|
|
|
if (!i18n.global.availableLocales.includes(lang)) {
|
|
|
|
await loadLocaleMessages(lang);
|
|
|
|
}
|
|
|
|
i18n.global.locale.value = lang;
|
|
|
|
};
|
|
|
|
|
|
|
|
loadLocaleMessages(fallbackLocale);
|
|
|
|
loadLocaleMessages(userLanguage);
|