2024-05-11 14:48:02 +08:00
|
|
|
|
// 多组件库的国际化和本地项目国际化兼容
|
2024-09-03 08:47:49 +08:00
|
|
|
|
import { createI18n } from "vue-i18n";
|
|
|
|
|
import type { App } from "vue";
|
2024-08-28 10:36:16 +08:00
|
|
|
|
|
2024-09-03 08:47:49 +08:00
|
|
|
|
// ? 从本地存储中获取数据
|
|
|
|
|
const languageData = localStorage.getItem("i18nStore");
|
2024-08-28 10:36:16 +08:00
|
|
|
|
|
2024-09-03 08:47:49 +08:00
|
|
|
|
// 配置多语言
|
|
|
|
|
export const i18n = createI18n({
|
|
|
|
|
// 如果要支持 compositionAPI,此项必须设置为 false
|
2024-08-28 10:36:16 +08:00
|
|
|
|
legacy: false,
|
2024-09-03 08:47:49 +08:00
|
|
|
|
// locale: 'zh',
|
2024-08-28 10:36:16 +08:00
|
|
|
|
fallbackLocale: "en",
|
2024-09-03 08:47:49 +08:00
|
|
|
|
// ? 全局注册$t方法
|
|
|
|
|
globalInjection: true,
|
|
|
|
|
// 本地内容存在时,首次加载如果本地存储没有多语言需要再刷新
|
|
|
|
|
messages: languageData ? JSON.parse(languageData).i18n : {}
|
2024-08-28 10:36:16 +08:00
|
|
|
|
});
|
2024-05-27 20:01:23 +08:00
|
|
|
|
|
2024-09-03 08:47:49 +08:00
|
|
|
|
export const $t: any = (i18n.global as any).t as any;
|
|
|
|
|
|
2024-08-28 10:36:16 +08:00
|
|
|
|
export function useI18n(app: App) {
|
|
|
|
|
app.use(i18n);
|
|
|
|
|
}
|