auto_localize 0.0.1
auto_localize: ^0.0.1 copied to clipboard
A command-line utility that automatically scans your Flutter project to find hardcoded UI strings and replaces them with localization keys using GetX's .tr and .trParams methods.
Flutter Localization Automation Tool #
A command-line utility that automatically scans your Flutter project to find hardcoded UI strings and replaces them with localization keys using GetX's .tr and .trParams methods.
What This Tool Does #
This script automates the tedious process of manually implementing localization in Flutter projects by:
-
Scanning Dart Files: Recursively searches through your project's lib directory for hardcoded strings in UI widgets.
-
Identifying Text Patterns: Detects various text widget patterns including:
- Standard
Textwidgets - Text inside buttons and other widgets
TextSpancomponentsRichTextwidgets- Text with complex string interpolation (e.g.,
'Expires: ${DateFormat('MMM dd, yyyy').format(item.priceExpiry!)}')
- Standard
-
String Replacement:
- Converts simple strings like
Text('Hello')toText('hello'.tr) - Handles complex strings with variables using
trParamsmethod - Preserves expressions inside string interpolations
- Converts simple strings like
-
JSON Generation:
- Creates a JSON translation file with original strings as values
- Maintains placeholders for interpolated variables
-
GetX Integration:
- Adds the necessary GetX import if not present
- Removes the
constkeyword where needed for compatibility
Installation #
Add this to your pubspec.yaml:
dependencies:
auto_localize: ^0.0.1
get: ^4.6.5 # Required for the .tr and .trParams methods
Or install it from the command line:
flutter pub add auto_localize
Usage #
As a Command-Line Tool #
You can run the tool directly from the command line:
# Navigate to your Flutter project
cd path/to/your/flutter/project
# Run the localization tool
dart run auto_localize [language_code]
Or install it globally:
dart pub global activate auto_localize
Then run it from anywhere:
auto_localize [project_path] [language_code]
Programmatic Usage #
You can also use the package programmatically in your Dart code:
import 'package:auto_localize/auto_localize.dart';
Future<void> main() async {
// Run with default settings (current directory, 'en' language)
await localize();
// Or with custom settings
await localize(
projectPath: '/path/to/your/project',
lang: 'fr',
);
}
Setting Up GetX Translations #
After running the tool, you'll need to set up GetX to use the generated translations:
- Add the following code to your
main.dartfile:
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'dart:convert';
import 'dart:io';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Load translations
await loadTranslations();
runApp(MyApp());
}
Future<void> loadTranslations() async {
// Define supported languages
final languages = ['en', 'fr', 'es']; // Add your supported languages
for (final lang in languages) {
try {
final jsonString = await rootBundle.loadString('assets/lang/lang_$lang.json');
final Map<String, dynamic> translations = json.decode(jsonString);
Get.addTranslations({lang: translations});
} catch (e) {
print('Failed to load $lang translations: $e');
}
}
// Set default language
Get.locale = const Locale('en', 'US');
Get.fallbackLocale = const Locale('en', 'US');
}
- Wrap your app with
GetMaterialApp:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GetMaterialApp(
title: 'My App',
translations: GetxTranslations(), // Your translations class if needed
locale: Get.locale,
fallbackLocale: Get.fallbackLocale,
home: HomePage(),
);
}
}
Running Tests #
To run the tests for this package:
flutter test
Contributing #
Contributions are welcome! Please feel free to submit a Pull Request.