Generate
The generate command is a powerful tool for creating type-safe, feature-rich GetX locale translations from JSON files. It automatically generates Dart translation files with advanced features like variable replacement, smart pluralization, and compile-time safety through generated constants.
📝 Description
Generate GetX locale translations from JSON files with advanced features:
🔑 Type-safe LocaleKeys - Compile-time safety with generated constants
🔗 Variable replacement - Dynamic content with
trVars🔢 Smart pluralization - Universal language support with
trCount🌐 Multi-language support - Arabic, English, and 100+ languages
⚡ Hot reload friendly - Real-time translation updates
⚙️ Usage
gexd locale generate [source_path] [options]Quick Start Examples
# Basic generation (default settings)
gexd locale generate assets/locales
# With sorted keys for better organization
gexd locale generate assets/locales --sort-keys
# Specify custom key style
gexd locale generate assets/locales --key-style dot --sort-keys
# Interactive mode
gexd locale generate� Detailed Usage
Command Syntax
Input Structure
Your locale files should follow this structure:
Output Structure
The command generates a single translations file based on your project template:
GetX Template:
Clean Template:
�🚩 Options & Flags
--sort-keys
--sort-keysDescription: Sort translation keys alphabetically in generated files
Benefits:
Better code organization and readability
Easier to find specific keys in large projects
Consistent ordering across team members
Improved version control diffs
Example:
--key-style
--key-styleDescription: Define naming style for generated LocaleKeys constants
Available Styles:
dot- Convert dots to underscores:app.name→app_namecamel- CamelCase style:app.name→appNamesnake- Snake case style:app.name→app_name
Example:
🌟 Advanced Features Generated
1. 🔑 Type-Safe LocaleKeys Class
The command automatically generates a LocaleKeys class with constants for all translation keys:
Input JSON:
Generated LocaleKeys:
Usage Benefits:
2. 🔗 Variable Replacement with trVars
trVarsJSON Definition:
Flutter Usage:
3. 🔢 Smart Pluralization with trCount
trCountEnglish Pluralization:
Arabic Advanced Pluralization:
Flutter Usage:
📁 Example Project Structure
Input Files
en_US.json:
ar_SA.json:
🚀 Real-World Usage Examples
Variable Syntax Options
GetX supports two variable replacement syntaxes in your JSON files:
1. Curly Brace Syntax {variable} (Recommended)
2. GetX @ Syntax @variable
Usage in Dart (Both syntaxes work identically):
Shopping Cart Widget
Form Validation
💡 Best Practices
✅ Translation File Organization
✅ Pluralization Guidelines
Always provide
otheras fallbackUse language-specific plural rules
Test with edge cases (0, 1, 2, large numbers)
Include variables in plural forms when needed
✅ Variable Naming
Use descriptive names:
{userName}not{a}Be consistent across languages
Document expected variables in code comments
✅ LocaleKeys Usage
🔧 Integration with GetX
Setup in main.dart
Dynamic Language Switching
🐛 Troubleshooting
Common Issues
Issue: LocaleKeys class not generated Solution: Ensure JSON files are valid and run the command again
Issue: Variables not working
Solution: Check variable syntax {variableName} in JSON
Issue: Pluralization not working
Solution: Verify __count structure in JSON files
Issue: Translations not updating Solution: Hot restart the app after generating new translations
Validation Tips
Validate JSON syntax before generation
Test all plural forms in your target languages
Verify variable names match between JSON and code
Check that all required languages have the same keys
📚 Related Resources
LOCALE_FEATURES.md - Advanced usage examples
GetX Documentation - GetX internationalization
Flutter Intl - Flutter internationalization guide
Generated automatically by gexd_doc | Enhanced with comprehensive examples and best practices
Last updated