flutter_hardcode_localizer 1.0.10 copy "flutter_hardcode_localizer: ^1.0.10" to clipboard
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:

  1. ❌ Manually creating JSON key-value pairs
  2. ❌ Manually replacing strings in code
  3. ❌ Manually running code generation
  4. ❌ Manual import management

Instead, it automates everything:

  1. βœ… Finds hardcoded strings in your Flutter project
  2. βœ… Automatically adds them to en.json with smart key generation
  3. βœ… Replaces strings with LocaleKeys.key.tr() format
  4. βœ… Works with arrays, methods, constructors, maps seamlessly
  5. βœ… 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 #

  1. Setup easy_localization in your project (if not already done)

  2. Add assets directory to pubspec.yaml:

flutter:
  assets:
    - assets/languages/
  1. Run this tool: dart run flutter_hardcode_localizer:localize

  2. 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
  1. Add import: import "lib/ui/theme/codegen_key.g.dart";

  2. 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 your pubspec.yaml under 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 file

Alternative: 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!

4
likes
150
points
73
downloads

Publisher

unverified uploader

Weekly Downloads

A Dart plugin that finds hardcoded strings in Flutter projects and provides quick-fix to move them to localization files for easy_localization.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

analyzer, args, custom_lint_builder, dart_style, path

More

Packages that depend on flutter_hardcode_localizer