Files
Code-Create/src/lang/index.ts
X1627315083@163.com 0bc1bd79ed fix
2026-05-19 09:57:24 +08:00

41 lines
1.0 KiB
TypeScript

import { createI18n } from 'vue-i18n'
import enLocale from './en'
import zhCnLocale from './zh-cn'
import zhTwLocale from './zh-tw'
export const LangType = {
en: "en", // 英文
zhTw: "zh-tw", // 繁体中文
zhCn: "zh-cn", // 简体中文
}
export const LANGS = Object.values(LangType)
// 语言配置整合
const messages = {
[LangType.en]: enLocale,
[LangType.zhCn]: zhCnLocale,
[LangType.zhTw]: zhTwLocale,
}
const localeLang = localStorage.getItem('language')
const defaultLocale = checkLocale(localeLang) ? localeLang! : LangType.en
// 创建 i18n
const i18n = createI18n({
legacy: false,
globalInjection: true, // 全局模式,可以直接使用 $t
locale: defaultLocale,
messages: messages
})
export default i18n
// 检查语言是否存在
function checkLocale(lang: any) {
return LANGS.includes(lang)
}
export const setLang = (lang: any) => {
if (lang === "") lang = LangType.en
if (!checkLocale(lang)) return false
i18n.global.locale.value = lang
localStorage.setItem('language', lang)
return true
}