This script automatically synchronizes JSON localization files and generates a translation class for Flutter projects.
- Open a terminal and navigate to the directory where the script is located.
- Execute the following command:
dart localization_checker.dart
You can configure the script to run automatically whenever you save a file. This is useful for keeping your localization files and translation class up-to-date without manually running the script.
However, if you prefer, you can also run the script manually from the terminal (see below).
- Install the Run on Save extension in your code editor (e.g., Visual Studio Code).
- Open your
.vscode/settings.json
file. - Add the following configuration:
{ "emeraldwalk.runonsave": { "commands": [ { "match": ".*\\.json$", // Match all JSON files "cmd": "dart localization_checker.dart" // Command to run the script } ] } }
The script's behavior can be customized by editing the config
variable.
const Map<String, dynamic> config = {
"languagePath": "assets/langs",
"mainJsonFile": "assets/langs/en.json",
"multiLanguageStringsFile": "lib/core/constants/app_multi_language_strings.dart",
"processJsonFiles": true,
"processClassFile": true,
};
-
languagePath
- What it does: Specifies the directory where JSON localization files are stored.
- Expected Value: A relative path to the folder containing the language files.
- Example:
assets/langs
: If your JSON files are located in theassets/langs
directory.lib/localization
: If your JSON files are in a custom directory.
- Notes: Ensure that this path exists in your project before running the script.
-
mainJsonFile
- What it does: Specifies the main JSON file that acts as the reference for synchronization.
- Expected Value: The full path to the primary JSON file (e.g.,
en.json
). - Example:
assets/langs/en.json
- Notes: This file should always exist as it serves as the source of truth for synchronization.
-
multiLanguageStringsFile
- What it does: Specifies where the generated
MultiLanguageStrings
class will be saved. - Expected Value: A full path to the
.dart
file where the class will be generated. - Example:
lib/core/constants/app_multi_language_strings.dart
- Notes: Ensure that the directory exists before running the script.
- What it does: Specifies where the generated
-
processJsonFiles
- What it does: Controls whether the script processes and synchronizes JSON files.
- Accepted Values:
true
: Synchronizes JSON files (adds missing keys and removes unused ones).false
: Skips JSON file processing.
- Default Value:
true
- Notes: Set to
false
if you only want to generate theMultiLanguageStrings
class.
-
processClassFile
- What it does: Controls whether the script generates the
MultiLanguageStrings
class. - Accepted Values:
true
: Generates the class.false
: Skips class generation.
- Default Value:
true
- Notes: Set to
false
if you only want to process JSON files without generating the class.
- What it does: Controls whether the script generates the
-
If you want to process only JSON files (skip class generation):
const Map<String, dynamic> config = { "languagePath": "assets/langs", "mainJsonFile": "assets/langs/en.json", "multiLanguageStringsFile": "lib/core/constants/app_multi_language_strings.dart", "processJsonFiles": true, "processClassFile": false, };
-
If you want to generate only the
MultiLanguageStrings
class:const Map<String, dynamic> config = { "languagePath": "assets/langs", "mainJsonFile": "assets/langs/en.json", "multiLanguageStringsFile": "lib/core/constants/app_multi_language_strings.dart", "processJsonFiles": false, "processClassFile": true, };
Parameter | Description | Example Value |
---|---|---|
languagePath |
Path to the folder with localization JSON files | assets/langs |
mainJsonFile |
Path to the main JSON file (reference file) | assets/langs/en.json |
multiLanguageStringsFile |
Path to save the generated class file | lib/core/constants/app_multi_language_strings.dart |
processJsonFiles |
Whether to process JSON files | true or false |
processClassFile |
Whether to generate the MultiLanguageStrings class |
true or false |