# Vue: MessagesFunction
URL: https://generaltranslation.com/zh/docs/vue/reference/types/messages-function.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: 从当前 catalog 中解析已注册的 messages、原始 strings 和 nullish 值。MessagesFunction 的 API 参考。

[`useMessages()`](/docs/vue/reference/composables/use-messages) 返回此 回调。它会解码由 [`msg()`](/docs/vue/reference/functions/msg) 创建的值，也接受普通源字符串。

## 概览 [#overview]

```ts
type MessagesFunction = <T extends string | null | undefined>(
  message: T,
  options?: GTStringOptions
) => T extends string ? string : T;
```

| 参数                    | 描述                                 | 类型                                                               | 可选 | 默认值  |
| --------------------- | ---------------------------------- | ---------------------------------------------------------------- | -- | ---- |
| [`message`](#message) | 编码后的消息、原始字符串、`null` 或 `undefined`。 | `T extends string \| null \| undefined`                          | 否  | —    |
| [`options`](#options) | 原始字符串的上下文。                         | [`GTStringOptions`](/docs/vue/reference/types/gt-string-options) | 是  | `{}` |

## 参数 [#parameters]

### `message`

**类型** `T extends string | null | undefined` · **必填**

传入由 [`msg()`](/docs/vue/reference/functions/msg) 返回的值、普通源字符串或 nullish 值。编码后的字符串包含原始源文本、哈希值以及可选的 `$context`。

### `options`

**类型** [`GTStringOptions`](/docs/vue/reference/types/gt-string-options) · **可选** · **默认值** `{}`

对于原始字符串，`$context` 会参与 catalog 哈希值的计算。对于编码后的值，[`msg()`](/docs/vue/reference/functions/msg) 存储的元数据优先，调用处提供的选项将被忽略。

## 返回值 [#returns]

| 输入          | 返回类型        | 行为                         |
| ----------- | ----------- | -------------------------- |
| `string`    | `string`    | 返回 catalog 翻译或解码后的源语言后备内容。 |
| `null`      | `null`      | 原样返回 `null`。               |
| `undefined` | `undefined` | 原样返回 `undefined`。          |

条件泛型会保留可空输入类型，因此可选 messages 无需额外的守卫即可直接传递。

## 解析 [#resolution]

对于编码后的消息，回调会在查找前解码已存储的源文本、`$context` 和预先计算的哈希值。对于普通字符串，它会根据该字符串和调用点的 `$context` 计算哈希值。

两种情况都要求活动 catalog 中存在字符串值。条目缺失或不是字符串时，将返回源文本。不执行 ICU 格式化或插值；花括号会保留为字面量。

当 [`createGT()`](/docs/vue/reference/functions/create-gt) 插件加载 catalog 或更改区域设置后，如需更新结果，请在 Vue 模板、计算属性或响应式副作用中调用该回调。

## 示例 [#example]

```vue title="src/PageTitle.vue"
<script setup lang="ts">
import { msg, useMessages } from 'gt-vue';

const title = msg('Settings', { $context: 'page title' });
const m = useMessages();
</script>

<template>
  <h1>{{ m(title) }}</h1>
</template>
```

## Sitemap

See the full [sitemap](https://generaltranslation.com/sitemap.md) for all pages.
