i18n 国际化模块
这是一个简化的 i18n 国际化模块,用于支持多语言切换。
功能特性
- 支持多语言切换
- 兼容 Vue I18n 的 API
- 支持在 Vue 组件中使用
$t方法 - 支持通过
tt工具函数进行翻译
使用方法
在 Vue 组件中使用
<template>
<view>
<text>{{ $t('user.login.title') }}</text>
</view>
</template>
在脚本中使用
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')
切换语言
import { switchLocale, getCurrentLocale } from '@/utils/utils.uts'
// 切换语言
switchLocale('en-US')
// 获取当前语言
const current = getCurrentLocale()
配置
默认语言为 zh-CN,可以在 index.uts 中修改 defaultLocale 常量。
注意事项
当前实现是简化版本,t 函数直接返回 key。实际项目中需要:
- 加载语言资源文件
- 实现真正的翻译逻辑
- 支持参数插值
文件结构
uni_modules/i18n/
├── index.uts # 主模块文件
├── package.json # 模块配置
└── README.md # 说明文档