auto_localize_cli 1.0.1
auto_localize_cli: ^1.0.1 copied to clipboard
AST-powered Flutter CLI that extracts hardcoded strings into .arb files, removes ancestor consts, and rewrites code to use context-less l10n getters.
example/lib/main.dart
import 'dart:io';
import 'package:auto_localize_cli/auto_localize_cli.dart';
/// # auto_localize_cli Example
///
/// This example demonstrates how to invoke the AutoLocalize CLI programmatically
/// or integrate the generated `localization_helper.dart` into Flutter apps.
///
/// ## 1. CLI Commands (Run in Terminal)
///
/// Scaffolds `l10n.yaml`, template ARB, and `lib/core/` helper files:
/// ```bash
/// dart run auto_localize_cli:init
/// ```
///
/// Or initialize with a custom base locale (e.g. Russian, Spanish, Bengali):
/// ```bash
/// dart run auto_localize_cli:init --locale ru
/// ```
///
/// Extract hardcoded UI strings and interpolations into `.arb` and rewrite files:
/// ```bash
/// dart run auto_localize_cli:extract
/// ```
///
/// Sub-second extraction for changed files in Git:
/// ```bash
/// dart run auto_localize_cli:extract --git-diff
/// ```
///
/// ## 2. Flutter Integration Patterns
///
/// ### Scenario A: Standard MaterialApp
/// ```dart
/// import 'package:flutter/material.dart';
/// // Generated by `dart run auto_localize_cli:init`:
/// import 'package:your_app/core/localization_helper.dart';
///
/// void main() => runApp(const MyApp());
///
/// class MyApp extends StatelessWidget {
/// const MyApp({super.key});
///
/// @override
/// Widget build(BuildContext context) {
/// return ValueListenableBuilder<Locale?>(
/// valueListenable: localeNotifier, // Live dynamic language switching
/// builder: (context, currentLocale, _) {
/// return MaterialApp(
/// navigatorKey: globalNavigatorKey, // <-- Pass key from localization_helper.dart
/// locale: currentLocale,
/// title: l10n.appTitle, // Safe pre-mount fallback!
/// localizationsDelegates: AppLocalizations.localizationsDelegates,
/// supportedLocales: AppLocalizations.supportedLocales,
/// home: const HomeScreen(),
/// );
/// },
/// );
/// }
/// }
/// ```
///
/// ### Scenario B: GoRouter Integration
/// ```dart
/// import 'package:go_router/go_router.dart';
/// import 'package:your_app/core/localization_helper.dart';
///
/// final goRouter = GoRouter(
/// navigatorKey: globalNavigatorKey, // <-- Wire GoRouter to globalNavigatorKey
/// routes: [
/// GoRoute(path: '/', builder: (context, state) => const HomeScreen()),
/// ],
/// );
/// ```
///
/// ### Scenario C: Existing rootNavigatorKey Integration
/// If your project already defines its own `rootNavigatorKey`:
/// ```dart
/// import 'package:your_app/core/localization_helper.dart';
/// import 'package:your_app/routes/app_router.dart'; // where your existing key lives
///
/// void main() {
/// // Option 1: Register your existing rootNavigatorKey with auto_localize:
/// setGlobalNavigatorKey(rootNavigatorKey);
/// runApp(const MyApp());
/// }
/// ```
void main() async {
final currentDirectory = Directory.current;
stdout.writeln('Running auto_localize_cli programmatically...\n');
// Step 1: Initialize localization setup with default or custom locale (e.g. 'en', 'ru', 'es')
final initRunner = InitRunner(
projectDir: currentDirectory,
defaultLocale: 'en',
);
await initRunner.run();
// Step 2: Extract hardcoded strings and rewrite Dart source files bottom-to-top
final extractRunner = ExtractRunner(
projectDir: currentDirectory,
);
await extractRunner.run();
}