安装
官网:https://vue-i18n.intlify.dev/api/general.html
pnpm add vue-i18n@9
使用
| |
| |
| import appConfig from "@/configure/app.config.ts"; |
| import {nextTick} from 'vue' |
| import type {Ref} from 'vue' |
| import {createI18n} from 'vue-i18n' |
| import type {I18n, Locale} from 'vue-i18n' |
| |
| |
| const DEFAULT_LANG: string = appConfig.defaultLanguage; |
| |
| const i18n = createI18n({ |
| legacy: false, |
| locale: DEFAULT_LANG, |
| messages: {}, |
| fallbackLocale: DEFAULT_LANG, |
| globalInjection: true, |
| }); |
| |
| const loadLocaleMessages = async (i18n: I18n, locale: Locale) => { |
| const messages = await import(`./lang/${locale}/index.ts`) |
| i18n.global.setLocaleMessage(locale, messages.default) |
| |
| if (i18n.mode === "legacy") { |
| i18n.global.locale = locale; |
| } else { |
| (i18n.global.locale as Ref<Locale>).value = locale; |
| } |
| document.querySelector('html').setAttribute('lang', locale) |
| |
| return nextTick(); |
| } |
| |
| |
| await loadLocaleMessages(i18n, DEFAULT_LANG) |
| |
| export default i18n; |
| |
| |
| const menu = { |
| "account": "用户菜单", |
| } |
| |
| export default { |
| ...menu |
| } |
| |
| |
| import i18n from "@/locale" |
| |
| |
| app.use(i18n) |
| |
| |
| <template> |
| <h1>{{ t('account') }}</h1> |
| <template> |
| |
| <script setup lang="ts"> |
| import {useI18n} from "vue-i18n"; |
| |
| const {t} = useI18n() |
| </script> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步