A command-line tool for cleaning up translation JSON files by removing unused keys and empty objects. This tool helps to keep your translation files organized and efficient by ensuring that only the necessary keys are present.
After installing the package, you can run the tool from the command line. Below is a step-by-step example of how to use the translation JSON cleaner.
If you haven't already installed the package, you can do so using npm:
npm install -g translation-json-cleaner
or use it with npm scripts:
npx translation-json-cleaner
Assume you have the following project structure:
my-project/
├── src/
│ ├── index.js
│ └── components/
│ ├── Button.js
│ └── Header.js
├── translations/
│ └── en.json
└── package.json
Your translations/en.json file might look like this:
{
"greeting": "Hello",
"farewell": "Goodbye",
"nested": {
"welcome": "Welcome to our application!",
"thankYou": "Thank you for using our app!"
}
}
Navigate to your project directory and run the translation JSON cleaner:
translation-json-cleaner
or use it with npm scripts:
npx translation-json-cleaner
The tool will prompt you for the following information:
- Translation File Path: Enter the path to your JSON translation file. For example:
./translations/en.json
- Project Path: Enter the root path of your project where the keys will be checked. For example:
./
- File Extensions: Enter a comma-separated list of file extensions to scan for translation keys. For example:
js,ts,jsx
The tool will scan your project files for keys used in your source code and compare them to the keys in your translation JSON file. It will list any unused keys found and ask whether you want to remove them.
If there are unused keys, you will see a prompt like this:
Unused keys found:
- farewell
Do you want to remove the unused keys and any empty objects? (yes/no)
Type yes to confirm removal. The unused keys will be deleted, and any empty objects will also be removed from your translation file. After running the tool, your translations/en.json file might look like this:
{
"greeting": "Hello",
"nested": {
"welcome": "Welcome to our application!",
"thankYou": "Thank you for using our app!"
}
}
You can install the package globally or as a dependency in your project.
To install the CLI tool globally, run:
npm install -g translation-json-cleaner
To add it to your project, use:
npm install translation-json-cleaner -g
After installation, you can run the tool from the command line:
translation-json-cleaner
It's recommended run this code from the root of your project to ensure that all translation files are cleaned up.
When you run the tool, you will be prompted to provide the following information:
- Translation File Path: The path to your JSON translation file (e.g.,
./path/to/translations.json
). - Project Path: The root path of your project where the keys will be checked (e.g.,
./
). - File Extensions: A comma-separated list of file extensions to scan for translation keys (e.g.,
js,ts,jsx
). You can leave this blank to check all file types.
The tool will:
- Scan your project for keys used in your source files.
- List any unused keys found in the translation file.
- Ask if you want to remove the unused keys and any empty objects left behind.
- Removes unused translation keys: Cleans up your translation JSON files by removing keys that are not referenced in your project.
- Removes empty objects: Cleans up any empty objects left after unused keys are removed.
- Supports nested translation keys: Handles complex translation structures with nested keys.
- Configurable: Allows you to specify the file extensions to scan for translation keys.
This project is licensed under the MIT License.
Contributions are welcome! Please open an issue or submit a pull request for any enhancements or bug fixes.