16.vue i18n
2024/10/7小于 1 分钟
16.vue i18n
16.1 vue-i18n
- vue的国际化插件vue-i18n
- 配置
import { createI18n } from "vue-i18n"
// 读取所有./lang中的json文件作为语言文件
function loadLocaleMessages() {
const locales = import.meta.glob('./lang/*.json', { eager: true })
const messages: {[key:string]:any} = {}
for (const [key, value] of Object.entries(locales)) {
const matched = key.match(/([A-Za-z0-9-_]+)\./i)
if (matched && matched.length > 1) {
const locale = matched[1]
messages[locale] = value
}
}
return messages
}
const messages = loadLocaleMessages()
const i18n = createI18n({
globalInjection: true, //全局生效$t
allowComposition: true,
legacy: false,
locale: 'en',
messages,
})
export default i18n;- 在入口文件中用
app.use装载配置好的i18n插件对象
