|
|
1 hete | |
|---|---|---|
| .. | ||
| README.md | 1 hete | |
| index.js | 1 hete | |
├── i18n/ # i18n 配置目录
│ ├── index.js # i18n 初始化配置
│ └── README.md # 使用文档
├── locales/ # 语言包目录(按模块分组)
│ ├── common/ # 通用模块
│ │ ├── zh_CN.js # 中文翻译
│ │ └── en_US.js # 英文翻译
│ ├── pages/ # 页面模块
│ │ └── home/ # 首页模块
│ │ ├── zh_CN.js # 中文翻译
│ │ └── en_US.js # 英文翻译
│ ├── index.js # 语言包导出
│ └── README.md # 语言包管理文档
├── store/ # 状态管理
│ └── locale.js # 语言切换 store
└── components/ # 组件
└── LanguageSwitcher/ # 语言切换组件
<template>
<view>
<!-- 使用 t 函数翻译 -->
<text>{{ t('home.title') }}</text>
<!-- 使用嵌套的翻译键 -->
<text>{{ t('common.button.confirm') }}</text>
</view>
</template>
<script setup>
import { useI18n } from 'vue-i18n'
const { t } = useI18n()
</script>
import { useI18n } from 'vue-i18n'
const { t } = useI18n()
// 使用翻译
const message = t('common.message.success')
// 使用参数
const greeting = t('common.greeting', { name: '张三' })
<script setup>
import { useLocaleStore } from '@/store/locale'
const localeStore = useLocaleStore()
// 切换到指定语言
localeStore.setLocale('en-US')
// 切换到下一个语言
localeStore.toggleLocale()
// 获取当前语言
const currentLang = localeStore.currentLocale
</script>
<template>
<view>
<!-- 直接使用组件 -->
<LanguageSwitcher />
</view>
</template>
<script setup>
import LanguageSwitcher from '@/components/LanguageSwitcher/index.vue'
</script>
在对应目录下创建语言文件:
// locales/pages/user/zh_CN.js
export default {
profile: '个人资料',
settings: '设置',
logout: '退出登录'
}
// locales/pages/user/en_US.js
export default {
profile: 'Profile',
settings: 'Settings',
logout: 'Logout'
}
// locales/index.js
import commonZhCN from './common/zh_CN'
import commonEnUS from './common/en_US'
import homeZhCN from './pages/home/zh_CN'
import homeEnUS from './pages/home/en_US'
import userZhCN from './pages/user/zh_CN' // 新增
import userEnUS from './pages/user/en_US' // 新增
export const messages = {
'zh-CN': {
common: commonZhCN,
home: homeZhCN,
user: userZhCN // 新增
},
'en-US': {
common: commonEnUS,
home: homeEnUS,
user: userEnUS // 新增
}
}
<template>
<text>{{ t('user.profile') }}</text>
</template>
为每个现有模块添加新语言翻译:
locales/
├── common/
│ ├── zh_CN.js
│ ├── en_US.js
│ └── ja_JP.js # 新增日文
└── pages/
└── home/
├── zh_CN.js
├── en_US.js
└── ja_JP.js # 新增日文
// locales/index.js
import commonZhCN from './common/zh_CN'
import commonEnUS from './common/en_US'
import commonJaJP from './common/ja_JP' // 新增
import homeZhCN from './pages/home/zh_CN'
import homeEnUS from './pages/home/en_US'
import homeJaJP from './pages/home/ja_JP' // 新增
export const messages = {
'zh-CN': {
common: commonZhCN,
home: homeZhCN
},
'en-US': {
common: commonEnUS,
home: homeEnUS
},
'ja-JP': { // 新增
common: commonJaJP,
home: homeJaJP
}
}
export const localeList = [
{ label: '简体中文', value: 'zh-CN' },
{ label: 'English', value: 'en-US' },
{ label: '日本語', value: 'ja-JP' } // 新增
]
userName、submitButtontext1、label2 等common、home、user、order 等common 模块存放通用文本:按钮、提示信息等// ✅ 推荐
{
user: {
profile: {
title: '个人资料',
name: '姓名',
email: '邮箱'
}
}
}
// ❌ 不推荐
{
userProfileTitle: '个人资料',
userProfileName: '姓名',
userProfileEmail: '邮箱'
}
// locales/zh-CN/common.js
export default {
greeting: '你好,{name}!',
itemCount: '共 {count} 项'
}
// 使用
t('common.greeting', { name: '张三' })
t('common.itemCount', { count: 10 })
const localeStore = useLocaleStore()
// 属性
localeStore.currentLocale // 当前语言
localeStore.availableLocales // 可用语言列表
// 方法
localeStore.initLocale() // 初始化语言设置
localeStore.setLocale(locale) // 设置语言
localeStore.toggleLocale() // 切换到下一个语言
localeStore.getCurrentLocaleName() // 获取当前语言名称
const { t, locale } = useI18n()
// 翻译文本
t('home.title')
t('common.greeting', { name: 'John' })
// 获取当前语言
console.log(locale.value)
createSSRApp 时需要特别配置(已完成)检查:
原因:可能使用了非响应式的方式获取翻译
解决:确保使用 t() 函数或 computed 包装
检查:
legacy: false 配置globalInjection