[PR #5141] [MERGED] chore: add script to detect and prune unused translation keys #5320

Closed
opened 2026-06-05 15:20:58 -04:00 by yindo · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Mintplex-Labs/anything-llm/pull/5141
Author: @angelplusultra
Created: 3/4/2026
Status: Merged
Merged: 3/9/2026
Merged by: @timothycarambat

Base: masterHead: prune-unused-translations


📝 Commits (7)

  • 3e73fc0 add script to prune dead translation keys
  • f1c6b2e add support for dynamic translation keys
  • 1380d0e Merge branch 'master' into prune-unused-translations
  • 6b2ba2c improve performance of script
  • e3bc89f fix dynamic t() detection and add keyboard shortcut keys to allowlist
  • 5d21b78 rename scripts
  • 6687b41 change commands

📊 Changes

26 files changed (+185 additions, -2911 deletions)

View changed files

📝 frontend/src/locales/ar/common.js (+0 -122)
📝 frontend/src/locales/cs/common.js (+0 -124)
📝 frontend/src/locales/da/common.js (+0 -128)
📝 frontend/src/locales/de/common.js (+0 -126)
frontend/src/locales/dynamicKeyAllowlist.js (+19 -0)
📝 frontend/src/locales/en/common.js (+0 -167)
📝 frontend/src/locales/es/common.js (+0 -129)
📝 frontend/src/locales/et/common.js (+0 -120)
📝 frontend/src/locales/fa/common.js (+0 -126)
frontend/src/locales/findUnusedTranslations.mjs (+162 -0)
📝 frontend/src/locales/fr/common.js (+0 -130)
📝 frontend/src/locales/he/common.js (+0 -122)
📝 frontend/src/locales/it/common.js (+0 -131)
📝 frontend/src/locales/ja/common.js (+0 -125)
📝 frontend/src/locales/ko/common.js (+0 -123)
📝 frontend/src/locales/lv/common.js (+0 -124)
📝 frontend/src/locales/nl/common.js (+0 -124)
📝 frontend/src/locales/pl/common.js (+0 -127)
📝 frontend/src/locales/pt_BR/common.js (+0 -122)
📝 frontend/src/locales/ro/common.js (+0 -125)

...and 6 more files

📄 Description

Pull Request Type

  • 🔨 chore (Build, CI, maintenance)

Relevant Issues

N/A

Description

Adds a script to detect and prune unused (dead) translation keys from the English translation file (en/common.js). Over time, translation keys accumulate that are no longer referenced anywhere in the frontend source code. This script walks the translation object, scans all frontend/src/**/*.{js,jsx} files (excluding locales/) for t() calls, and diffs the two sets to find orphaned keys.

New file: frontend/src/locales/findUnusedTranslations.mjs

  • Reports all defined keys in en/common.js not referenced in source
  • Supports --delete flag to remove unused keys and prune empty parent objects
  • Exits with code 1 when unused keys are found (CI-friendly)
  • Detects dynamic t(variable) calls that cannot be statically analyzed and warns about them
  • Respects a dynamic key allowlist to prevent accidental deletion of keys used via variables

New file: frontend/src/locales/dynamicKeyAllowlist.js

  • Allowlist for translation keys that are referenced dynamically (e.g. t(someVariable))
  • Keys listed here are excluded from unused-key detection and will never be pruned
  • Developers should add keys here when introducing dynamic t() calls

New yarn scripts:

  • yarn verify:dead-translations — report unused keys
  • yarn prune-dead-translations — remove unused keys and lint
  • yarn prune-dead-translations-and-normalize — remove unused keys, then normalize all other language files

Visuals (if applicable)

N/A — tooling script, no UI changes.

Additional Information

Follows the same patterns as the existing verifyTranslations.mjs and normalizeEn.mjs scripts (Node built-ins only, no new dependencies). Uses static regex extraction of t("key") calls. For dynamic/computed t() calls (e.g. t(variable)), the script warns about them and supports a dynamicKeyAllowlist.js file to safeguard keys that are used at runtime but can't be detected statically.

Developer Validations

  • I ran yarn lint from the root of the repo & committed changes
  • Relevant documentation has been updated (if applicable)
  • I have tested my code functionality
  • Docker build succeeds locally

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Mintplex-Labs/anything-llm/pull/5141 **Author:** [@angelplusultra](https://github.com/angelplusultra) **Created:** 3/4/2026 **Status:** ✅ Merged **Merged:** 3/9/2026 **Merged by:** [@timothycarambat](https://github.com/timothycarambat) **Base:** `master` ← **Head:** `prune-unused-translations` --- ### 📝 Commits (7) - [`3e73fc0`](https://github.com/Mintplex-Labs/anything-llm/commit/3e73fc0552d8e5fa8b5b67bfcd9ae02f733fa8df) add script to prune dead translation keys - [`f1c6b2e`](https://github.com/Mintplex-Labs/anything-llm/commit/f1c6b2e1729ff775366d6aefde06ba8c96d60f4f) add support for dynamic translation keys - [`1380d0e`](https://github.com/Mintplex-Labs/anything-llm/commit/1380d0e7aab3b95adae2e57774427b01c9c1b62f) Merge branch 'master' into prune-unused-translations - [`6b2ba2c`](https://github.com/Mintplex-Labs/anything-llm/commit/6b2ba2c50e9d4b1c49bdab46a83f024f2a033e82) improve performance of script - [`e3bc89f`](https://github.com/Mintplex-Labs/anything-llm/commit/e3bc89feedc9bb4ebd9aad4387a187c266c3894c) fix dynamic t() detection and add keyboard shortcut keys to allowlist - [`5d21b78`](https://github.com/Mintplex-Labs/anything-llm/commit/5d21b78f7753f7ef73004ca6e05742471bf86057) rename scripts - [`6687b41`](https://github.com/Mintplex-Labs/anything-llm/commit/6687b41f5e4f791c13c86744f066364bd413b68f) change commands ### 📊 Changes **26 files changed** (+185 additions, -2911 deletions) <details> <summary>View changed files</summary> 📝 `frontend/src/locales/ar/common.js` (+0 -122) 📝 `frontend/src/locales/cs/common.js` (+0 -124) 📝 `frontend/src/locales/da/common.js` (+0 -128) 📝 `frontend/src/locales/de/common.js` (+0 -126) ➕ `frontend/src/locales/dynamicKeyAllowlist.js` (+19 -0) 📝 `frontend/src/locales/en/common.js` (+0 -167) 📝 `frontend/src/locales/es/common.js` (+0 -129) 📝 `frontend/src/locales/et/common.js` (+0 -120) 📝 `frontend/src/locales/fa/common.js` (+0 -126) ➕ `frontend/src/locales/findUnusedTranslations.mjs` (+162 -0) 📝 `frontend/src/locales/fr/common.js` (+0 -130) 📝 `frontend/src/locales/he/common.js` (+0 -122) 📝 `frontend/src/locales/it/common.js` (+0 -131) 📝 `frontend/src/locales/ja/common.js` (+0 -125) 📝 `frontend/src/locales/ko/common.js` (+0 -123) 📝 `frontend/src/locales/lv/common.js` (+0 -124) 📝 `frontend/src/locales/nl/common.js` (+0 -124) 📝 `frontend/src/locales/pl/common.js` (+0 -127) 📝 `frontend/src/locales/pt_BR/common.js` (+0 -122) 📝 `frontend/src/locales/ro/common.js` (+0 -125) _...and 6 more files_ </details> ### 📄 Description ### Pull Request Type - [x] 🔨 chore (Build, CI, maintenance) ### Relevant Issues N/A ### Description Adds a script to detect and prune unused (dead) translation keys from the English translation file (`en/common.js`). Over time, translation keys accumulate that are no longer referenced anywhere in the frontend source code. This script walks the translation object, scans all `frontend/src/**/*.{js,jsx}` files (excluding `locales/`) for `t()` calls, and diffs the two sets to find orphaned keys. **New file:** `frontend/src/locales/findUnusedTranslations.mjs` - Reports all defined keys in `en/common.js` not referenced in source - Supports `--delete` flag to remove unused keys and prune empty parent objects - Exits with code 1 when unused keys are found (CI-friendly) - Detects dynamic `t(variable)` calls that cannot be statically analyzed and warns about them - Respects a dynamic key allowlist to prevent accidental deletion of keys used via variables **New file:** `frontend/src/locales/dynamicKeyAllowlist.js` - Allowlist for translation keys that are referenced dynamically (e.g. `t(someVariable)`) - Keys listed here are excluded from unused-key detection and will never be pruned - Developers should add keys here when introducing dynamic `t()` calls **New yarn scripts:** - `yarn verify:dead-translations` — report unused keys - `yarn prune-dead-translations` — remove unused keys and lint - `yarn prune-dead-translations-and-normalize` — remove unused keys, then normalize all other language files ### Visuals (if applicable) N/A — tooling script, no UI changes. ### Additional Information Follows the same patterns as the existing `verifyTranslations.mjs` and `normalizeEn.mjs` scripts (Node built-ins only, no new dependencies). Uses static regex extraction of `t("key")` calls. For dynamic/computed `t()` calls (e.g. `t(variable)`), the script warns about them and supports a `dynamicKeyAllowlist.js` file to safeguard keys that are used at runtime but can't be detected statically. ### Developer Validations - [x] I ran `yarn lint` from the root of the repo & committed changes - [ ] Relevant documentation has been updated (if applicable) - [x] I have tested my code functionality - [ ] Docker build succeeds locally --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
yindo added the pull-request label 2026-06-05 15:20:58 -04:00
yindo closed this issue 2026-06-05 15:20:58 -04:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Mintplex-Labs/anything-llm#5320