# gt-react: General Translation React SDK: 分岐コンポーネント URL: https://generaltranslation.com/ja/docs/react/guides/branches.mdx --- title: 分岐コンポーネント description: 翻訳内で条件付きコンテンツを扱うための分岐コンポーネントの使い方 --- {/* 自動生成: 直接編集せず、代わりに content/docs-templates/ 内のテンプレートを編集してください。 */} 分岐コンポーネントを使うと、[``](/docs/react/api/components/t) コンポーネント内で条件に応じたコンテンツのレンダリングが可能になります。if/else 文や複数形処理のルールといった動的なロジックを扱いつつ、どのコンテンツのバリエーションも適切に翻訳できるようにします. ## 利用可能なコンポーネント * [``](/docs/react/api/components/branch): 値や状態に応じてコンテンツを切り替え * [``](/docs/react/api/components/plural): ロケール固有のルールに基づく複数形の自動処理 ## クイックスタート 分岐コンポーネントは [``](/docs/react/api/components/t) 内で使用でき、条件分岐を処理できます。 ```jsx import { T, Branch, Plural, Num, Var } from 'gt-react'; function NotificationPanel({ user, messageCount }) { return ( {user.name} is currently online

} away={

{user.name} is away

} >

{user.name} status unknown

You have {messageCount} message

} other={

You have {messageCount} messages

} />
); } ``` ## 分岐コンポーネントの仕組み 分岐コンポーネントは、翻訳内の条件付きレンダリングを次のように処理します。 1. [``](/docs/react/api/components/t) 内の**三項演算子**や条件ロジックを**置き換える** 2. 条件が想定どおりの値に一致しない場合に**フォールバックコンテンツ**を提供する 3. 考えられるすべてのコンテンツバリエーションを**翻訳可能**にする 4. 複数形の処理で**ロケールのルール**に自動的に従う ```jsx // ❌ これは動作しない - 内の条件ロジック

{isActive ? 'User is active' : 'User is inactive'}

// ✅ これは動作する - 分岐を使った条件ロジック User is active

} false={

User is inactive

} />
``` ## コンポーネントガイド ### Branch - 条件付きコンテンツ 値や状態に応じた条件付きレンダリングには、[``](/docs/react/api/components/branch) を使用します。 ```jsx // ユーザーステータスの表示 Administrator Dashboard

} user={

User Dashboard

} guest={

Guest Access

} >

Access level unknown

// 真偽値による条件分岐 Welcome back!

} false={

Please log in

} />
// サブスクリプションプラン Upgrade to unlock premium features

} premium={

Enjoy your premium experience

} enterprise={

Contact support for enterprise solutions

} >

Subscription status unavailable

``` ### 複数形 - スマートな複数形処理 数に応じて変化する内容には、[``](/docs/react/api/components/plural) を使用します: ```jsx // 基本的な複数形処理 {itemCount} item in cart

} other={

{itemCount} items in cart

} />
// ゼロの処理 No new notifications

} one={

{notifications} notification

} other={

{notifications} notifications

} />
// 複雑な複数形処理(Unicode CLDRルールに従う) Due today

} one={

Due in {days} day

} few={

Due in {days} days

} many={

Due in {days} days

} other={

Due in {days} days

} />
``` ### 変数コンポーネントと組み合わせる 分岐と変数コンポーネントはスムーズに連携します。 ```jsx Order {order.id} is pending. Total: {order.total}

} shipped={

Order {order.id} shipped on {order.shippedDate}

} delivered={

Order {order.id} was delivered successfully

} >

Order status unknown

``` ## 分岐コンポーネントを使うべき場面 ### 三項演算子を置き換える 条件分岐を[``](/docs/react/api/components/t)内で使える形に変換します: ```jsx // ❌ 内では三項演算子を使用できません {isActive ?

Active user

:

Inactive user

}
// ✅ 代わりに Branch を使用してください Active user

} false={

Inactive user

} />
``` ### 複数の条件に対応する switch 文や複数の if/else 分岐を置き換えます。 ```jsx // ❌ 複雑な条件分岐ロジック {status === 'loading' ?

Loading...

: status === 'error' ?

Error occurred

: status === 'success' ?

Success!

:

Unknown status

}
// ✅ すっきりした分岐ロジック Loading...

} error={

Error occurred

} success={

Success!

} >

Unknown status

``` ### 複数形のルール 手動での複数形処理を置き換えます: ```jsx // ❌ 手動の複数形処理 {count === 1 ?

1 item

:

{count} items

}
// ✅ 自動の複数形処理 {count} item

} other={

{count} items

} />
``` ## スタンドアロンでの使用 分岐コンポーネントは、翻訳を行わない純粋なロジックであれば [``](/docs/react/api/components/t) の外でも使用できます。 ```jsx // 純粋な条件付きレンダリング } light={} > // 純粋な複数形処理 } other={} /> ``` ## よくある問題 ### 不足している分岐キー 一致しない値に対するフォールバックコンテンツを、必ず指定してください。 ```jsx // ❌ 予期しない値に対するフォールバックなし } user={} // userRole が "moderator" の場合はどうなる? /> // ✅ 常にフォールバックを含める } user={} > {/* その他の値に対するフォールバック */} ``` ### 複数形が不足している場合 デフォルトのロケールに必要な複数形を用意してください。 ```jsx // ❌ "other" 形式が不足しています 1 item

} // 0、2、3 などの場合は? /> // ✅ 必要な形式を含める No items

} one={

1 item

} other={

{count} items

} /> ``` ### 複雑にネストしたロジック これは機能しますが、分岐ロジックはシンプルに保ち、深いネストは避けることをおすすめします。 ```jsx // ❌ 複雑なネストされた分岐 {/* 読みにくく保守が困難 */} // ✅ ロジックをフラット化するか、複数のコンポーネントを使用する } active-offline={} inactive-online={} > ``` 複数形処理のルールについて詳しくは、[Unicode CLDR ドキュメント](https://cldr.unicode.org/index/cldr-spec/plural-rules)を参照してください。 ## 次のステップ * [文字列翻訳ガイド](/docs/react/guides/strings) - JSX を使わずにプレーンテキストを翻訳する * [動的コンテンツガイド](/docs/key-concepts/dynamic-content) - runtime 翻訳を扱う * API リファレンス: * [`` コンポーネント](/docs/react/api/components/branch)