mayr_i18n_generation 1.0.0
mayr_i18n_generation: ^1.0.0 copied to clipboard
Provide automatic type-safe translation key generation for projects using mayr_i18n.
1.0.0 #
π Initial Release #
A complete Dart code generation package for type-safe i18n translation keys.
Features
- β¨ Type-safe translation keys - Generate Dart classes from JSON translation files
- ποΈ build_runner integration - Automatic code generation with build_runner
- π§ CLI tool for manual code generation:
generate- Automatically creates output file and generates translation keys
- π¦ Nested JSON support - Handle deeply nested translation structures
- π Multi-locale support - Manage multiple language files easily
- π― Targeted generation - Configure which file to process via
output_fileto avoid conflicts with other generators - π Automatic file creation - CLI command creates the output file with proper structure if it doesn't exist
- π Unique class names - Uses full path for nested class names to prevent conflicts (e.g.,
_ProfileDetailsKeys,_UserDetailsKeys) - π Comprehensive documentation - Full API docs and examples
- β Well-tested - Extensive test coverage for all components
Note: Commands for verifying, syncing, and auditing translations are available in the mayr_i18n package.
Configuration
mayr_i18n:
lang_directory: assets/lang
default_locale: en
mayr_i18n_generation:
output_file: lib/translations.dart # Default: lib/generated/translations.dart
The output_file configuration specifies which file to create and process. The CLI command will automatically create this file (with the necessary part directive) if it doesn't exist, then generate the .g.dart file with translation keys.
Core Components
I18nConfig- Read and manage i18n configuration from pubspec.yamlGeneratorConfig- Read generator-specific settings (output_file)JsonParser- Parse and validate JSON translation filesKeyTreeBuilder- Build hierarchical key structuresCodeEmitter- Generate clean, readable Dart codeI18nGenerator- build_runner integration for automatic generation
Example Usage
// Define translations in JSON
{
"app": {
"welcome": "Welcome {name}!",
"logout": "Goodbye"
}
}
// Access with type-safe keys
I18nKeys.app.welcome.tr(args: {'name': 'John'});
See the README.md for complete documentation and example/ for working examples.