Skip to content

国际化

SeeYouUI 内置了一套轻量级的国际化方案,支持中英文以及其他任意语言的切换。

它完全兼容 uni-app 所有平台(H5、App、小程序),无外部依赖,且与组件库深度集成 —— 切换语言后所有组件的文案自动更新。

快速开始

零配置使用

如果你只需要中文或英文,不需要任何配置,直接使用即可。

组件会跟随系统语言自动切换:

  • 系统语言为中文时,组件显示中文文案
  • 系统语言为其他时,组件默认显示英文文案
vue
<template>
  <!-- 按钮文案自动跟随系统语言 -->
  <see-calendar />
  <see-picker />
  <see-pagination />
</template>

指定默认语言

在你的入口文件(main.jsmain.ts)中,创建 i18n 实例并在 app.use() 时传入:

js
// main.js
import { createI18n } from 'see-u-ui'
import SeeYouUI from 'see-u-ui'

// 创建 i18n 实例
const i18n = createI18n({
  locale: 'zh-CN',          // 默认语言
  fallbackLocale: 'zh-CN',  // 找不到 key 时的回退语言
})

// #ifdef VUE3
import { createSSRApp } from 'vue'
export function createApp() {
  const app = createSSRApp(App)
  // 传入 i18n 实例
  app.use(SeeYouUI, { i18n })
  return { app }
}
// #endif

在组件中动态切换语言

vue
<template>
  <view>
    <see-calendar />
    <button @click="switchToEn">English</button>
    <button @click="switchToZh">中文</button>
  </view>
</template>

<script setup>
import { useI18n } from 'see-u-ui'

const { setLocale } = useI18n()

const switchToEn = () => setLocale('en')
const switchToZh = () => setLocale('zh-CN')
</script>

自定义语言包

你可以覆盖内置文案,或添加全新的语言。

js
import { createI18n } from 'see-u-ui'

const i18n = createI18n({
  locale: 'ja',             // 使用日语
  fallbackLocale: 'zh-CN',  // 找不到日语 key 时回退到中文
  messages: {
    // 完全自定义中文文案
    'zh-CN': {
      cancel: '返回',
      confirm: '好的',
      'calendar.week.sunday': '周日',
      // ... 覆盖其他 key
    },
    // 添加新语言
    'ja': {
      cancel: 'キャンセル',
      confirm: '確認',
      'calendar.week.sunday': '日',
      'calendar.week.monday': '月',
      // ...
    },
  },
})

export function createApp() {
  const app = createSSRApp(App)
  app.use(SeeYouUI, { i18n })
  return { app }
}

在业务代码中使用翻译

useI18n() 返回的 t() 函数也可以用来翻译你自己的文案:

vue
<template>
  <view>
    <h1>{{ t('greeting', { name: userName }) }}</h1>
    <button @click="toggleLang">切换语言</button>
  </view>
</template>

<script setup>
import { ref } from 'vue'
import { useI18n } from 'see-u-ui'

const { t, locale, setLocale } = useI18n()
const userName = ref('宝宝')

const toggleLang = () => {
  locale.value = locale.value === 'zh-CN' ? 'en' : 'zh-CN'
}
</script>

你需要在 createI18n 中定义自己的翻译 key:

js
const i18n = createI18n({
  messages: {
    'zh-CN': {
      greeting: '你好,{name}!',
      // ... 其他组件 key 会与内置语言包自动合并
    },
    'en': {
      greeting: 'Hello, {name}!',
    },
  },
})

使用 <see-config> 配置

如果你使用 <see-config> 组件作为根节点,也可以在上面设置语言:

vue
<!-- App.vue -->
<template>
  <see-config locale="en">
    <!-- 所有子组件的语言都变为英文 -->
    <router-view />
  </see-config>
</template>

内置语言 Key 列表

以下是 SeeYouUI 组件内置的所有翻译 key,你可以覆盖其中任意一个:

通用

Key中文英文
cancel取消Cancel
confirm确定OK
close关闭Close
loading加载中...Loading...
noData暂无数据No Data
loadMore加载更多Load More
pullRefresh下拉刷新Pull to Refresh
retry重试Retry
search搜索Search
reset重置Reset

Calendar 日历

Key中文英文
calendar.week.sundaySu
calendar.week.mondayMo
calendar.week.tuesdayTu
calendar.week.wednesdayWe
calendar.week.thursdayTh
calendar.week.fridayFr
calendar.week.saturdaySa
calendar.today今天Today
calendar.month.january1月Jan
calendar.month.february2月Feb
calendar.month.march3月Mar
calendar.month.april4月Apr
calendar.month.may5月May
calendar.month.june6月Jun
calendar.month.july7月Jul
calendar.month.august8月Aug
calendar.month.september9月Sep
calendar.month.october10月Oct
calendar.month.november11月Nov
calendar.month.december12月Dec

Picker / DatetimePicker 选择器

Key中文英文
picker.cancel取消Cancel
picker.confirm确定Confirm
picker.yearYear
picker.monthMonth
picker.dayDay
picker.hourHour
picker.minuteMin
picker.secondSec
picker.startDate开始日期Start
picker.endDate结束日期End

Upload 上传

Key中文英文
upload.selectFile选择文件Select File
upload.uploading上传中...Uploading...
upload.success上传成功Uploaded
upload.fail上传失败Upload Failed
upload.exceedSize文件大小超出限制File size exceeded
upload.exceedCount文件数量超出限制File count exceeded

Pagination 分页器

Key中文英文
pagination.total共 {total} 条{total} items
pagination.prev上一页Prev
pagination.next下一页Next

Table 表格

Key中文英文
table.empty暂无数据No Data
table.sortAsc升序Ascending
table.sortDesc降序Descending

Empty 空状态

Key中文英文
empty.default暂无数据No Data
empty.network网络不给力Network Error
empty.search没有找到相关内容No Results Found
Key中文英文
modal.cancel取消Cancel
modal.confirm确定Confirm

ActionSheet 操作菜单

Key中文英文
actionSheet.cancel取消Cancel

Search 搜索

Key中文英文
search.placeholder请输入搜索内容Please enter keywords
search.cancel取消Cancel

CountDown 倒计时

Key中文英文
countdown.dayd
countdown.hourh
countdown.minutem
countdown.seconds

Keyboard 键盘

Key中文英文
keyboard.done完成Done

Cascader 级联选择器

Key中文英文
cascader.placeholder请选择Please select

NoticeBar 滚动通知

Key中文英文
noticeBar.close关闭Close
noticeBar.more详情Details

SwipeAction 滑动操作

Key中文英文
swipeAction.confirm确认删除?Confirm delete?

NoNetwork 无网络

Key中文英文
noNetwork.title网络不给力Network Error
noNetwork.retry点击重试Tap to retry

LoadingPage 加载页

Key中文英文
loadingPage.loading加载中...Loading...

关于 vue-i18n

SeeYouUI 的内置 i18n 是完全独立的,与 vue-i18n 无关。

如果你在项目中使用 vue-i18n,两者可以共存互不干扰,但你需要自行处理两个 i18n 实例之间的语言同步(如果有这方面的需求)。

推荐的做法是:直接使用 SeeYouUI 内置的 i18n 来管理组件库的文案,项目自身的业务文案你可以选择使用 SeeYouUI 的 i18n(useI18n)或 vue-i18n,二者不冲突。

API 参考

详见 useI18n API 文档

辽 ICP 备 2025070134 号