localize_generator_keys
A Dart CLI tool that automatically scans your Flutter project for hardcoded UI strings and replaces them with .tr
keys for localization using GetX. EasyLocalization etc It also generates a corresponding JSON file containing the translations.
๐ Features
- Detects and replaces hardcoded strings in widgets like
Text
,TextSpan
,RichText
,Text.rich
, and buttons. - Generates a translation key based on the original string.
- Outputs a structured JSON file under
assets/lang/
. - Supports customization for language code.
๐ฆ Installation
Add the package to dev_dependencies
in pubspec.yaml
:
dev_dependencies:
localize_generator_keys: latest
or
dart pub global activate localize_generator_keys
๐ง Usage
From the root of your Flutter project:
dart run localize_generator_keys
also can use it like
dart run localize_generator_keys . en
- The first argument is the root path to scan (e.g.,
.
). - The second argument is the language code (
en
,ar
, etc).
The tool will:
- Scan all
.dart
files underlib/
- Replace hardcoded strings with
.tr
keys - Output
assets/lang/lang_en.json
๐ง How it works
This tool uses regular expressions to match widgets like:
Text("Hello World")
TextSpan(text: "Welcome")
TextButton(child: Text("Click Me"))
And transforms them into:
Text("hello_world".tr)
TextSpan(text: "welcome".tr)
TextButton(child: Text("click_me".tr))
With a generated lang_en.json
like:
{
"hello_world": "Hello World",
"welcome": "Welcome",
"click_me": "Click Me"
}
๐ Output
assets/
โโโ lang/
โโโ lang_en.json
Great Mix
๐ Support for Multiple Languages (Offline)
To translate your translation files (lang_en.json
) to other languages without the need for an internet connection, you can use the argos_translator_offline package that I developed.
โ Features:
- Offline translation using the Argos Translate library.
- Supports more than 50 languages.
- No external API or internet connection required.
๐งช Example Usage:
dart run argos_translator_offline path=assets/lang/lang_en.json from=en to=ar
This will translate the file from English to Arabic while maintaining the same structure.
For more details, check out the argos_translator_offline package page on pub.dev.
โ ๏ธ Limitations
- Only works with strings in quotes (single/double).
- Does not detect variables inside text.
- Regex-based matching may skip edge cases.
๐ License
MIT
Built with โค๏ธ by abdelrhmantolba.online