π mayr_i18n_generator
Code generation for mayr_i18n
mayr_i18n_generator provides automatic type-safe translation key generation for projects using mayr_i18n.
It scans your JSON language files and generates Dart classes with structured translation keys and autocompletion support β making localization clean, reliable, and developer-friendly.
π Installation
Add the packages to your pubspec.yaml:
dependencies:
mayr_i18n: ^1.0.0
dev_dependencies:
mayr_i18n_generator: ^1.0.0
βοΈ Configuration
In your root pubspec.yaml, configure your translation settings under mayr_i18n:
mayr_i18n:
lang_directory: assets/lang # Directory containing JSON language files
default_locale: en # Default locale key
mayr_i18n_generation:
output_file: lib/translations.dart # File to generate code for (default: lib/generated/translations.dart)
Note: The output_file configuration specifies which file the generator should process. This prevents conflicts with other code generators (like json_serializable, freezed, etc.) that also create .g.dart files. If not specified, it defaults to lib/generated/translations.dart.
Example directory structure:
assets/
lang/
en.json
fr.json
genz.json
lib/
translations.dart # Your file with part directive
translations.g.dart # Generated by the CLI
π§ Example
1οΈβ£ Define your translation JSON
{
"app": {
"welcome": "Welcome {name}",
"logout": "Goodbye"
},
"errors": {
"network": "No internet connection"
}
}
2οΈβ£ Run code generation
The CLI command will automatically create the necessary files:
dart run mayr_i18n_generator:generate
This creates:
lib/translations.dart(or your configuredoutput_file) with the part directivelib/translations.g.dartwith the generated translation keys
You don't need to manually create the filesβthe generator does it for you!
3οΈβ£ Use the generated keys
After generation, use the type-safe keys in your code:
// Access the generated keys
final greeting = I18nKeys.app.welcome.tr(args: {'name': 'Mayor'});
print(greeting); // "Welcome Mayor"
Or access by string:
print('app.welcome'.tr()); // "Goodbye"
π§ Manual Code Generation
Generate code using the CLI command:
dart run mayr_i18n_generator:generate
This will:
- Read the
output_fileconfiguration frommayr_i18n_generationsection inpubspec.yaml - Create the output file (if it doesn't exist) with the necessary part directive
- Read your JSON translation files
- Generate type-safe translation key classes in the
.g.dartfile
The command will create the file at the configured path (default: lib/generated/translations.dart) along with its generated counterpart (translations.g.dart).
Note: Additional CLI commands for verification, syncing, and auditing translations are available in the mayr_i18n package.
π§© Configuration recap
No build system needed. Use the CLI to generate code on demand.
π‘ Advanced
Code generation
Run:
dart run mayr_i18n_generator:generate
Supports Nested JSON
{
"profile": {
"name": "Name",
"details": {
"age": "Age",
"email": "Email"
}
}
}
Accessed as:
I18nKeys.profile.details.email.tr();
Class Naming
To avoid naming conflicts, nested classes are named using their full path:
// profile.details β _ProfileDetailsKeys
class _ProfileDetailsKeys {
const _ProfileDetailsKeys._();
final String age = 'profile.details.age';
final String email = 'profile.details.email';
}
// user.details β _UserDetailsKeys (different class!)
class _UserDetailsKeys {
const _UserDetailsKeys._();
final String name = 'user.details.name';
}
This ensures that even if multiple keys have the same nested structure (e.g., profile.details and user.details), they generate unique class names and won't conflict.
π§± Output Example
I18nKeys.app.welcome.tr(args: {'name': 'Tovia'});
I18nKeys.errors.network.tr();
π§© Integration with Flutter
When using the Flutter wrapper (mayr_i18n_flutter), youβll also get:
- Reactive locale switching
- Persistent locale preferences
- Built-in
LanguageSwitcherwidget
π§βπ» Author
MayR Labs
Crafting clean, reliable, and human-centric Flutter and Dart solutions. π mayrlabs.com
π Licence
This package is licensed under the MIT License β which means you are free to use it for commercial and non-commercial projects, with proper attribution.
See the LICENSE file for more details.
MIT Β© 2025 MayR Labs
π Support
If you find this package helpful, please consider giving it a βοΈ on GitHub β it motivates and helps the project grow!
You can also support by:
- Sharing the package with your friends, colleagues, and tech communities.
- Using it in your projects and giving feedback.
- Contributing new ideas, features, or improvements.
Every little bit of support counts! ππ
Libraries
- mayr_i18n_generation
- Support for generating type-safe i18n translation keys.