# gt-node: General Translation Node.js SDK: getMessages URL: https://generaltranslation.com/zh/docs/node/reference/functions/get-messages.mdx --- title: getMessages description: 获取适用于当前请求区域设置的 General Translation 解析器,用于解析通过 msg 注册的消息。getMessages 的 API 参考。 --- 这是一个 async 函数,会返回一个用于解析预注册消息的解析器。将它与 [`msg`](/docs/node/reference/functions/msg) 搭配使用,可以在构建时注册字符串,并在运行时解析其翻译。 ## 概览 [#overview] 在模块作用域中使用 `msg` 注册字符串,然后在 [`withGT`](/docs/node/reference/functions/with-gt) 作用域内调用并 await `getMessages`,以获取同步的 `m(encodedMsg, options?)` 解析器。 ```ts import { msg, getMessages } from 'gt-node'; // 在构建时(或模块作用域)注册 const greeting = msg('Hello, world!'); // 在运行时解析 const m = await getMessages(); const translated = m(greeting); ``` 签名: ```ts getMessages(): Promise type MFunctionType = ( encodedMsg: T, options?: GTTranslationOptions ) => T extends string ? string : T; ``` *注意:必须在 [`withGT`](/docs/node/reference/functions/with-gt) 的回调中调用 `getMessages`,以便它知道应使用哪个区域设置。* ## 工作原理 [#how-it-works] * **两步模式。** [`msg`](/docs/node/reference/functions/msg) 会注册一个字符串,供构建时翻译;`getMessages` 则会在运行时按当前区域设置解析它。 * **模块作用域字符串。** 这种模式适用于定义在模块作用域、且需要按请求翻译的字符串,例如常量、枚举和错误消息。 * **Null 透传。** 如果向 `m` 传入 `null` 或 `undefined`,它会分别返回 `null` 或 `undefined`。 ## 参数 [#parameters] `getMessages` 不接受任何参数。返回的 `m` 解析器接受以下参数: | 参数 | 说明 | 类型 | 可选 | 默认值 | | ---------------------------- | ---------------------- | ----------------------------- | -- | ---- | | [`encodedMsg`](#encoded-msg) | 由 `msg()` 返回的已编码消息字符串。 | `string \| null \| undefined` | 否 | — | | [`options`](#options) | 用于在解析后的消息中插入变量值的翻译选项。 | `GTTranslationOptions` | 是 | `{}` | ### `encodedMsg` [#encoded-msg] **类型** `string | null | undefined` · **必填** 由 [`msg`](/docs/node/reference/functions/msg) 返回的已编码消息字符串。若传入 `null`/`undefined`,则返回 `null`/`undefined`。 ### `options` [#options] **Type** `GTTranslationOptions` · **可选** 使用 `{key}` 语法将变量值插入解析后的消息中,以及传入翻译选项,例如 `$context`、`$id`、`$locale` 和 `$maxChars`。如果编码后的消息已包含插值选项 (来自 `msg('...', { ... })`) ,则会使用这些选项,并忽略此参数。 ## 返回值 [#returns] **类型** `Promise` 解析结果为 `m` 消息解析函数。`m(encodedMsg, options?)` 会返回解码并插值后的翻译结果,同时保留 `null`/`undefined` 输入。 ## 示例 [#examples] ```ts title="messages.js" // 在模块作用域注册 消息 import { msg } from 'gt-node'; export const GREETING = msg('Hello, world!'); export const WELCOME = msg('Welcome, {name}!'); ``` ```ts title="handler.js" // 解析请求区域设置的消息 import { withGT, getMessages } from 'gt-node'; import { GREETING, WELCOME } from './messages'; function handleRequest(locale) { return withGT(locale, async () => { const m = await getMessages(); return { greeting: m(GREETING), welcome: m(WELCOME, { name: 'Alice' }), }; }); } ``` ```ts title="handler.js" // 使用变量 — 将值作为第二个参数传入 import { msg, getMessages, withGT } from 'gt-node'; const ORDER_STATUS = msg('Order {orderId} is {status}.'); function getOrderMessage(locale, orderId, status) { return withGT(locale, async () => { const m = await getMessages(); return m(ORDER_STATUS, { orderId, status }); }); } ``` ## 注意事项 [#notes] * [`msg`](/docs/node/reference/functions/msg) 会注册一个字符串,供构建时翻译;`getMessages` 会在运行时解析它。 * 这种模式适用于在模块作用域中定义、且需要针对每个请求进行翻译的字符串 (如常量、枚举、错误消息) 。 * 如果向 `m` 传入 `null` 或 `undefined`,它会分别返回 `null` 或 `undefined`。