更新记录

1.1.0(2026-06-05) 下载此版本

新特性

  • 整合原 locale 目录下的完整逻辑
  • 内置多语言支持(中文、英文、日文)
  • 新增 registerLocaleMessages 方法
  • 新增 mergeLocaleMessages 方法
  • 新增 mergeLocaleMessageByKey 方法
  • 新增 getI18nConfig 方法
  • 导出工具函数 createLocaleMessagesdefineLocaleMessagesmergeLocaleMessages

改进

  • 优化语言包合并逻辑
  • 默认使用系统语言
  • 改进 fallback 语言机制

平台兼容性

uni-app(3.7.9)

Vue2 Vue3 Chrome Safari app-vue app-nvue Android iOS 鸿蒙
-
微信小程序 支付宝小程序 抖音小程序 百度小程序 快手小程序 京东小程序 鸿蒙元服务 QQ小程序 飞书小程序 小红书小程序 快应用-华为 快应用-联盟
- - - - -

uni-app x(3.7.9)

Chrome Safari Android iOS 鸿蒙 微信小程序
- - - - -

其他

多语言 暗黑模式 宽屏模式
×

dk-i18n 国际化插件

一个功能完整的 uni-app 国际化插件,支持多语言切换和动态语言加载。

特性

  • 内置多语言支持(中文、英文、日文)
  • 支持语言动态注册和合并
  • 支持 fallback 语言
  • 支持参数插值
  • 语言设置持久化存储
  • 完全兼容原 locale 目录下的 API

安装

在 HBuilderX 中导入本插件即可使用。

使用说明

基础使用

import i18n from '@/uni_modules/dk-i18n/index.js'

// 初始化(可选,已内置默认语言包)
i18n.init()

// 使用翻译
console.log(i18n.t('index.title'))
console.log(i18n.t('hello'))

// 在模板中使用
<text>{{ $t('index.home') }}</text>

自定义初始化

i18n.init({
  locale: 'en',
  fallbackLocale: 'zh-Hans',
  messages: {
    'zh-Hans': {
      custom: '自定义文本'
    },
    'en': {
      custom: 'Custom text'
    }
  }
})

切换语言

i18n.setLocale('en')
console.log(i18n.getLocale())

动态注册语言包

import { defineLocaleMessages, mergeLocaleMessages } from '@/uni_modules/dk-i18n/index.js'

// 注册新语言
i18n.registerLocaleMessages('fr', {
  'hello': 'Bonjour'
})

// 合并语言包
i18n.mergeLocaleMessages({
  'zh-Hans': {
    'new.key': '新增的键'
  }
})

// 通过 key 合并语言包
const customMessages = {
  'custom.text': '自定义文本'
}
i18n.mergeLocaleMessageByKey('zh-Hans', customMessages)

获取配置

const config = i18n.getI18nConfig()
console.log(config.locale)
console.log(Object.keys(config.messages))

内置语言

插件已内置以下语言:

  • zh-Hans - 简体中文
  • zh-Hant - 繁体中文
  • en - English
  • ja - 日语

API 参考

实例方法

方法 说明 参数
init(options) 初始化 i18n options: 配置对象
t(key, params) 获取翻译文本 key: 键名,params: 参数对象
setLocale(locale) 设置当前语言 locale: 语言代码
getLocale() 获取当前语言 -
getLocales() 获取所有可用语言 -
getI18nConfig() 获取 i18n 配置 -
registerLocaleMessages(key, ...messages) 注册语言包 key: 语言代码,messages: 语言包
mergeLocaleMessages(data) 合并语言包 data: 语言包对象
mergeLocaleMessageByKey(key, ...messages) 按 key 合并语言包 key: 语言代码,messages: 语言包

导出函数

函数 说明
createLocaleMessages(...items) 创建语言包对象
defineLocaleMessages(key, ...messages) 定义语言包
mergeLocaleMessages(...messages) 合并多个语言包

目录结构

uni_modules/dk-i18n/
├── uniCloud/
├── components/
├── utssdk/
├── hybrid/
├── pages/
├── static/
├── wxcomponents/
├── locales/
│   ├── en.json
│   ├── zh-Hans.json
│   ├── zh-Hant.json
│   ├── ja.json
│   └── uni-app.ja.json
├── js_sdk/
│   └── index.js
├── index.js           # 入口文件(主)
├── license.md
├── package.json
├── readme.md
└── changelog.md

兼容性

  • HBuilderX 3.1.0+
  • uni-app 4.1.0+
  • uni-app x 4.2.0+

更新日志

changelog.md

许可证

license.md

隐私、权限声明

1. 本插件需要申请的系统权限列表:

none

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

none

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

none

许可协议

许可证

MIT License

Copyright (c) 2026 kordar

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

暂无用户评论。