fluffychat/scripts/REMOVE_UNUSED_INTL_KEYS.md
copilot-swe-agent[bot] a8c1d0130d Add script to remove unused keys from all .arb files
Co-authored-by: ggurdin <46800240+ggurdin@users.noreply.github.com>
2025-11-17 18:45:38 +00:00

93 lines
2.9 KiB
Markdown

# 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
1. **Loads Unused Keys**: Reads the list of unused keys from `unused_intl_keys.json`
2. **Processes Each File**: For each `.arb` file in `lib/l10n/`:
- Loads the file as JSON
- Identifies keys to remove (both base keys and their `@key` metadata)
- Removes those keys while preserving order
- Writes the cleaned JSON back to the file
3. **Reports Results**: Shows how many keys were removed from each file
## Usage
```bash
# 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:
1. The key itself: `"accountInformation": "Account information"`
2. 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 by `find_unused_intl_keys.py`)
## Example
```bash
# 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_*.arb` files
- Related: `scripts/find_unused_intl_keys.py` (generates the input file)