# General Translation React SDKs (gt-react, gt-next, gt-react-native): decodeVars
URL: https://generaltranslation.com/zh/docs/react/reference/functions/decode-vars.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: 将由 declareVar 编码的变量还原为原始值。decodeVars 的 API 参考。

`decodeVars` 函数会从包含 [`declareVar`](/docs/react/reference/functions/declare-var) 标记的字符串中提取原始值。由于 [`declareVar`](/docs/react/reference/functions/declare-var) 会向源文本添加与 ICU 兼容的标记，这些标记可能会干扰字符串处理逻辑，因此 `decodeVars` 会将它们移除。

*可用于 `gt-react`、`gt-next`、`gt-tanstack-start` 和 `gt-react-native`。*

## 概览 [#overview]

传入一个包含 [`declareVar`](/docs/react/reference/functions/declare-var) 标记的字符串，`decodeVars` 会返回将原始值原地替换回去后的字符串。

```tsx
function getGreeting({ name }) {
  const greeting = 'Hello, ' + declareVar(name);
  // "Hello, {_gt_, select, other {Brian}}"

  return decodeVars(greeting);
  // "Hello, Brian" — 就好像从未调用过 declareVar 一样
}
```

*注意：`decodeVars` 仅作用于包含 [`declareVar`](/docs/react/reference/functions/declare-var) 标记的源字符串。它不会影响已翻译的字符串，也不是通用的 ICU MessageFormat 解析器。*

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

`decodeVars` 用于处理由 [`declareVar`](/docs/react/reference/functions/declare-var) 创建的 ICU MessageFormat 模式，将每个 `_gt_` 变量还原为其原始值，同时原样保留其他内容不变。当现有的字符串处理逻辑需要作用于包含变量标记的字符串时，请使用它。

## 参数 [#parameters]

| 参数                         | 描述                                                                               | 类型       | 可选 | 默认值 |
| -------------------------- | -------------------------------------------------------------------------------- | -------- | -- | --- |
| [`icuString`](#icu-string) | 包含来自 [`declareVar`](/docs/react/reference/functions/declare-var) 调用的 ICU 标记的字符串。 | `string` | 否  | —   |

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

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

包含由 [`declareVar`](/docs/react/reference/functions/declare-var) 生成的 ICU 标记的字符串。

## 返回值 [#returns]

**类型** `string`

移除 ICU 标记后、保留原始变量值的字符串。

## 示例 [#examples]

*示例从 `gt-react` 导入；请改为从你所用框架的 package 导入。*

```tsx title="BasicUsage.tsx"
import { declareVar, decodeVars } from 'gt-react';

const encodedVar = declareVar(name);
// "{_gt_, select, other {Alice}}"
const decodedVar = decodeVars(encodedVar);
// "Alice"
```

```tsx title="MultipleVariables.tsx"
import { declareVar, decodeVars } from 'gt-react';

const name1 = 'Alice';
const name2 = 'Bob';
const encodedVar =
  'Hello, ' + declareVar(name1) + '! My name is ' + declareVar(name2);
// "Hello, {_gt_, select, other {Alice}}! My name is {_gt_, select, other {Bob}}"

const decodedVar = decodeVars(encodedVar);
// "Hello, Alice! My name is Bob"
```

## 注意事项 [#notes]

* 仅对包含 [`declareVar`](/docs/react/reference/functions/declare-var) 标记的字符串使用 `decodeVars`。
* 它针对由 [`declareVar`](/docs/react/reference/functions/declare-var) 创建的 ICU 模式，不应在任意 ICU 字符串上使用。
* 它不会影响翻译处理——它只会移除这些标记，以便进行字符串操作。

## Sitemap

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