# gt-node: General Translation Node.js SDK: decodeMsg
URL: https://generaltranslation.com/zh/docs/node/reference/functions/decode-msg.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: 从 General Translation 的编码后的消息中还原原始消息字符串。decodeMsg 的 API 参考。

从由 [`msg`](/docs/node/reference/functions/msg) 编码的值中提取原始的插值后消息字符串。当你需要编码后的消息的人类可读内容，而不是其翻译结果时，请使用此函数。

## 概览 [#overview]

使用编码后的消息调用 `decodeMsg`。它会返回编码后选项之前的内容 (即直到最后一个冒号为止的所有内容) 。如果输入中没有冒号分隔符，则会原样返回。

```ts
import { msg, decodeMsg } from 'gt-node';

const encoded = msg('Hello, {name}!', { name: 'Brian' });
console.log(decodeMsg(encoded)); // 'Hello, Brian!'
```

函数签名：

```ts
decodeMsg(encodedMsg: string): string;
decodeMsg<T extends null | undefined>(encodedMsg: T): T;
```

## 工作原理 [#how-it-works]

* **按分隔符拆分。** 编码后的消息格式为 `interpolatedContent:encodedOptions`。`decodeMsg` 返回最后一个冒号之前的内容。
* **透传。** 当该值不包含冒号，或为 `null`/`undefined` 时，会原样返回。

## 参数 [#parameters]

| 参数                           | 描述          | 类型                            | 可选 | 默认值 |
| ---------------------------- | ----------- | ----------------------------- | -- | --- |
| [`encodedMsg`](#encoded-msg) | 要解码的编码后的消息。 | `string \| null \| undefined` | 否  | —   |

### `encodedMsg` [#encoded-msg]

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

要解码的消息内容，通常为 [`msg`](/docs/node/reference/functions/msg) 的返回值。

## 返回值 [#returns]

**类型** `string | null | undefined`

解码后的消息内容；如果无法解码，则原样返回输入。

## 示例 [#examples]

```ts
import { msg, decodeMsg } from 'gt-node';

const encoded = msg('Welcome, {name}!', { name: 'Alice' });
console.log(decodeMsg(encoded)); // 'Welcome, Alice!'

console.log(decodeMsg('Plain text')); // 'Plain text'（无选项可剥离）
```

## 注意事项 [#notes]

* `decodeMsg` 返回的是源内容，而不是译文。请使用 [`getMessages`](/docs/node/reference/functions/get-messages) 来解析译文。
* 如果要读取编码后的选项而非内容，请使用 [`decodeOptions`](/docs/node/reference/functions/decode-options)。

## Sitemap

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