# react-native: decodeVars URL: https://generaltranslation.com/ja/docs/react-native/api/strings/decode-vars.mdx --- title: decodeVars description: decodeVars() 文字列関数の API リファレンス --- {/* 自動生成: 直接編集しないでください。代わりに content/docs-templates/ 内のテンプレートを編集してください。 */} ## 概要 `declareVar` はソーステキストに ICU 互換のマーカーを追加するため、既存の文字列処理ロジックに問題を引き起こす可能性があります。 `decodeVars` 関数は、`declareVar` マーカーを含む文字列から元の値を抽出します。 ```jsx function getGreeting({ name }) { const greeting = "Hello, " + declareVar(name); // "Hello, {_gt_, select, other {Brian}}" const decodedGreeting = decodeVars(greeting); // "Hello, Brian" <- `declareVar` を使用しなかった場合と同じ文字列 return decodedGreeting; } ``` `declareVar` が影響するのはエンコードされた文字列だけなので、これはソース文字列でのみ使用することを想定しており、翻訳済みの文字列には影響しません。 **文字列処理:** `declareVar` マーカーを含む文字列を既存の文字列操作ロジックで処理する必要がある場合は、`decodeVars` を使用してください。 ## リファレンス ### パラメータ | Name | Type | Description | | ----------- | -------- | ---------------------------------------- | | `icuString` | `string` | `declareVar` の呼び出しで生成された ICU マーカーを含む文字列。 | ### 戻り値 ICUマーカーを削除し、元の変数値を含む文字列。 *** ## 例 ### 基本的な使い方 変数マーカーを含む文字列から、元の値を抽出します。 ```jsx copy import { declareVar, decodeVars } from 'gt-react-native'; const encodedVar = declareVar(name); // "{_gt_, select, other {Alice}}" const decodedVar = decodeVars(encodedVar); // "Alice" ``` ### 複数の変数 `decodeVars` は複数の変数を指定して使用できます。 ```jsx copy import { declareVar, decodeVars } from 'gt-react-native'; 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" ``` *** ## 注意 * `decodeVars` は、`declareVar` マーカーを含む文字列を処理する必要がある場合にのみ使用してください * この関数は、`declareVar` によって作成された ICU MessageFormat パターン専用です * 元の変数値は、エンコード前の状態がそのまま正確に保持されます * 翻訳処理には影響せず、文字列操作のためにマーカーを削除するだけです * decodeVars は汎用の ICU MessageFormat パーサーではないため、任意の ICU 文字列には使用しないでください ## 次のステップ * 動的なコンテンツをマークするには [`declareVar`](/docs/react-native/api/strings/declare-var) を参照してください * 静的な関数呼び出しを作成するには [`derive`](/docs/react-native/api/strings/derive) を参照してください * 文字列のエンコードとデコードの方法については [`msg`](/docs/react-native/api/strings/msg) を参照してください