# i18n **Repository Path**: xxxxasd/i18n ## Basic Information - **Project Name**: i18n - **Description**: 国际化 i18n 在vue 中的使用 - **Primary Language**: JavaScript - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-04-22 - **Last Updated**: 2025-04-22 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Vue 中使用 vue-i18n@9.11.1 的完整指南 vue-i18n 是 Vue.js 的国际化插件,下面详细介绍如何在 Vue 3 项目中使用 vue-i18n 9.x 版本。 ## 安装与基本配置 ### 1. 安装 ```bash npm install vue-i18n@9.11.1 # 或 yarn add vue-i18n@9.11.1 ``` ### 2. 基本配置 在项目中创建 i18n 实例: ```javascript // src/i18n.js import { createI18n } from 'vue-i18n' // 准备翻译消息 const messages = { en: { message: { hello: 'Hello world' } }, zh: { message: { hello: '你好,世界' } } } // 创建 i18n 实例 const i18n = createI18n({ legacy: false, // 使用 Composition API 模式 locale: 'en', // 默认语言 fallbackLocale: 'en', // 备用语言 messages // 语言包 }) export default i18n ``` ### 3. 在 Vue 应用中启用 ```javascript // src/main.js import { createApp } from 'vue' import App from './App.vue' import i18n from './i18n' const app = createApp(App) app.use(i18n) app.mount('#app') ``` ## 基本使用 ### 1. 在模板中使用 ```vue ``` ### 2. 在组件中使用 #### 选项式 API ```javascript export default { methods: { showMessage() { console.log(this.$t('message.hello')) } } } ``` #### 组合式 API ```vue ``` ## 进阶功能 ### 1. 切换语言 ```vue ``` ### 2. 使用带参数的翻译 ```javascript // i18n.js const messages = { en: { greeting: 'Hello, {name}!' }, zh: { greeting: '你好,{name}!' } } ``` ```vue ``` ### 3. 复数处理 ```javascript // i18n.js const messages = { en: { apple: 'apple | apples', banana: 'no bananas | one banana | {count} bananas' } } ``` ```vue ``` ### 4. 日期和数字格式化 ```javascript // i18n.js const datetimeFormats = { en: { short: { year: 'numeric', month: 'short', day: 'numeric' } }, zh: { short: { year: 'numeric', month: 'short', day: 'numeric' } } } const numberFormats = { en: { currency: { style: 'currency', currency: 'USD' } }, zh: { currency: { style: 'currency', currency: 'CNY' } } } const i18n = createI18n({ // ...其他配置 datetimeFormats, numberFormats }) ``` ```vue ``` ## 项目实践建议 ### 1. 模块化语言文件 ``` src/ locales/ en/ common.json home.json zh/ common.json home.json ``` ```javascript // i18n.js import { createI18n } from 'vue-i18n' import enCommon from './locales/en/common.json' import zhCommon from './locales/zh/common.json' import enHome from './locales/en/home.json' import zhHome from './locales/zh/home.json' const messages = { en: { ...enCommon, ...enHome }, zh: { ...zhCommon, ...zhHome } } // 创建i18n实例... ``` ### 2. 异步加载语言包 ```javascript async function loadLocaleMessages(i18n, locale) { const messages = await import(`./locales/${locale}.json`) i18n.global.setLocaleMessage(locale, messages.default) return nextTick() } // 切换语言时 const changeLang = async (lang) => { if (!i18n.global.availableLocales.includes(lang)) { await loadLocaleMessages(i18n, lang) } locale.value = lang } ``` ### 3. 与路由结合 ```javascript router.beforeEach((to, from, next) => { const lang = to.params.lang || 'en' const i18n = useI18n() if (i18n.locale.value !== lang) { i18n.locale.value = lang } next() }) ``` ## 常见问题解决 1. **警告 "You are running the esm-bundler build of vue-i18n"**: ```javascript // vite.config.js export default defineConfig({ resolve: { alias: { 'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js' } } }) ``` 2. **TypeScript 支持**: ```typescript // src/i18n.d.ts declare module 'vue-i18n' { export interface DefineLocaleMessage { message: { hello: string } } } ``` 3. **SSR 支持**: 需要确保每个请求都有独立的 i18n 实例,避免状态共享问题。 通过以上配置和使用方法,你可以在 Vue 3 项目中高效地实现国际化功能。