# gt-next: General Translation Next.js SDK: 分岐コンポーネント URL: https://generaltranslation.com/ja/docs/next/guides/branches.mdx --- title: 分岐コンポーネント description: 翻訳内で条件付きコンテンツに分岐コンポーネントを使用する方法 --- {/* 自動生成: 直接編集せず、代わりに content/docs-templates/ 内の template を編集してください。 */} 分岐コンポーネントを使うと、[``](/docs/next/api/components/t) コンポーネント内で条件に応じたコンテンツをレンダリングできます。if/else 文や複数形のルールといった動的ロジックを扱いながら、あらゆるコンテンツのバリエーションを適切に翻訳できるようにします。 ## 使用可能なコンポーネント * [``](/docs/next/api/components/branch): 値や状態に応じて表示を切り替える条件付きコンテンツ * [``](/docs/next/api/components/plural): ロケールごとのルールに基づく自動複数形処理 ## クイックスタート 分岐コンポーネントは [``](/docs/next/api/components/t) 内で使用でき、条件分岐を扱えます。 ```jsx import { T, Branch, Plural, Num, Var } from 'gt-next'; 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/next/api/components/t) 内の**三項演算子**や条件ロジックを**置き換える** 2. 条件が想定した値に一致しない場合に**フォールバックコンテンツ**を提供する 3. あり得るすべてのコンテンツバリエーションを**翻訳可能にする** 4. 複数形の扱いで**ロケールのルール**に自動的に従う ```jsx // ❌ これは動作しない - 内の条件ロジック

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

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

} false={

User is inactive

} />
``` ## コンポーネントガイド ### Branch - 条件付きコンテンツ 値や状態に応じて条件付きでレンダリングするには、[``](/docs/next/api/components/branch) を使用します。 ```jsx // ユーザーステータスの表示 管理者ダッシュボード

} user={

ユーザーダッシュボード

} guest={

ゲストアクセス

} >

アクセスレベル不明

// 真偽値による条件分岐 おかえりなさい!

} false={

ログインしてください

} />
// サブスクリプションプラン プレミアム機能を利用するにはアップグレードしてください

} premium={

プレミアム体験をお楽しみください

} enterprise={

エンタープライズソリューションについてはサポートにお問い合わせください

} >

サブスクリプションのステータスを取得できません

``` ### 複数形 - スマートな複数形処理 数量に応じて内容が変わる場合は、[``](/docs/next/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/next/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/next/api/components/t) の外側でも使用できます。 ```jsx // 純粋な条件付きレンダリング } light={} > // 純粋な複数形処理 } other={} /> ``` ## よくある問題 ### branchキーの不足 一致する値がない場合に備えて、常にフォールバックコンテンツを指定してください。 ```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/next/guides/strings) - JSX を使わずにプレーンテキストを翻訳する * [動的コンテンツガイド](/docs/key-concepts/dynamic-content) - runtime 翻訳に対応する * API リファレンス: * [`` コンポーネント](/docs/next/api/components/branch)