Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Script to find unused i18n keys

Blue Mousec7e749add838
diff --git a/.gitignore b/.gitignore
index 540a7b665..69054d19c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -64,6 +64,7 @@ crowdin.yml
.cache
web/vite.config.ts.timestamp*
web/.react-router/
+web/unused_keys.txt
# Docs
docs/build
diff --git a/web/find_unused_keys.js b/web/find_unused_keys.js
new file mode 100644
index 000000000..56a507d30
--- /dev/null
+++ b/web/find_unused_keys.js
@@ -0,0 +1,83 @@
+import fs from 'fs'
+import path from 'path'
+
+const locales = JSON.parse(fs.readFileSync('public/locales/en.json', 'utf8'))
+const allKeys = []
+function flatten(obj, prefix = '') {
+ for (const key in obj) {
+ const fullKey = prefix ? `${prefix}.${key}` : key
+ if (typeof obj[key] === 'object' && !Array.isArray(obj[key])) {
+ flatten(obj[key], fullKey)
+ } else {
+ allKeys.push(fullKey)
+ }
+ }
+}
+flatten(locales)
+
+const dynamicPrefixes = [
+ 'dashboard.',
+ 'common.',
+ 'profileSettings.',
+ 'apiNotifications.',
+ 'experiments.status.',
+ 'alert.metrics.',
+ 'alert.conditions.',
+ 'project.',
+ 'project.mapping.',
+ 'project.contains.',
+ 'organisations.role.',
+ 'auth.signup.features.',
+ 'footer.',
+ 'project.settings.roles.',
+ 'main.faq.items.',
+ 'main.web.features.',
+ 'main.sessions.features.',
+ 'performance.fast.list.',
+ 'errors.fast.list.',
+ 'startups.whyUs.',
+ 'smbs.whyUs.',
+ 'marketers.whyUs.',
+ 'experiments.multipleVariantOptions.',
+ 'experiments.featureFlagMode.',
+ 'experiments.exposureTrigger.',
+ 'experiments.status.',
+ 'project.settings.tabs.',
+ 'project.settings.difficultyLevels.',
+]
+
+function getFiles(dir) {
+ let results = []
+ const list = fs.readdirSync(dir)
+ list.forEach((file) => {
+ file = path.join(dir, file)
+ const stat = fs.statSync(file)
+ if (stat && stat.isDirectory()) {
+ results = results.concat(getFiles(file))
+ } else {
+ if (file.match(/\.(ts|tsx|json|html)$/) && !file.endsWith('en.json') && !file.includes('node_modules')) {
+ results.push(file)
+ }
+ }
+ })
+ return results
+}
+
+const files = getFiles('app')
+const fileContents = files.map((f) => fs.readFileSync(f, 'utf8'))
+
+const unusedKeys = allKeys.filter((key) => {
+ if (dynamicPrefixes.some((p) => key.startsWith(p))) {
+ return false
+ }
+
+ const isUsed = fileContents.some((content) => content.includes(key))
+ if (isUsed) {
+ return false
+ }
+
+ return true
+})
+
+fs.writeFileSync('unused_keys.txt', unusedKeys.join('\n'))
+console.log(`Stored ${unusedKeys.length} unused keys in unused_keys.txt`)