30 lines
671 B
TypeScript
30 lines
671 B
TypeScript
![]() |
import { createI18n } from 'vue-i18n'
|
||
|
import zh from './locales/zh.json'
|
||
|
import en from './locales/en.json'
|
||
|
|
||
|
// 获取浏览器语言或从localStorage获取保存的语言
|
||
|
const getDefaultLocale = (): string => {
|
||
|
const savedLocale = localStorage.getItem('locale')
|
||
|
if (savedLocale) {
|
||
|
return savedLocale
|
||
|
}
|
||
|
|
||
|
const browserLocale = navigator.language.toLowerCase()
|
||
|
if (browserLocale.startsWith('zh')) {
|
||
|
return 'zh'
|
||
|
}
|
||
|
return 'en'
|
||
|
}
|
||
|
|
||
|
const i18n = createI18n({
|
||
|
legacy: false, // 使用 Composition API 模式
|
||
|
locale: getDefaultLocale(), // 默认语言
|
||
|
fallbackLocale: 'zh', // 回退语言
|
||
|
messages: {
|
||
|
zh,
|
||
|
en
|
||
|
}
|
||
|
})
|
||
|
|
||
|
export default i18n
|