🚀 Why Use Motrgem?
Manually finding and converting all hardcoded text into localization keys can be tedious. Motrgem does this for you automatically by:
Scanning your Flutter project for text in widgets like Text, AppBar, Button, Tooltip, etc.
Generating unique, camelCase localization keys (e.g., "Hello World" → helloWorld)
Updating .arb files with extracted texts
Replacing hardcoded strings in your Dart code
Managing localization imports and multi-language support
Step-by-Step Guide
1. Install Motrgem
Run inside your project root:
flutter pub add dev:motrgem
dart pub global activate motrgem
This adds motrgem to dev_dependencies and runs flutter pub get.
2. Initialize L10n in your project (one-time setup)
Run (use dart run if Motrgem is a dev dependency):
dart run motrgem start
What this does:
Adds required localization dependencies (flutter_localizations, intl, analyzer, etc.) to pubspec.yaml.
Creates l10n.yaml config and lib/l10n/ directory.
Generates an initial app_en.arb and enables flutter: generate: true.
3. Dry run (preview what will change)
Before modifying files, preview extraction:
dart run motrgem --dry-run
This prints what strings would be extracted and where - no code changes. Use this to review and avoid surprises.
4. Extract texts and replace hardcoded strings
When ready to apply changes:
dart run motrgem --replace
flutter clean
flutter pub get
What happens:
Motrgem scans source (using Dart analyzer), generates camelCase IDs and ARB entries, updates lib/l10n/app_en.arb, and replaces hardcoded strings in your Dart files with AppLocalizations references (e.g. Text('Hi') → Text(AppLocalizations.of(context)!.hi)).
5. Important:
After replacement, remove const from widgets that use AppLocalizations (e.g. const Text(AppLocalizations.of(context)!.helloWorld) should become Text(AppLocalizations.of(context)!.helloWorld)). Motrgem cannot keep them const.
6. Add additional locales
Create new locale ARB files (example: Spanish, French, Arabic):
dart run motrgem --add-locale es
dart run motrgem --add-locale fr
dart run motrgem --add-locale ar
This creates lib/l10n/app_es.arb, app_fr.arb etc., with auto transilation for the main file in english app_en.arb.
**
🪄 Step 7 - Enable Language Switching at Runtime
**
7.1 Add setLocale in main.dart to change language from anywhere:
class MyApp extends StatefulWidget { const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
// Helper to change language from anywhere
static void setLocale(BuildContext context, Locale newLocale) {
_MyAppState? state = context.findAncestorStateOfType<_MyAppState>();
state?.setLocale(newLocale);
}
}
7.2 Add locale handling inside _MyAppStat in main.dart
class _MyAppState extends State<MyApp> {
Locale? _locale;
void setLocale(Locale locale) {
setState(() => _locale = locale);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
locale: _locale,
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
home: const HomePage(),
);
}
}
7.3 Switch language anywhere in your app
Call the helper method when the user chooses a new language:
MyApp.setLocale(context, const Locale('es')); // Spanish
MyApp.setLocale(context, const Locale('ar')); // Arabic
MyApp.setLocale(context, const Locale('en')); // English
Motrgem is open-source under the MIT License.
Learn more: pub.dev/packages/motrgem
Githup: https://github.com/mohammadabusalh1/motrgem
Thanks 🙏
Mohammad Abu Salh
abusalhm102@gmail.com
https://www.linkedin.com/in/abusalh
Top comments (0)