فهرست منبع

Merge pull request #11144 from overleaf/jpa-translations-helper-scripts

[web] scripts: add script for checking coverage of non-en translations

GitOrigin-RevId: ccde0d5b56b9e3b8f2a32916ecf1b442482edd12
Jakob Ackermann 3 سال پیش
والد
کامیت
0b7e28432d
2فایلهای تغییر یافته به همراه58 افزوده شده و 0 حذف شده
  1. 35 0
      services/web/scripts/translations/checkCoverage.js
  2. 23 0
      services/web/scripts/translations/cleanupUnusedLocales.js

+ 35 - 0
services/web/scripts/translations/checkCoverage.js

@@ -0,0 +1,35 @@
+const fs = require('fs')
+const Path = require('path')
+
+const LOCALES = Path.join(__dirname, '../../locales')
+const SORT_BY_PROGRESS = process.argv.includes('--sort-by-progress')
+
+function count(file) {
+  return Object.keys(require(Path.join(LOCALES, file))).length
+}
+
+async function main() {
+  const EN = count('en.json')
+  const rows = []
+
+  for (const file of await fs.promises.readdir(LOCALES)) {
+    if (file === 'README.md') continue
+    const n = count(file)
+    const name = file.replace('.json', '')
+    rows.push({
+      name,
+      done: n,
+      missing: EN - n,
+      progress: ((100 * n) / EN).toFixed(2).padStart(5, ' ') + '%',
+    })
+  }
+  if (SORT_BY_PROGRESS) {
+    rows.sort((a, b) => b.done - a.done)
+  }
+  console.table(rows)
+}
+
+main().catch(error => {
+  console.error(error)
+  process.exit(1)
+})

+ 23 - 0
services/web/scripts/translations/cleanupUnusedLocales.js

@@ -4,6 +4,7 @@ const { execSync } = require('child_process')
 
 const EN_JSON = Path.join(__dirname, '../../locales/en.json')
 const CHECK = process.argv.includes('--check')
+const SYNC_NON_EN = process.argv.includes('--sync-non-en')
 
 async function main() {
   const locales = JSON.parse(await fs.promises.readFile(EN_JSON, 'utf-8'))
@@ -82,6 +83,28 @@ async function main() {
       unusedKeys.push(key)
     }
   }
+
+  if (SYNC_NON_EN) {
+    if (CHECK) {
+      throw new Error('--check is incompatible with --sync-non-en')
+    }
+    const LOCALES = Path.join(__dirname, '../../locales')
+    for (const name of await fs.promises.readdir(LOCALES)) {
+      if (name === 'README.md') continue
+      if (name === 'en.json') continue
+      const path = Path.join(LOCALES, name)
+      const locales = JSON.parse(await fs.promises.readFile(path, 'utf-8'))
+      for (const key of Object.keys(locales)) {
+        if (!found.has(key)) {
+          delete locales[key]
+        }
+      }
+      const sorted =
+        JSON.stringify(locales, Object.keys(locales).sort(), 2) + '\n'
+      await fs.promises.writeFile(path, sorted)
+    }
+  }
+
   if (unusedKeys.length === 0) {
     return
   }