# General Translation React SDKs (gt-react, gt-next): decodeVars URL: https://generaltranslation.com/ja/docs/react/reference/functions/decode-vars.mdx --- title: decodeVars description: General Translation gt-react で、declareVar によってエンコードされた変数を元の値に展開して戻します。decodeVars の API リファレンス。 --- `decodeVars` 関数は、[`declareVar`](/docs/react/reference/functions/declare-var) のマーカーを含む文字列から元の値を取り出します。`declareVar` は原文に ICU 互換マーカーを追加するため、それらのマーカーが文字列処理ロジックの妨げになることがあります。`decodeVars` はそれらを取り除きます。 *`gt-react`、`gt-next`、`gt-tanstack-start`、`gt-react-native` で利用できます。例では `gt-react` から import していますが、実際にはお使いのフレームワークのパッケージから import してください。* ## 概要 [#overview] `declareVar` マーカーを含む文字列を渡すと、`decodeVars` は元の値がインプレースで入った状態の文字列を返します。 ```tsx function getGreeting({ name }) { const greeting = 'Hello, ' + declareVar(name); // "Hello, {_gt_, select, other {Brian}}" return decodeVars(greeting); // "Hello, Brian" — declareVar を使わなかった場合と同じ結果 } ``` *注: `decodeVars` が作用するのは、`declareVar` マーカーを含むソース文字列だけです。翻訳済みの文字列には影響せず、汎用的な ICU MessageFormat パーサーでもありません。* ## 仕組み [#how-it-works] `decodeVars` は、[`declareVar`](/docs/react/reference/functions/declare-var) で作成された ICU MessageFormat のパターンを対象に、他の内容はそのまま保持したまま、各 `_gt_` 変数を元の値に戻して補間します。変数マーカーを含む文字列に対して、既存の文字列操作ロジックを実行する必要がある場合に使用します。 ## パラメーター [#parameters] | パラメーター | 説明 | 型 | 任意 | デフォルト | | -------------------------- | ---------------------------------------- | -------- | --- | ----- | | [`icuString`](#icu-string) | `declareVar` の呼び出しで生成された ICU マーカーを含む文字列。 | `string` | いいえ | — | ### `icuString` [#icu-string] **Type** `string` · **必須** [`declareVar`](/docs/react/reference/functions/declare-var) によって生成された ICU マーカーを含む string。 ## 戻り値 [#returns] **型** `string` ICUマーカーを削除し、元の変数の値を含む文字列。 ## 例 [#examples] ```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] * `decodeVars` は、[`declareVar`](/docs/react/reference/functions/declare-var) の マーカー を含む strings にのみ使用してください。 * これは `declareVar` によって生成された ICU パターン を対象としており、任意の ICU strings には使用しないでください。 * 翻訳処理には影響せず、string の操作のために マーカー を取り除くだけです。