# react-native: declareVar URL: https://generaltranslation.com/ja/docs/react-native/api/strings/declare-var.mdx --- title: declareVar description: declareVar() 文字列関数の API リファレンス --- {/* 自動生成: 直接編集しないでください。代わりに content/docs-templates/ 内の template を編集してください。 */} ## 概要 `declareVar` 関数は、`derive` で使用する際の `` コンポーネントに対応する文字列版です。 `derive` の内容に含まれる動的なコンテンツのうち、ハッシュ計算から除外し、runtime で変数として扱うべきものを示します。 ```jsx function Component() { function getGreeting(name) { return "Hello, " + declareVar(name); } // 結果: "Hello, {_gt_, select, other {Brian}}" gt(`${derive(getGreeting(name))}. How are you?`); // 結果: "Hello, Brian. How are you?" return

{message}

; } ``` `declareVar` は動的なコンテンツを ICU 互換のプレースホルダーでラップし、runtime では元の値に解決されます。 ICU マーカーを削除する場合は、[`decodeVars`](/docs/react-native/api/strings/decode-vars) を使用できます。 **ICU マーカー:** `declareVar` はソーステキストに ICU 互換のマーカーを追加します。文字列処理で元の値が必要な場合は、`decodeVars` を使用して抽出してください。 ## リファレンス ### パラメーター | 名前 | 型 | 説明 | | --------------- | ----------------------------------------------------------- | ----------------------------------------------------- | | `value` | ` value \| string \| number \| boolean\| undefined \| null` | 変数としてマークする動的な値。 | | `options?` | `Object` | `declareVar` 関数のオプション。 | | `options.$name` | `string` | 翻訳コンテキストで使用する変数名。 (`` コンポーネントの `name` prop と同様) | ### 戻り値 元の値を保持し、runtime に解決される ICU 互換マーカーを含む文字列。 *** ## 動作 ### 変数のマーキング `declareVar` で値をラップすると、次のように処理されます。 1. 値を ICU の `select` 文形式に変換します 2. 翻訳処理用の動的コンテンツとしてマークします 3. 翻訳ハッシュの計算対象から除外します 4. runtime で補間できるよう、元の値を保持します ### ICU 形式 この関数は、ICU MessageFormat に対応した文字列を出力します: ```jsx declareVar("John") // → "{_gt_, select, other {John}}" ``` *** ## 例 ### 基本的な使い方 静的関数内で動的なコンテンツをマークします。 ```jsx copy import { derive, declareVar, gt } from 'gt-react-native'; function getGreeting(name) { return `Hello, ${declareVar(name)}!`; } function Component() { const name = "Brian"; const message = gt(`${derive(getGreeting(name))} Welcome back.`); return

{message}

; } ``` ### 他の ICU 関数と組み合わせる `declareVar` は、他の ICU 関数と組み合わせて、より複雑な文字列を作成できます。 ```jsx copy import { declareVar, derive, useGT } from 'gt-react-native'; function Component({ name1, name2 }) { const gt = useGT(); const message = gt("Hello, {name1}! My name is " + derive(declareVar(name2)), { name1 }); return

{message}

; // 結果: "Hello, Brian! My name is Archie" } ``` *** ## 注意事項 * `declareVar` は、`derive` から呼び出される関数内でのみ使用してください * この関数は、文字列処理に干渉する可能性のある ICU マーカーを追加します * 必要に応じて、元の値を取り出すには `decodeVars` を使用してください * 変数は翻訳対象から除外され、runtime でも保持されます ## 次のステップ * 文字列内に静的な関数呼び出しを作成するには、[`derive`](/docs/react-native/api/strings/derive)を参照してください * 元の値を抽出するには、[`decodeVars`](/docs/react-native/api/strings/decode-vars)を参照してください * JSX での対応については、[``](/docs/react-native/api/components/var)を参照してください