flutter_hardcode_localizer 1.0.10
flutter_hardcode_localizer: ^1.0.10 copied to clipboard
A Dart plugin that finds hardcoded strings in Flutter projects and provides quick-fix to move them to localization files for easy_localization.
Flutter Hardcode Localizer CLI Utility #
π Enhanced for easy_localization - Automate the tedious process of manually adding JSON key-values for Flutter localization by CLI Utility!
π― Perfect Companion for easy_localization Package #
This tool eliminates the manual process of:
- β Manually creating JSON key-value pairs
- β Manually replacing strings in code
- β Manually running code generation
- β Manual import management
Instead, it automates everything:
- β Finds hardcoded strings in your Flutter project
- β Automatically adds them to en.json with smart key generation
- β Replaces strings with LocaleKeys.key.tr() format
- β Works with arrays, methods, constructors, maps seamlessly
- β Skips every type of import statements, assertions and annotations
π What It Does #
Before (Manual Process): #
// 1. You have hardcoded strings
Text('Hello World')
final items = ['Save', 'Cancel', 'Delete'];
// 2. You manually add to assets/languages/en.json:
{
"helloWorld": "Hello World",
"save": "Save",
"cancel": "Cancel",
"delete": "Delete"
}
// 3. You manually replace in code:
Text(LocaleKeys.helloWorld.tr())
final items = [LocaleKeys.save.tr(), LocaleKeys.cancel.tr(), LocaleKeys.delete.tr()];
// 4. You manually run: flutter packages pub run easy_localization:generate
After (Automated with running CLI utility): #
# Just run this tool:
dart run flutter_hardcode_localizer:localize
# It automatically:
# β
Finds all hardcoded strings
# β
Adds them to assets/languages/en.json
# β
Replaces with LocaleKeys.key.tr()
# β
Shows you next steps
π¨ What Gets Generated #
β Arrays/Lists #
// Before
final items = ['Hello', 'World', 'Flutter'];
// After
final items = [LocaleKeys.hello.tr(), LocaleKeys.world.tr(), LocaleKeys.flutter.tr()];
β Method Arguments #
// Before
Text('Click me')
ElevatedButton(child: Text('Submit'))
// After
Text(LocaleKeys.clickMe.tr())
ElevatedButton(child: Text(LocaleKeys.submit.tr()))
β Constructor Parameters #
// Before
MaterialApp(
title: 'My App',
home: Scaffold(...)
)
// After
MaterialApp(
title: LocaleKeys.myApp.tr(),
home: Scaffold(...)
)
π Installation & Usage #
Prerequisites #
Make sure you have easy_localization already set up in your project.
Install Tool #
dev_dependencies:
flutter_hardcode_localizer: ^latest_version
Run Tool #
dart run flutter_hardcode_localizer:localize \
--targetPath example \
--targetFiles lib/main.dart,lib/widgets/sample_widget.dart \
--skipFiles lib/widgets/skip_file_1.dart,lib/widgets/skip_file_2.dart \
--autoApproveSuggestedKeys true \
--prefix prefixText
--targetPath
(Optional) Path to your projectβs target directory.
(Default) '.'
--targetFiles
(Optional) Comma-separated list of full file paths to process, rest of the files will be skipped.
(Default) []
--autoApproveSuggestedKeys
(Optional, true/false) Automatically approve suggested JSON keys without developer consent.
(Default) false
--skipFiles
(Optional) Comma-separated list of file paths to skip during processing.
(Default) [lib/ui/theme/codegen_key.g.dart]
--prefix
(Optional) Add [a-z]{2,6} prefix to every json keys.
(Default) ''
π Complete easy_localization Workflow #
-
Setup easy_localization in your project (if not already done)
-
Add assets directory to pubspec.yaml:
flutter:
assets:
- assets/languages/
-
Run this tool:
dart run flutter_hardcode_localizer:localize -
Generate LocaleKeys: Use the following command to generate LocaleKeys class:
dart run easy_localization:generate --source-dir assets/languages/ -f keys -O lib/ui/theme -o codegen_key.g.dart
-
Add import:
import "lib/ui/theme/codegen_key.g.dart"; -
Test your app - all strings now use proper localization!
π Important Notes:
- The tool creates JSON files in
assets/languages/directory- You must add
assets/languages/to yourpubspec.yamlunder flutter > assets- The generation command parameters:
--source-dir assets/languages/- Specifies where your translation files are located-f keys- Generate only keys format (not classes)-O lib/ui/theme- Output directory for generated files-o codegen_key.g.dart- Custom filename for the generated fileAlternative: You can also use the basic command:
flutter packages pub run easy_localization:generate
π― Why This Tool? #
Problem with Manual easy_localization Setup: #
- π« Tedious: Manually find all hardcoded strings
- π« Error-Prone: Miss strings or create wrong keys
- π« Time-Consuming: Large projects take hours
- π« Inconsistent: Different key naming conventions
Solution with This Tool: #
- π Automated: Finds all strings automatically
- π Accurate: Uses AST parsing for 100% coverage
- π Fast: Process entire projects in minutes
- π Consistent: Smart camelCase key generation
π οΈ Technical Features #
- Smart Detection: Skips URLs, file paths, constants automatically
- Context Awareness: Shows where strings are found (arrays, methods, etc.)
- Batch Processing: Handles multiple strings per file efficiently
- Error Handling: Graceful fallbacks and comprehensive error reporting
- Code Formatting: Maintains proper Dart code formatting
- Standard Path: Uses
assets/languages/following easy_localization conventions
π Benefits #
- β Saves Hours: No more manual string hunting
- β Reduces Errors: Automated process prevents mistakes
- β Consistent Naming: Smart key generation
- β Works with Complex Code: Arrays, methods, constructors, maps
- β Perfect Integration: Designed specifically for easy_localization
- β
Standard Directory Structure: Uses conventional
assets/languages/path
π License #
MIT License - Perfect for both personal and commercial projects.
Made with β€οΈ for Flutter developers using easy_localization
Automate the boring stuff, focus on building great apps!