国际化 (i18n) 使用指南
📦 核心文件结构
src/
├── hooks/web/
│ └── useI18n.ts # 国际化 Hook
├── locales/
│ ├── setupI18n.ts # i18n 初始化配置
│ ├── helper.ts # 辅助函数
│ ├── useLocale.ts # 语言切换 Hook
│ └── lang/ # 语言文件目录
│ ├── zh_CN.ts # 中文入口
│ ├── en.ts # 英文入口
│ ├── zh-CN/ # 中文翻译文件
│ │ ├── sys.ts # 系统相关
│ │ ├── common.ts # 通用文本
│ │ ├── component.ts # 组件文本
│ │ ├── layout.ts # 布局文本
│ │ └── routes/ # 路由翻译
│ └── en/ # 英文翻译文件(结构同上)
├── settings/
│ └── localeSetting.ts # 语言配置
└── store/modules/
└── locale.ts # 语言状态管理🎯 基本使用方式
1. 在 Vue 组件中使用
<script setup lang="ts">
import { useI18n } from '@/hooks/web/useI18n';
// 不带命名空间
const { t } = useI18n();
// 带命名空间(推荐,避免 key 冲突)
const { t: tSys } = useI18n('sys');
const { t: tLayout } = useI18n('layout');
</script>
<template>
<!-- 基本用法 -->
<div>{{ t('common.okText') }}</div> <!-- 确认 -->
<!-- 带命名空间 -->
<div>{{ tSys('app.logoutTip') }}</div> <!-- 温馨提醒 -->
<div>{{ tLayout('header.home') }}</div> <!-- 首页 -->
</template>2. 在 Pinia Store 中使用
// src/store/modules/user.ts
import { useI18n } from '@/hooks/web/useI18n';
const { t } = useI18n();
// 使用翻译
createConfirm({
title: t('sys.app.logoutTip'), // 温馨提醒
content: t('sys.app.logoutMessage'), // 是否确认退出系统?
});3. 在普通 TypeScript 文件中使用
import { useI18n } from '@/hooks/web/useI18n';
export function someFunction() {
const { t } = useI18n();
const message = t('sys.api.apiRequestFailed');
console.log(message); // 请求出错,请稍候重试
}🔑 翻译 Key 的层级结构
根据 genMessage 函数 (src/locales/helper.ts:15),语言文件会按目录结构生成嵌套的 key:
// 目录结构: src/locales/lang/zh-CN/sys.ts
// 文件内容: { api: { operationFailed: '操作失败' } }
// 访问方式:
t('sys.api.operationFailed') // '操作失败'🌐 支持的语言
当前项目支持两种语言(在 src/settings/localeSetting.ts 定义):
export const LOCALE = {
ZH_CN: 'zh_CN', // 简体中文
EN_US: 'en', // 英文
};🔄 动态切换语言
使用 useLocale Hook 进行语言切换:
import { useLocale } from '@/locales/useLocale';
const { changeLocale, getLocale } = useLocale();
// 切换到英文
await changeLocale('en');
// 切换到中文
await changeLocale('zh_CN');
// 获取当前语言
const currentLang = getLocale.value;💡 最佳实践
1. 使用命名空间避免冲突
// ✅ 推荐 - 使用命名空间
const { t: tLayout } = useI18n('layout');
tLayout('header.home');
// ✅ 也可以 - 完整路径
const { t } = useI18n();
t('layout.header.home');
// ❌ 不推荐 - 没有命名空间容易冲突
const { t } = useI18n();
t('home'); // 可能在多个文件中定义2. 新增翻译的步骤
- 在中文翻译文件中添加 key
// src/locales/lang/zh-CN/sys.ts
export default {
// ...existing code
myFeature: {
title: '我的功能',
description: '这是一个新功能',
},
};- 在英文翻译文件中添加对应的 key
// src/locales/lang/en/sys.ts
export default {
// ...existing code
myFeature: {
title: 'My Feature',
description: 'This is a new feature',
},
};- 在组件中使用
const { t } = useI18n('sys');
const title = t('myFeature.title');3. 格式化参数
// 翻译文件: { sendText: '{0}秒后重新获取' }
t('component.countdown.sendText', ['60']) // '60秒后重新获取'
// 命名参数
// 翻译文件: { greeting: '欢迎 {name}' }
t('sys.greeting', { name: '张三' }) // '欢迎 张三'📝 常用翻译 Key 参考
通用文本 (common)
import { useI18n } from '@/hooks/web/useI18n';
const { t } = useI18n();
t('common.okText') // '确认'
t('common.cancelText') // '取消'
t('common.saveText') // '保存'
t('common.delText') // '删除'
t('common.resetText') // '重置'
t('common.searchText') // '搜索'
t('common.queryText') // '查询'
t('common.inputText') // '请输入'
t('common.chooseText') // '请选择'系统 API (sys.api)
const { t } = useI18n('sys');
t('api.apiRequestFailed') // '请求出错,请稍候重试'
t('api.operationFailed') // '操作失败'
t('api.timeoutMessage') // '登录超时,请重新登录!'
t('api.networkException') // '网络异常'布局相关 (layout)
const { t } = useI18n('layout');
t('header.home') // '首页'
t('header.dropdownItemLoginOut') // '退出系统'
t('multipleTab.reload') // '刷 新'
t('multipleTab.close') // '关闭当前'组件相关 (component)
const { t } = useI18n('component');
t('modal.okText') // '确认'
t('modal.cancelText') // '关闭'
t('table.settingDens') // '密度'
t('upload.uploadSuccess') // '上传成功'🔧 常见问题
Q: 如何添加新的翻译文件?
在 src/locales/lang/zh-CN/ 或 src/locales/lang/en/ 下创建新的 .ts 文件,系统会自动加载。
例如创建 src/locales/lang/zh-CN/business.ts:
export default {
product: {
name: '商品名称',
price: '商品价格',
},
};使用时: t('business.product.name')
Q: 路由和菜单的翻译如何处理?
对于路由和菜单,使用导出的 t 函数(非 useI18n):
import { t } from '@/hooks/web/useI18n';
const route = {
path: '/dashboard',
meta: {
title: t('routes.dashboard.dashboard'),
},
};Q: 如何处理动态内容的翻译?
使用参数化翻译:
// 翻译文件
export default {
message: {
welcome: '欢迎, {name}!',
itemCount: '共 {count} 条数据',
},
};
// 使用
const { t } = useI18n('message');
t('welcome', { name: '张三' }) // '欢迎, 张三!'
t('itemCount', { count: 10 }) // '共 10 条数据'📚 相关资源
- Vue I18n 官方文档
- 项目配置:
src/settings/localeSetting.ts - i18n 初始化:
src/locales/setupI18n.ts
页面信息›
别名国际化指南i18n 指南