Branching Components
Conditional behavior with branching components in dictionary entries
Overview
Branching components in gt-next
enable dynamic, conditional rendering of translations based on specific criteria or values.
When combined with dictionaries,
these components provide a powerful way to handle content variations and localization logic directly in your dictionary entries.
Branching components include:
<Branch>
: Dynamically renders a specific branch of content based on a branch value.<Plural>
: Handles pluralization rules based on a numerical value (n
).
Branching components are processed locally, ensuring that all logic and conditional rendering happens on the client or server without requiring additional translation API calls.
Why Use Branching Components with Dictionaries?
By using branching components in dictionaries, you can:
- Implement conditional logic directly in your dictionary entries.
- Simplify the management of localized content variations.
- Automatically adapt translations to complex scenarios such as pluralization or application states.
This approach eliminates the need for external conditional logic, consolidating both content and logic into the dictionary itself.
Adding Branching Components to Dictionaries
Branching components can be included in dictionary entries to manage variations in content dynamically.
Example: Adding Branches to a Dictionary
Below is an example of how you can use <Branch>
and <Plural>
components in a dictionary.
Accessing Branches
When accessing dictionary entries that use branching components, you must pass the relevant dynamic data (e.g., branch
value or n
)
through the options parameter. This can be thought of keys that map to a value that will be rendered.
Example: Client-Side Usage with useGT()
Example: Server-Side Usage with getGT()
Best Practices
Define Clear Branch Keys
When using <Branch>
, define clear and meaningful branch keys that align with your application's state or logic.
Include a Fallback
Always include a fallback value in <Branch>
and <Plural>
components to ensure graceful handling of unexpected conditions or values.
Common Pitfalls
Missing Dynamic Values
If dynamic values (e.g., branch or n) are not provided, the Branching Component may not render the expected content. Always ensure that the necessary options are passed when accessing dictionary entries.
Overloading Dictionary Entries
Avoid combining too much logic or complexity into a single dictionary entry. Break down entries into smaller, manageable components to maintain clarity and flexibility.
Notes
- Branching Components handle conditional logic locally, ensuring efficient and secure translations.
- Combine Branching Components with Variable Components for more complex scenarios involving dynamic content.
- The
<Plural>
component follows Unicode CLDR pluralization rules for accurate localization.
Next Steps
- Explore the API Reference for
Branching Components
. - Learn about Unicode CLDR pluralization rules rules for pluralization support.
- Review the Template Dictionary Design Pattern for creating scalable and maintainable translations.