vue-i18n / general / MessageContext
接口:MessageContext<T>
消息上下文。
Vue I18n General
类型参数
| 类型参数 | 默认类型 |
|---|---|
T | string |
方法
linked()
调用签名
ts
linked(key, modifier?): MessageType<T>;解析链接的消息。
参数
| 参数 | 类型 | 描述 |
|---|---|---|
key | string | 消息键 |
modifier? | string | 修饰符 |
返回值
MessageType<T> 解析后的消息。
调用签名
ts
linked(
key,
modifier?,
type?): MessageType<T>;重载的 linked
参数
| 参数 | 类型 | 描述 |
|---|---|---|
key | string | 消息键 |
modifier? | string | 修饰符 |
type? | string | 消息类型 |
返回值
MessageType<T> 解析后的消息。
调用签名
ts
linked(key, options?): MessageType<T>;重载的 linked
参数
| 参数 | 类型 | 描述 |
|---|---|---|
key | string | 消息键 |
options? | LinkedOptions | LinkedOptions | 链接选项 |
返回值
MessageType<T> 解析后的消息。
list()
ts
list(index): unknown;从列表中解析消息值。
参数
| 参数 | 类型 | 描述 |
|---|---|---|
index | number | 消息值的索引。 |
返回值
unknown 解析后的消息值。
示例
js
const messages = {
en: {
greeting: ({ list }) => `hello, ${list(0)}!`
}
}named()
ts
named(key): unknown;从命名中解析消息值。
参数
| 参数 | 类型 | 描述 |
|---|---|---|
key | string | 消息值的键。 |
返回值
unknown 解析后的消息值。
示例
js
const messages = {
en: {
greeting: ({ named }) => `hello, ${named('name')}!`
}
}plural()
ts
plural(messages): T;使用复数索引解析消息。
参数
| 参数 | 类型 | 描述 |
|---|---|---|
messages | T[] | 使用翻译函数解析的复数索引消息。 |
返回值
T 解析后的消息。
备注
这是通过翻译函数与复数索引解析的。
示例
js
const messages = {
en: {
car: ({ plural }) => plural(['car', 'cars']),
apple: ({ plural, named }) =>
plural([
'no apples',
'one apple',
`${named('count')} apples`
])
}
}属性
type
ts
type: string;由消息函数处理的消息类型。
备注
通常是 text,在消息函数中需要返回string。
values
ts
values: Record<string, unknown>;消息值。
备注
消息值是从翻译函数传递的参数值,例如 $t、t 或 translate。
示例
vue-i18n $t (或 t) 情况:
html
<p>{{ $t('greeting', { name: 'DIO' }) }}</p> <!-- `{ name: 'DIO' }` 是消息值 -->@intlify/core (@intlify/core-base) translate 情况:
js
translate(context, 'foo.bar', ['dio']) // `['dio']` 是消息值