motrgem 1.0.3 copy "motrgem: ^1.0.3" to clipboard
motrgem: ^1.0.3 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 #

Add this to your pubspec.yaml dev_dependencies:

dev_dependencies:
  motrgem: ^1.0.0

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. Run Flutter Pub Get #

flutter pub get

3. Extract Texts (Dry Run) #

See what texts will be extracted:

dart run motrgem --dry-run

4. Extract and Replace Texts #

dart run motrgem --replace

5. Add Localization Support to Your App #

Update your main.dart:

import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      localizationsDelegates: AppLocalizations.localizationsDelegates,
      supportedLocales: AppLocalizations.supportedLocales,
      home: const HomePage(),
    );
  }
}

6. Generate Localization Files #

flutter gen-l10n

Or simply run:

flutter pub get

7. Add More Locales #

dart run motrgem --add-locale es
dart run motrgem --add-locale fr
dart run motrgem --add-locale ar

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 #

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),
    ),
  );
}

Generated ARB File #

{
  "@@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"
  }
}
3
likes
0
points
337
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