# gt-node: General Translation Node.js SDK: decodeVars
URL: https://generaltranslation.com/zh/docs/node/reference/functions/decode-vars.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: 将 General Translation 的 declareVar 变量重新插值到字符串中。decodeVars 的 API 参考。

将 [`declareVar`](/docs/node/reference/functions/declare-var) 编码进 ICU 字符串的 `_gt_` 变量重新插值，并将每个已编码的 `select` 表达式替换为其对应的值。

## 概览 [#overview]

使用包含 [`declareVar`](/docs/node/reference/functions/declare-var) 输出内容的 ICU 字符串调用 `decodeVars`。它会返回一个字符串，其中每个编码后的字符串变量都会被替换为其 `other` 值。不含任何 `_gt_` 变量的字符串会原样返回。

```ts
import { decodeVars, declareVar } from 'gt-node';

const encoded = 'Hi ' + declareVar('Brian') + ', my name is {name}';
const decoded = decodeVars(encoded);
console.log(decoded); // 'Hi Brian, my name is {name}'
```

签名：

```ts
decodeVars(icuString: string): string
```

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

* **定向插值。** 只有由 [`declareVar`](/docs/node/reference/functions/declare-var) 生成的 `_gt_` 变量会被插值；其他 ICU 占位符 (如 `{name}`) 会保留不变。
* **透传。** 当 字符串 中不包含 `_gt_` 标识符时，会原样返回。

## 参数 [#parameters]

| 参数                         | 描述            | 类型       | 可选 | 默认值 |
| -------------------------- | ------------- | -------- | -- | --- |
| [`icuString`](#icu-string) | 要插值的 ICU 字符串。 | `string` | 否  | —   |

### `icuString` [#icu-string]

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

一个 ICU 字符串，可能包含由 [`declareVar`](/docs/node/reference/functions/declare-var) 编码的 `_gt_` 变量。

## 返回值 [#returns]

**类型** `string`

返回将已编码的 `_gt_` 变量替换为对应值后的字符串；如果不存在这类变量，则原样返回输入内容。

## 示例 [#examples]

```ts
import { decodeVars, declareVar } from 'gt-node';

const encoded = 'Hi ' + declareVar('Brian') + ', my name is {name}';
console.log(decodeVars(encoded)); // 'Hi Brian, my name is {name}'
```

## 注意事项 [#notes]

* `decodeVars` 是对 [`declareVar`](/docs/node/reference/functions/declare-var) 的补充；它只会影响 `_gt_` 变量。

## Sitemap

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