translate_gen
Tools to manage translations in Flutter':' extract strings, configure localization, and replace texts using config files.
OmarMahmoud-alam/translate_gen open-source repository details.
English project snapshot. Visit GitHub for the latest content.
This package provides tools to assist with translation tasks in Flutter projects, including preparing configuration files, extracting translatable strings, and replacing them based on a configuration. It supports multiple AI translation providers including Gemini and DeepSeek R1.
Add the following to your pubspec.yaml:
dev_dependencies:
translate_gen: ^1.0.6
Run flutter pub get to install the package.
The prepare command generates a configuration file (prepare.dart) and an empty replace.json file under the assets/translate_gen directory. You can choose between two configuration types: normal or easy (for easy_localization package).
Command:
flutter pub run translate_gen:prepare
Command with Options:
# For normal translation setup
flutter pub run translate_gen:prepare --type normal
# For easy_localization package setup (default)
flutter pub run translate_gen:prepare --type easy
Output:
assets/translate_gen/prepare.dart with the following content:For Easy Localization (default):
import 'package:translate_gen/src/extract/exception_rules.dart';
import 'package:translate_gen/src/translate/translation_provider.dart';
final translationConfig = ExceptionRules(
textExceptions: ['import'],
lineExceptions: ['line_start_to_skip'],
contentExceptions: ['substring_to_skip'],
folderExceptions: [''],
extractFilter: [
RegExp(r"'[^']*[\u0600-\u06FF][^']*'"),
RegExp(r'"[^"]*[\u0600-\u06FF][^"]*"')
],
import: [
"import 'package:easy_localization/easy_localization.dart';",
"import 'package:$projectName/core/app_strings/locale_keys.dart';"
],
key: "LocaleKeys.{key}.tr()",
keyWithVariable: "LocaleKeys.{key}.tr(args: [{args}])",
translate: true,
extractOutput: 'replace.json',
aiKey: '', // Required only when using TranslationProvider.gemini
aiModel: TranslationProvider.deepseekR1, // Available options: gemini, deepseekR1
);
For Normal Translation:
import 'package:translate_gen/src/extract/exception_rules.dart';
import 'package:translate_gen/src/translate/translation_provider.dart';
final translationConfig = ExceptionRules(
textExceptions: ['import'],
lineExceptions: ['line_start_to_skip'],
contentExceptions: ['substring_to_skip'],
folderExceptions: [''],
extractFilter: [
RegExp(r"'[^']*[\u0600-\u06FF][^']*'"),
RegExp(r'"[^"]*[\u0600-\u06FF][^"]*"')
],
import: [],
key: "s.current.{key}",
keyWithVariable: "s.current.{key}({args})", //not work in flutter_localization only in easy_localization
translate: true,
extractOutput: 'replace.json',
aiKey: '', // Required only when using TranslationProvider.gemini
aiModel: TranslationProvider.deepseekR1, // Available options: gemini, deepseekR1
);
assets/translate_gen/replace.json as an empty JSON file:{}
Purpose:
This command sets up the necessary configuration for the translation process, defining rules for exceptions, filters for extracting translatable strings (e.g., Arabic text), and the format for translation keys.
easy_localization package, including the necessary import and .tr() method callsThe package supports multiple AI translation providers:
When using Google Gemini as your AI model, you must provide a valid API key:
aiModel to TranslationProvider.geminiaiKey fieldExample configuration for Gemini:
final translationConfig = ExceptionRules(
// ... other configuration ...
aiKey: 'your-gemini-api-key-here',
aiModel: TranslationProvider.gemini,
);
Note: The
aiKeyfield is only required when usingTranslationProvider.geminiand translation is enabled. You can leave it empty when using other AI models.
The extract command scans the specified path (or default path) for translatable strings and generates key-value pairs to be stored in replace.json.
Command:
flutter pub run translate_gen:extract [--path='lib/core']
Parameters:
--path: Optional. Specifies the directory to scan for translatable strings. Defaults to lib/core if not provided.Output:
assets/translate_gen/replace.json with extracted strings in the format:{
"key1": "translatable string 1",
"key2": "translatable string 2"
}
Purpose: This command identifies strings (e.g., Arabic text matching the regex patterns in prepare.dart) and prepares them for translation by storing them in replace.json. The AI model specified in the configuration will be used for automatic translation.
The replace command replaces strings in the specified path (or default path) with translation keys based on the content of replace.json.
Command:
flutter pub run translate_gen:replace [--path='lib/core']
Parameters:
--path: Optional. Specifies the directory where strings will be replaced. Defaults to lib/core if not provided.Behavior:
replace.json to get key-value pairsprepare.dart (e.g., LocaleKeys.{key}.tr() or LocaleKeys.{key}.tr(args: [{args}]) for strings with variables)import 'package:easy_localization/easy_localization.dart';) to files as specified in the configurationPurpose: This command automates the replacement of hard-coded strings with translation keys, enabling easy localization using the easy_localization package.
After running the prepare command, the following structure is created:
assets/
└── translate_gen/
├── prepare.dart
└── replace.json
| Parameter | Type | Description |
|---|---|---|
textExceptions |
List<String> |
Text patterns to exclude from extraction |
lineExceptions |
List<String> |
Line patterns to skip during extraction |
contentExceptions |
List<String> |
Content substrings to skip |
folderExceptions |
List<String> |
Folders to exclude from scanning |
extractFilter |
List<RegExp> |
Regex patterns to match translatable strings |
import |
List<String> |
Import statements to add to files |
key |
String |
Format for translation keys |
keyWithVariable |
String |
Format for translation keys with variables |
translate |
bool |
Enable/disable automatic translation |
extractOutput |
String |
Output file for extracted strings |
aiKey |
String |
API key for Gemini (required only for Gemini) |
aiModel |
TranslationProvider |
AI model to use for translation |
extractFilter in prepare.dart is configured to detect Arabic strings (Unicode range \u0600-\u06FF). Modify the regex patterns to support other languages if neededreplace.json file is overwritten during the extract command, so back up any manual changes before running itreplace command, make sure replace.json has the following content (with at least an empty object {})