项目正常启动
This commit is contained in:
67
uni_modules/i18n/README.md
Normal file
67
uni_modules/i18n/README.md
Normal file
@@ -0,0 +1,67 @@
|
||||
# i18n 国际化模块
|
||||
|
||||
这是一个简化的 i18n 国际化模块,用于支持多语言切换。
|
||||
|
||||
## 功能特性
|
||||
|
||||
- 支持多语言切换
|
||||
- 兼容 Vue I18n 的 API
|
||||
- 支持在 Vue 组件中使用 `$t` 方法
|
||||
- 支持通过 `tt` 工具函数进行翻译
|
||||
|
||||
## 使用方法
|
||||
|
||||
### 在 Vue 组件中使用
|
||||
|
||||
```vue
|
||||
<template>
|
||||
<view>
|
||||
<text>{{ $t('user.login.title') }}</text>
|
||||
</view>
|
||||
</template>
|
||||
```
|
||||
|
||||
### 在脚本中使用
|
||||
|
||||
```typescript
|
||||
import i18n from '@/uni_modules/i18n/index.uts'
|
||||
import { tt } from '@/utils/i18nfun.uts'
|
||||
|
||||
// 方式1:直接使用 i18n
|
||||
const text = i18n.global.t('user.login.title')
|
||||
|
||||
// 方式2:使用工具函数
|
||||
const text2 = tt('user.login.title')
|
||||
```
|
||||
|
||||
### 切换语言
|
||||
|
||||
```typescript
|
||||
import { switchLocale, getCurrentLocale } from '@/utils/utils.uts'
|
||||
|
||||
// 切换语言
|
||||
switchLocale('en-US')
|
||||
|
||||
// 获取当前语言
|
||||
const current = getCurrentLocale()
|
||||
```
|
||||
|
||||
## 配置
|
||||
|
||||
默认语言为 `zh-CN`,可以在 `index.uts` 中修改 `defaultLocale` 常量。
|
||||
|
||||
## 注意事项
|
||||
|
||||
当前实现是简化版本,`t` 函数直接返回 key。实际项目中需要:
|
||||
1. 加载语言资源文件
|
||||
2. 实现真正的翻译逻辑
|
||||
3. 支持参数插值
|
||||
|
||||
## 文件结构
|
||||
|
||||
```
|
||||
uni_modules/i18n/
|
||||
├── index.uts # 主模块文件
|
||||
├── package.json # 模块配置
|
||||
└── README.md # 说明文档
|
||||
```
|
||||
39
uni_modules/i18n/index.uts
Normal file
39
uni_modules/i18n/index.uts
Normal file
@@ -0,0 +1,39 @@
|
||||
// i18n 国际化配置
|
||||
// 这是一个简化的 i18n 实现,用于支持多语言切换
|
||||
|
||||
// 语言资源
|
||||
const messages: UTSJSONObject = new UTSJSONObject()
|
||||
|
||||
// 默认语言
|
||||
const defaultLocale = 'zh-CN'
|
||||
|
||||
// 当前语言(响应式)
|
||||
let currentLocale = defaultLocale
|
||||
|
||||
// 翻译函数
|
||||
function t(key: string, values: UTSJSONObject | null = null, locale: string | null = null): string {
|
||||
const targetLocale = locale ?? currentLocale
|
||||
// 这里应该从 messages 中获取翻译,简化实现直接返回 key
|
||||
// 实际项目中应该加载语言资源文件
|
||||
return key
|
||||
}
|
||||
|
||||
// 创建响应式 locale 对象
|
||||
const localeObj = {
|
||||
get value(): string {
|
||||
return currentLocale
|
||||
},
|
||||
set value(newLocale: string) {
|
||||
currentLocale = newLocale
|
||||
}
|
||||
}
|
||||
|
||||
// 导出 i18n 对象(兼容 Vue I18n 的 API)
|
||||
const i18nInstance = {
|
||||
global: {
|
||||
t: t,
|
||||
locale: localeObj
|
||||
}
|
||||
}
|
||||
|
||||
export default i18nInstance
|
||||
9
uni_modules/i18n/package.json
Normal file
9
uni_modules/i18n/package.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"name": "i18n",
|
||||
"version": "1.0.0",
|
||||
"main": "index.uts",
|
||||
"types": "index.uts",
|
||||
"uni_modules": {
|
||||
"uni_modules": true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user