diff --git a/src/components/LangSelect/index.vue b/src/components/LangSelect/index.vue index 298e5dd..a30a2f6 100644 --- a/src/components/LangSelect/index.vue +++ b/src/components/LangSelect/index.vue @@ -5,8 +5,8 @@ @@ -20,14 +20,15 @@ import { useAppStore } from '@/store/modules/app'; const appStore = useAppStore(); const { locale } = useI18n(); -function handleLanguageChange(lang: string) { + +const message: any = { + zh_CN: '切换语言成功!', + en_US: 'Switch Language Successful!', +} +const handleLanguageChange = (lang: string) => { locale.value = lang; appStore.changeLanguage(lang); - if (lang == 'en') { - ElMessage.success('Switch Language Successful!'); - } else { - ElMessage.success('切换语言成功!'); - } + ElMessage.success(message[lang] || '切换语言成功!'); } diff --git a/src/lang/en.ts b/src/lang/en_US.ts similarity index 100% rename from src/lang/en.ts rename to src/lang/en_US.ts diff --git a/src/lang/index.ts b/src/lang/index.ts index 8fdf19e..f8c0801 100644 --- a/src/lang/index.ts +++ b/src/lang/index.ts @@ -2,16 +2,16 @@ import { createI18n } from 'vue-i18n'; // 本地语言包 -import enLocale from './en'; -import zhCnLocale from './zh-cn'; +import enUSLocale from './en_US'; +import zhCNLocale from './zh_CN'; import Cookies from 'js-cookie'; const messages = { - 'zh-cn': { - ...zhCnLocale + zh_CN: { + ...zhCNLocale }, - en: { - ...enLocale + en_US: { + ...enUSLocale } }; @@ -33,7 +33,7 @@ export const getLanguage = () => { return locale; } } - return 'zh-cn'; + return 'zh_CN'; }; const i18n = createI18n({ diff --git a/src/lang/zh-cn.ts b/src/lang/zh_CN.ts similarity index 100% rename from src/lang/zh-cn.ts rename to src/lang/zh_CN.ts diff --git a/src/store/modules/app.ts b/src/store/modules/app.ts index 1fb8337..90a4d0e 100644 --- a/src/store/modules/app.ts +++ b/src/store/modules/app.ts @@ -1,6 +1,6 @@ import Cookies from 'js-cookie'; -import zhCn from 'element-plus/es/locale/lang/zh-cn'; -import en from 'element-plus/es/locale/lang/en'; +import zhCN from 'element-plus/es/locale/lang/zh-cn'; +import enUS from 'element-plus/es/locale/lang/en'; export const useAppStore = defineStore('app', () => { const sidebarStatus = Cookies.get('sidebarStatus'); @@ -11,14 +11,18 @@ export const useAppStore = defineStore('app', () => { }); const device = ref('desktop'); const size = ref(Cookies.get('size') || 'default'); + // 语言 const language = ref(Cookies.get('language')); + const languageObj: any = { + en_US: enUS, + zh_CN: zhCN + }; const locale = computed(() => { - if (language.value == 'en') { - return en; - } else { - return zhCn; + if (!language.value) { + return zhCN; } + return languageObj[language.value]; }); const toggleSideBar = (withoutAnimation?: boolean) => { diff --git a/src/utils/i18n.ts b/src/utils/i18n.ts index 481010a..ab77af0 100644 --- a/src/utils/i18n.ts +++ b/src/utils/i18n.ts @@ -1,8 +1,12 @@ // translate router.meta.title, be used in breadcrumb sidebar tagsview import i18n from '@/lang/index'; -export const translateRouteTitleI18n = (title: string): string => { - // 判断是否存在国际化配置,如果没有原生返回 +/** + * 获取国际化路由,如果不存在则原生返回 + * @param title 路由名称 + * @returns {string} + */ +export const translateRouteTitle = (title: string): string => { const hasKey = i18n.global.te('route.' + title); if (hasKey) { const translatedTitle = i18n.global.t('route.' + title); diff --git a/src/utils/request.ts b/src/utils/request.ts index af73926..d5fac4e 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -7,14 +7,13 @@ import { HttpStatus } from '@/enums/RespEnum'; import { errorCode } from '@/utils/errorCode'; import { LoadingInstance } from 'element-plus/es/components/loading/src/loading'; import FileSaver from 'file-saver'; +import { getLanguage } from '@/lang'; let downloadLoadingInstance: LoadingInstance; // 是否显示重新登录 export const isRelogin = { show: false }; axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'; -// 对应国际化资源文件后缀 -axios.defaults.headers['Content-Language'] = 'zh_CN'; // 创建 axios 实例 const service = axios.create({ baseURL: import.meta.env.VITE_APP_BASE_API, @@ -24,6 +23,9 @@ const service = axios.create({ // 请求拦截器 service.interceptors.request.use( (config: InternalAxiosRequestConfig) => { + // 对应国际化资源文件后缀 + config.headers['Content-Language'] = getLanguage(); + const isToken = (config.headers || {}).isToken === false; // 是否需要防止数据重复提交 const isRepeatSubmit = (config.headers || {}).repeatSubmit === false;