# gt-node: General Translation Node.js SDK: decodeOptions
URL: https://generaltranslation.com/zh/docs/node/reference/functions/decode-options.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: 从 General Translation 编码后的消息中还原已编码的选项。decodeOptions 的 API 参考。

解码 [`msg`](/docs/node/reference/functions/msg) 嵌入在编码后的消息中的选项对象。可用它查看编码消息中携带的元数据和插值变量。

## 概览 [#overview]

使用编码后的消息调用 `decodeOptions`。该函数会解析并返回选项对象；如果消息不包含编码后的选项，或无法解析，则返回 `null`。

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

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

签名：

```ts
decodeOptions(encodedMsg: string): TranslationOptions | null
```

## 工作机制 [#how-it-works]

* **按分隔符拆分。** 编码后的消息格式为 `interpolatedContent:encodedOptions`。`decodeOptions` 会读取最后一个冒号后面的片段，对其进行 base64 解码，并解析 JSON 选项。
* **返回空值。** 当消息中没有冒号分隔符，或编码片段无法解析时，会返回 `null`。

## 参数 [#parameters]

| 参数                           | 描述            | Type     | 可选 | 默认值 |
| ---------------------------- | ------------- | -------- | -- | --- |
| [`encodedMsg`](#encoded-msg) | 用于读取选项的编码后的消息。 | `string` | 否  | —   |

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

**类型** `string` · **必填**

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

## 返回值 [#returns]

**类型** `TranslationOptions | null`

已解码的选项对象，包含所有插值变量以及 `$_source` 和 `$_hash` 等元数据；如果没有可解码的选项，则返回 `null`。

## 示例 [#examples]

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

const encoded = msg('You have {count} messages', { count: 3 });
const options = decodeOptions(encoded);
console.log(options?.count); // 3

console.log(decodeOptions('Plain text')); // null
```

## 备注 [#notes]

* 如需读取消息内容而非其选项，请使用 [`decodeMsg`](/docs/node/reference/functions/decode-msg)。

## Sitemap

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