motrgem 1.1.2 copy "motrgem: ^1.1.2" to clipboard
motrgem: ^1.1.2 copied to clipboard

A Flutter localization tool that automatically extracts hardcoded texts from widgets and converts them to l10n format.

example/example.md

Motrgem Example #

This example shows how to use Motrgem to add localization to your Flutter project.

Installation #

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.yaml configuration
  • Create lib/l10n directory
  • Create initial app_en.arb file

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
  • βœ… const keyword 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
3
likes
0
points
401
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter localization tool that automatically extracts hardcoded texts from widgets and converts them to l10n format.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

analyzer, args, path, translator, yaml

More

Packages that depend on motrgem