maxkb/ui/src/locales/useLocale.ts
tongque a50e356f42 feat(i18n): initialize vue-i18n for internationalization support
- Added vue-i18n as a dependency.
- Configured vue-i18n in the main application file.
- Created initial locale files with translations.
2024-04-27 17:24:53 +08:00

29 lines
799 B
TypeScript

import { useLocalStorage } from '@vueuse/core';
import { computed } from 'vue';
import { useI18n } from 'vue-i18n';
import { i18n, langCode, localeConfigKey } from '@/locales/index';
export function useLocale() {
const { locale } = useI18n({ useScope: 'global' });
function changeLocale(lang: string) {
// 如果切换的语言不在对应语言文件里则默认为简体中文
if (!langCode.includes(lang)) {
lang = 'zh_CN';
}
locale.value = lang;
useLocalStorage(localeConfigKey, 'zh_CN').value = lang;
}
const getComponentsLocale = computed(() => {
return i18n.global.getLocaleMessage(locale.value).componentsLocale;
});
return {
changeLocale,
getComponentsLocale,
locale,
};
}