mayr_i18n_generation 1.0.0 copy "mayr_i18n_generation: ^1.0.0" to clipboard
mayr_i18n_generation: ^1.0.0 copied to clipboard

Provide automatic type-safe translation key generation for projects using mayr_i18n.

License Platform

Pub Version Pub.dev Score Pub Likes Pub.dev Publisher Downloads

Build Status Issues Last Commit Contributors

🌍 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 configured output_file) with the part directive
  • lib/translations.g.dart with 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_file configuration from mayr_i18n_generation section in pubspec.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.dart file

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 LanguageSwitcher widget

πŸ§‘β€πŸ’» 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! πŸš€πŸ’™

0
likes
150
points
41
downloads

Publisher

verified publishermayrlabs.com

Weekly Downloads

Provide automatic type-safe translation key generation for projects using mayr_i18n.

Repository (GitHub)
View/report issues
Contributing

Topics

#i18n #dart #json-translation #productivity #development

Documentation

Documentation
API reference

License

MIT (license)

Dependencies

analyzer, args, build, glob, path, source_gen, yaml

More

Packages that depend on mayr_i18n_generation