Skip to content

vue-i18n / general / MessageContext

接口:MessageContext<T>

消息上下文。

Vue I18n General

类型参数

类型参数默认类型
Tstring

方法

linked()

调用签名

ts
linked(key, modifier?): MessageType<T>;

解析链接的消息。

参数
参数类型描述
keystring消息键
modifier?string修饰符
返回值

MessageType<T> 解析后的消息。

调用签名

ts
linked(
   key, 
   modifier?, 
type?): MessageType<T>;

重载的 linked

参数
参数类型描述
keystring消息键
modifier?string修饰符
type?string消息类型
返回值

MessageType<T> 解析后的消息。

调用签名

ts
linked(key, options?): MessageType<T>;

重载的 linked

参数
参数类型描述
keystring消息键
options?LinkedOptionsLinkedOptions | 链接选项
返回值

MessageType<T> 解析后的消息。


list()

ts
list(index): unknown;

从列表中解析消息值。

参数

参数类型描述
indexnumber消息值的索引。

返回值

unknown 解析后的消息值。

示例

js
const messages = {
  en: {
    greeting: ({ list }) => `hello, ${list(0)}!`
  }
}

named()

ts
named(key): unknown;

从命名中解析消息值。

参数

参数类型描述
keystring消息值的键。

返回值

unknown 解析后的消息值。

示例

js
const messages = {
  en: {
    greeting: ({ named }) => `hello, ${named('name')}!`
  }
}

plural()

ts
plural(messages): T;

使用复数索引解析消息。

参数

参数类型描述
messagesT[]使用翻译函数解析的复数索引消息。

返回值

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>;

消息值。

备注

消息值是从翻译函数传递的参数值,例如 $tttranslate

示例

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']` 是消息值