Flutter Universal String Extractor
A powerful, interactive command-line script to automate the extraction of hard-coded strings from your Flutter project for localization. This tool scans your Dart files, replaces strings with a localization key, and updates your JSON language files accordingly.
The Problem
Manually finding every hard-coded string, replacing it with a .tr() extension, and adding the key to your JSON files is tedious, time-consuming, and prone to errors. This process slows down development, especially in large projects.
The Solution
This script automates the entire process. It intelligently identifies user-facing strings, skips code-like or debug strings, and prepares your project for translation into multiple languages, saving you hours of manual work.
Features
- Interactive CLI: User-friendly prompts guide you through the process.
- Automated Replacement: Replaces string literals like
"Hello World"with'Hello World'.tr(). - Smart String Detection: Intelligently skips strings that are not meant for translation (e.g., paths, URLs, interpolated strings like
"$name", debug logs). - JSON File Management: Automatically creates or updates your
.jsonlanguage files.- Preserves existing translations.
- Adds new keys to your base language file (e.g.,
en.json). - Adds placeholder keys to target language files (e.g.,
ar.json,fr.json).
- Package Support: Works with both
easy_localizationandlocalize_and_translate. - Safe and Flexible: Scans only the directories you specify and requires confirmation before modifying any files.
Prerequisites
- Dart SDK installed on your machine.
- A Flutter project using either
easy_localizationorlocalize_and_translate.
How to Use
-
Save the Script: Save the
extract_strings.dartscript to the root directory of your Flutter project. -
Run the Script: Open your terminal in the project root and run the following command:
extract_strings -
Follow the Prompts: The script will ask you a series of questions:
- Which localization package you are using.
- Your project's base language (e.g.,
en). - Your target languages (e.g.,
ar, fr). - Which folders inside
lib/to scan (or*for all).
4. Confirm and Execute: The script will show a summary of the actions it's about to take. Type yto proceed.
Example
Before:
// in lib/screens/home_screen.dart
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Welcome Home"),
),
body: Center(
child: Text("This is the home page."),
),
);
}
}
After Running the Script:
// in lib/screens/home_screen.dart
import 'package:easy_localization/easy_localization.dart';
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Welcome Home'.tr()),
),
body: Center(
child: Text('This is the home page.'.tr()),
),
);
}
}
Result in assets/lang/en.json:
{
"This is the home page.": "This is the home page.",
"Welcome Home": "Welcome Home"
}
Result in assets/lang/ar.json:
{
"This is the home page.": "",
"Welcome Home": ""
}
Configuration
You can modify the constants at the top of the extract_strings.dart script to change the default directories:
const String searchDir = 'lib';
const String langAssetsDir = 'assets/lang';
License
This project is licensed under the MIT License - see the LICENSE file for details.