| | |
| | | /** |
| | | * Multi-language related operations |
| | | */ |
| | | import type { LocaleType } from '/@/locales/types'; |
| | | import type { Ref } from 'vue'; |
| | | |
| | | import { unref, ref } from 'vue'; |
| | | import { useLocaleSetting } from '/@/hooks/setting/useLocaleSetting'; |
| | | |
| | | import { dateUtil } from '/@/utils/dateUtil'; |
| | | import type { LocaleType } from '/#/config'; |
| | | |
| | | import { i18n } from './setupI18n'; |
| | | import { useLocaleStoreWithOut } from '/@/store/modules/locale'; |
| | | import { unref, computed } from 'vue'; |
| | | import { loadLocalePool, setHtmlPageLang } from './helper'; |
| | | |
| | | const antConfigLocaleRef = ref<any>(null); |
| | | interface LangModule { |
| | | message: Recordable; |
| | | dateLocale: Recordable; |
| | | dateLocaleName: string; |
| | | } |
| | | |
| | | function setI18nLanguage(locale: LocaleType) { |
| | | const localeStore = useLocaleStoreWithOut(); |
| | | |
| | | if (i18n.mode === 'legacy') { |
| | | i18n.global.locale = locale; |
| | | } else { |
| | | (i18n.global.locale as any).value = locale; |
| | | } |
| | | localeStore.setLocaleInfo({ locale }); |
| | | setHtmlPageLang(locale); |
| | | } |
| | | |
| | | export function useLocale() { |
| | | const { getLang, getLocale, setLocale: setLocalSetting } = useLocaleSetting(); |
| | | const localeStore = useLocaleStoreWithOut(); |
| | | const getLocale = computed(() => localeStore.getLocale); |
| | | const getShowLocalePicker = computed(() => localeStore.getShowPicker); |
| | | |
| | | const getAntdLocale = computed((): any => { |
| | | return i18n.global.getLocaleMessage(unref(getLocale))?.antdLocale ?? {}; |
| | | }); |
| | | |
| | | // Switching the language will change the locale of useI18n |
| | | // And submit to configuration modification |
| | | function changeLocale(lang: LocaleType): void { |
| | | if (i18n.mode === 'legacy') { |
| | | i18n.global.locale = lang; |
| | | } else { |
| | | ((i18n.global.locale as unknown) as Ref<string>).value = lang; |
| | | async function changeLocale(locale: LocaleType) { |
| | | const globalI18n = i18n.global; |
| | | const currentLocale = unref(globalI18n.locale); |
| | | if (currentLocale === locale) { |
| | | return locale; |
| | | } |
| | | setLocalSetting({ lang }); |
| | | // i18n.global.setLocaleMessage(locale, messages); |
| | | |
| | | switch (lang) { |
| | | // Simplified Chinese |
| | | case 'zh_CN': |
| | | import('ant-design-vue/es/locale/zh_CN').then((locale) => { |
| | | antConfigLocaleRef.value = locale.default; |
| | | }); |
| | | |
| | | dateUtil.locale('cn'); |
| | | break; |
| | | // English |
| | | case 'en': |
| | | import('ant-design-vue/es/locale/en_US').then((locale) => { |
| | | antConfigLocaleRef.value = locale.default; |
| | | }); |
| | | dateUtil.locale('en-us'); |
| | | break; |
| | | |
| | | // other |
| | | default: |
| | | break; |
| | | if (loadLocalePool.includes(locale)) { |
| | | setI18nLanguage(locale); |
| | | return locale; |
| | | } |
| | | } |
| | | const langModule = ((await import(`./lang/${locale}.ts`)) as any).default as LangModule; |
| | | if (!langModule) return; |
| | | |
| | | // initialization |
| | | function setLocale() { |
| | | const lang = unref(getLang); |
| | | lang && changeLocale(lang); |
| | | const { message } = langModule; |
| | | |
| | | globalI18n.setLocaleMessage(locale, message); |
| | | loadLocalePool.push(locale); |
| | | |
| | | setI18nLanguage(locale); |
| | | return locale; |
| | | } |
| | | |
| | | return { |
| | | setLocale, |
| | | getLocale, |
| | | getLang, |
| | | getShowLocalePicker, |
| | | changeLocale, |
| | | antConfigLocale: antConfigLocaleRef, |
| | | getAntdLocale, |
| | | }; |
| | | } |