2.9 KiB
2.9 KiB
Remove Unused Translation Keys Script
This Python script removes unused translation keys from all .arb files in the repository.
Purpose
After identifying unused keys with find_unused_intl_keys.py, this script automates the removal of those keys from all language files. It removes both the key-value pairs and their corresponding metadata entries.
How It Works
- Loads Unused Keys: Reads the list of unused keys from
unused_intl_keys.json - Processes Each File: For each
.arbfile inlib/l10n/:- Loads the file as JSON
- Identifies keys to remove (both base keys and their
@keymetadata) - Removes those keys while preserving order
- Writes the cleaned JSON back to the file
- Reports Results: Shows how many keys were removed from each file
Usage
# First, generate the list of unused keys
python3 scripts/find_unused_intl_keys.py
# Then, remove those keys from all .arb files
python3 scripts/remove_unused_intl_keys.py
Output
The script provides progress output showing how many keys were removed from each file:
Loading unused keys from JSON file...
Found 488 unused keys to remove.
Found 54 .arb files to process.
Processing .arb files...
================================================================================
intl_en.arb: Removed 488 keys/metadata entries
intl_es.arb: Removed 557 keys/metadata entries
intl_fr.arb: Removed 950 keys/metadata entries
...
================================================================================
Total keys/metadata entries removed: 50015
Processed 54 .arb files successfully.
What Gets Removed
For each unused key (e.g., accountInformation), the script removes:
- The key itself:
"accountInformation": "Account information" - Its metadata (if present):
"@accountInformation": { ... }
Important Notes
- Backup recommended: The script modifies files in place. Consider committing your work or creating a backup before running.
- JSON parsing: The script uses Python's JSON library, which:
- Preserves the order of keys (using OrderedDict)
- May reformat indentation to 2 spaces
- Resolves duplicate keys by keeping the last value
- Validation: After running, verify the files are still valid JSON and that the application still works correctly.
Requirements
- Python 3.x
- Input file:
scripts/unused_intl_keys.json(generated byfind_unused_intl_keys.py)
Example
# Full workflow
cd /path/to/repository
# Step 1: Find unused keys
python3 scripts/find_unused_intl_keys.py
# Step 2: Review the list in scripts/unused_intl_keys.json
# Step 3: Remove the unused keys
python3 scripts/remove_unused_intl_keys.py
# Step 4: Verify the changes
git diff lib/l10n/
Related Files
- Input:
scripts/unused_intl_keys.json - Modified: All
lib/l10n/intl_*.arbfiles - Related:
scripts/find_unused_intl_keys.py(generates the input file)