Files
loan/src/lang/index.ts
2024-03-09 18:27:20 +08:00

55 lines
1.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { createI18n } from 'vue-i18n';
import {Locale} from 'vant'
import zhCN from 'vant/lib/locale/lang/zh-CN'
import idId from 'vant/lib/locale/lang/id-ID'
import hiIN from 'vant/lib/locale/lang/hi-IN'
import zhLocale from './zh_cn'
import idLocale from './id_ID'
import hiLocale from './hi-IN'
import type { App } from 'vue';
const messages = {
zh: {
...zhCN,
...zhLocale
},
id: {
...idId,
...idLocale
},
hi: {
...hiIN,
...hiLocale
}
}
const language = (navigator.language || 'zh').toLocaleLowerCase(); // 这是获取浏览器的语言
const defLanguage = localStorage.getItem('lang'); // 这是获取浏览器的语言
console.log('当前的环境语言是:', language)
console.log('当前的后台语言是:', localStorage.getItem('lang'))
const i18n = createI18n({
allowComposition: true,
legacy: false,
locale: defLanguage || language.split('-')[0] || 'zh', // 设置默认语言
fallbackLocale: 'zh', // 设置备用语言
messages: messages // 设置资源文件对象
})
// 更新vant组件库本身的语言变化支持国际化
function vantLocales (app: App<Element>) {
app.use(i18n)
const lan = defLanguage || language.split('-')[0] || 'zh'
if (lan === 'hi') {
Locale.use(lan, hiIN)
console.log('========================', lan)
} else if (lan === 'id') {
Locale.use(lan, idId)
} else {
Locale.use(lan, zhCN)
}
}
export {i18n, vantLocales}