函数 API
September 6, 2025 · View on GitHub
目录
函数列表
initI18n
类型
参数说明
t
类型
参数说明
属性
t
类型
参数说明
withLocale
类型
参数说明
setI18n
类型
参数说明
其他类型
LangPack
I18nState
FormatFunc
FormatDateFunc
FormatPluralFunc
函数列表
initI18n
初始化固定配置,获取核心 API
类型
(
props: {
namespace: string,
locale?: string,
langs?: Record<string, (() => Promise<LangPack>) | LangPack>,
beginIndex?: number,
formatNumber?: FormatFunc,
formatCurrency?: FormatFunc,
formatDate?: FormatDateFunc,
formatTime?: FormatDateFunc,
formatPlural?: FormatPluralFunc,
}
) => ({
t,
setI18n,
})
参数说明
| 参数名 | 说明 |
|---|---|
| namespace | 指定命名空间 |
| locale |
指定当前语言 📢📢📢: locale 的值默认跟语言代码相对应,如需自定义,需参考 codeLocaleMap 的用法
|
| langs | 设置当前语言包 |
| beginIndex |
设置 t 函数中 插值变量 起始下标,默认为 0
|
| formatNumber |
数字 类型 插值变量 的 格式化器 ,对应的类型标记是 n 或 N
|
| formatCurrency |
货币 类型 插值变量 的 格式化器 ,对应的类型标记是 c 或 C
|
| formatDate |
日期 类型 插值变量 的 格式化器 ,对应的类型标记是 d 或 D
|
| formatTime |
时间 类型 插值变量 的 格式化器 ,对应的类型标记是 t 或 T
|
| formatPlural |
复数 类型 插值变量 的 格式化器 ,对应的类型标记是 p 或 P
|
t
获取国际化文案
内部会根据当前语言 locale 从语言包 langs 中获取 text 对应的 文案 ,未匹配到对应翻译内容会直接显示 text 本身内容
类型
interface Translate {
(text: string, ...args: Array<string | number | unknown>): string
t: (
key: string,
text: string,
...args: Array<string | number | unknown>
) => string
withLocale: (locale?: string) => Translate
}
参数说明
| 参数名 | 说明 |
|---|---|
| text | 待翻译的文案,该文案需满足特定 匹配规则 |
| args |
表示 插值变量 ,没有个数限制, text 文案中需要以 {index} 的形式来接收, index 表示 插值变量 的位置,从 0 开始(可在 initI18n 中自定义起始值),第 1 个参数对应 0,对 2 个参数对应 1,以此往复
|
属性
t
获取自定义 key 国际化文案内部会根据当前语言
locale 从语言包 langs 中获取 key 对应的 文案 ,未匹配到对应翻译内容会直接显示 text 本身内容
类型
( key: string, text: string, ...args: Array<string | number | unknown> ) => string
参数说明
| 参数名 | 说明 |
|---|---|
| key | 自定义key,需要满足特定 匹配规则 |
| text | 待翻译的文案,该文案需满足特定 匹配规则 |
| args |
表示 插值变量 ,没有个数限制, text 文案中需要以 {index} 的形式来接收, index 表示 插值变量 的位置,从 0 开始(可在 initI18n 中自定义起始值),第 1 个参数对应 0,对 2 个参数对应 1,以此往复
|
withLocale
生成新的 t 函数
适用于服务端,每个接口响应需要做国际化的处理
类型
( locale?: string, ) => Translate
参数说明
| 参数名 | 说明 |
|---|---|
| locale |
指定当前语言 若未指定语言,则与生成前的语言保持一致 |
setI18n
设置语言、语言包
类型
(
props: {
locale?: string,
langs?: Record<string, LangPack>,
}
) => Promise<I18nState>
参数说明
| 参数名 | 说明 |
|---|---|
| locale | 指定当前语言 |
| langs | 设置当前语言包,支持增量添加,新增的会覆盖合并到原有的之中 |
其他类型
以下类型是为了方便文档说明,与代码中类型写法上会存在区别,需以实际代码为准
LangPack
语言包
type LangPack = Record<string, string>
I18nState
命名空间下的状态
type I18nState = {
namespace: string
locale?: string
langs?: Record<string, (() => Promise<LangPack>) | LangPack>
beginIndex?: number
formatNumber?: FormatFunc,
formatCurrency?: FormatFunc,
formatDate?: FormatDateFunc,
formatTime?: FormatDateFunc,
formatPlural?: FormatPluralFunc,
}
FormatFunc
通用的 格式化器 类型
type FormatFunc = <T>(props: {
/**
* 当前语言
*/
locale: string,
/**
* 插值变量
*/
payload: string | number | unknown | T,
/**
* t 函数
*/
t: t,
}) => number | string
FormatDateFunc
日期(时间)的 格式化器 类型
type FormatDateFunc = <T>(props: {
/**
* 当前语言
*/
locale: string,
/**
* 插值变量
*/
payload: string | number | Date | unknown | T,
/**
* t 函数
*/
t: t,
}) => string
FormatPluralFunc
复数的 格式化器 类型
type FormatPluralFunc = <T>(props: {
/**
* 当前语言
*/
locale: string,
/**
* 插值变量
*/
payload: string | number | unknown | T,
/**
* 默认将量词和名词组合起来的字符串,不需要复数处理的语言可以直接返回该属性
*/
text: string
/**
* 复数关键词
*/
keyword: string
/**
* t 函数
*/
t: t,
}) => string