motrgem 1.3.0
motrgem: ^1.3.0 copied to clipboard
A Flutter localization tool that automatically extracts hardcoded texts from widgets and converts them to l10n format.
Motrgem Example #
This example shows how to use Motrgem to add localization to your Flutter project.
Installation #
Quick Install (Recommended) #
flutter pub add dev:motrgem
Or manually add to your pubspec.yaml:
dev_dependencies:
motrgem: ^1.1.1
Then run:
flutter pub get
Usage #
1. Initialize Your Project #
dart run motrgem start
This will:
- Add necessary dependencies to your
pubspec.yaml - Create
l10n.yamlconfiguration - Create
lib/l10ndirectory - Create initial
app_en.arbfile
2. Extract Texts (Dry Run) - Optional #
See what texts will be extracted without making changes:
dart run motrgem --dry-run
3. Extract and Replace Texts #
dart run motrgem --replace
β¨ What happens automatically:
- Extracts all hardcoded texts from your widgets
- Generates unique IDs (reuses IDs for duplicate texts)
- Creates/updates ARB file
- Replaces texts with
AppLocalizations.of(context)!.textId - Adds proper imports to modified files
- Configures MaterialApp with localization delegates π
- Runs
flutter cleanπ - Runs
flutter pub getπ - Runs
flutter gen-l10nπ
4. Add More Locales with Automatic Translation #
dart run motrgem --add-locale es
β¨ Automatic translation:
- Creates new locale file (e.g.,
app_es.arb) - Automatically translates all texts using Google Translate π
- Shows progress for each translation
- Supports 35+ languages
Example output:
π Translating texts to Spanish...
Translating (0/5): "Hello World"
Translating (1/5): "My App"
...
β
Translated 5 out of 5 texts
π Created locale file: lib/l10n/app_es.arb
Supported languages: Arabic (ar), Spanish (es), French (fr), German (de), Hindi (hi), Japanese (ja), Korean (ko), Portuguese (pt), Russian (ru), Chinese (zh), and many more!
Before and After #
Before #
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('My App'),
),
body: Center(
child: Text('Hello World'),
),
floatingActionButton: FloatingActionButton(
onPressed: () {},
tooltip: 'Add',
child: Icon(Icons.add),
),
);
}
After (Automatically Generated by Motrgem) #
import 'package:your_app/l10n/app_localizations.dart';
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(AppLocalizations.of(context)!.myApp),
),
body: Center(
child: Text(AppLocalizations.of(context)!.helloWorld),
),
floatingActionButton: FloatingActionButton(
onPressed: () {},
tooltip: AppLocalizations.of(context)!.add,
child: Icon(Icons.add),
),
);
}
Notes:
- β Import is automatically added
- β
constkeyword is automatically removed where needed - β MaterialApp is automatically configured with localization delegates
Generated ARB Files #
lib/l10n/app_en.arb (English - automatically generated):
{
"@@locale": "en",
"myApp": "My App",
"@myApp": {
"description": "Text from Text in main.dart"
},
"helloWorld": "Hello World",
"@helloWorld": {
"description": "Text from Text in main.dart"
},
"add": "Add",
"@add": {
"description": "Text from FloatingActionButton.tooltip in main.dart"
}
}
lib/l10n/app_es.arb (Spanish - automatically translated):
{
"@@locale": "es",
"myApp": "Mi aplicaciΓ³n",
"helloWorld": "Hola Mundo",
"add": "Agregar"
}
Advanced Features #
Automatic Duplicate Detection #
If you have the same text multiple times, Motrgem reuses the same ID:
// Before
Text('Hello World') // In file1.dart
Text('Hello World') // In file2.dart
// After - Both use the same ID
Text(AppLocalizations.of(context)!.helloWorld)
Text(AppLocalizations.of(context)!.helloWorld)
Output:
β»οΈ Reusing ID "helloWorld" for: "Hello World"
Change Language at Runtime #
import 'package:flutter/material.dart';
import 'package:your_app/l10n/app_localizations.dart';
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
static void setLocale(BuildContext context, Locale newLocale) {
_MyAppState? state = context.findAncestorStateOfType<_MyAppState>();
state?.setLocale(newLocale);
}
}
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(),
);
}
}
// Usage: Switch to Spanish
MyApp.setLocale(context, const Locale('es'));
What's New in v1.0.6 #
- β Fixed duplicate text entries - Same text now reuses the same ID
- β Automatic MaterialApp configuration
- β Automatic translation for new locales (35+ languages)
- β Automatic Flutter commands execution (clean, pub get, gen-l10n)
- β Smart import management with correct package paths
- β Const keyword handling
- β Visual feedback for reused IDs