π mayr_i18n
Simple, JSON-controlled internationalization for Dart projects β¨ Easy to set up β’ π§© CLI powered β’ π Key-safe β’ β‘ Extensible
π§ Overview
mayr_i18n makes localization dead simple.
It lets you manage your translations in plain JSON files, verify and sync them via CLI commands, and translate strings dynamically with '.tr()'.
Itβs the perfect foundation for both Dart and Flutter apps, with a Flutter wrapper (mayr_i18n_flutter) coming soon.
π Features
β
Supports nested JSON
β
Works with non-standard languages like genz.json or pidgin.json
β
Built-in key verification, sync, audit, and file creation tools
β
.tr() translation lookup with variable interpolation
β
Configurable via pubspec.yaml or .env
β
Lightweight, no Flutter dependency
β
Extensible architecture β code-gen and pluralization ready
π¦ Installation
dart pub add mayr_i18n
βοΈ Configuration
In your projectβs pubspec.yaml, add:
mayr_i18n:
lang_directory: assets/i18n
default_locale: en
env_support: true
Optional .env overrides:
MAYR_I18N_DEFAULT_LOCALE=en
MAYR_I18N_LANG_DIRECTORY=assets/i18n
Then place your translation files here:
assets/i18n/
βββ en.json
βββ fr.json
βββ genz.json
π§© Example JSON
assets/i18n/en.json
{
"app": {
"welcome": "Welcome, {name}!",
"logout": "Youβve been logged out."
}
}
assets/i18n/genz.json
{
"app": {
"welcome": "Yo {name}, whatβs good?",
"logout": "Catch you later!"
}
}
π§ Usage
import 'package:mayr_i18n/mayr_i18n.dart';
Future<void> main() async {
await MayrI18n.instance.load(); // Loads from config
print('app.welcome'.tr(args: {'name': 'Mayor'}));
// Output β Welcome, Mayor!
MayrI18n.instance.changeLanguage('genz');
print('app.welcome'.tr(args: {'name': 'Mayor'}));
// Output β Yo Mayor, whatβs good?
}
π‘ String extension
'app.logout'.tr(); // simple key lookup
'user.greeting'.tr(args: {'name': 'Tovia'});
π CLI Commands
Run all commands using:
dart run mayr_i18n <command>
β
verify
Checks that all JSON files contain the same keys.
dart run mayr_i18n verify
Output:
β οΈ Missing key in fr.json: app.logout
β
All keys verified in en.json, genz.json
π sync
Adds any missing keys across language files (with empty placeholders).
dart run mayr_i18n sync
Output:
π§© Synced 3 missing keys in fr.json
π§Ή audit
Scans your codebase for translation key usage.
It shows:
- Keys used but not defined
- Keys defined but unused
dart run mayr_i18n audit
Output:
β Missing keys: profile.title
π§© Unused keys: app.oldFeature
β create <lang>
Create a new translation file quickly.
dart run mayr_i18n create pidgin
Output:
β
Created assets/i18n/pidgin.json
π§© Nested Keys
Supports deeply nested JSON structures:
{
"auth": {
"errors": {
"invalid": "Invalid credentials"
}
}
}
Usage:
'auth.errors.invalid'.tr();
π§ Language Switching
MayrI18n.instance.changeLanguage('genz');
π§° CLI Tools
| Command | Description |
|---|---|
verify |
Check missing keys |
sync |
Add missing keys to files |
audit |
Detect unused/missing keys in code |
create <lang> |
Create a new language JSON |
π§ Example Project Structure
lib/
main.dart
assets/
i18n/
en.json
fr.json
pubspec.yaml
.env
π§© Coming Soon: mayr_i18n_flutter
The Flutter version will add:
MayrI18nWidgetfor wrappingMaterialApp- Automatic reactive rebuild on language change
- Persistent language setting with
SharedPreferences - Built-in
LanguageSwitcherwidget
π§± Internal Architecture (Quick Summary)
| Component | Role |
|---|---|
| MayrI18n | Singleton managing current locale and translations |
| StringExtension | Adds .tr() method |
| CLI Commands | Verify, sync, audit, create |
| Config Reader | Parses pubspec.yaml and .env |
| JSON Loader | Loads and flattens translation trees |
π§ Example Output
π mayr_i18n v1.0.0
Running verify...
β
All good β 3 files, 124 keys verified.
Tip: Run `mayr_i18n sync` to auto-fix missing keys.
π‘ Pro Tip
Run all i18n tasks automatically before release:
dart run mayr_i18n verify && dart run mayr_i18n audit
π’ Additional Information
π€ Contributing
Contributions are highly welcome! If you have ideas for new extensions, improvements, or fixes, feel free to fork the repository and submit a pull request.
Please make sure to:
- Follow the existing coding style.
- Write tests for new features.
- Update documentation if necessary.
Let's build something amazing together!
π Reporting Issues
If you encounter a bug, unexpected behaviour, or have feature requests:
- Open an issue on the repository.
- Provide a clear description and steps to reproduce (if it's a bug).
- Suggest improvements if you have any ideas.
Your feedback helps make the package better for everyone!
π§βπ» 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
- A Dart-based, JSON-powered internationalization (i18n) library.