Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

frontend: check-locales reads what a translation says, not only that it is there

Boris 'B' Kurktchiev97078419c4f6
diff --git a/frontend/scripts/check-locales.mjs b/frontend/scripts/check-locales.mjs
index ffc00360a..e03d130e7 100644
--- a/frontend/scripts/check-locales.mjs
+++ b/frontend/scripts/check-locales.mjs
@@ -1,5 +1,6 @@
#!/usr/bin/env node
-// Guards the one invariant every locale in src/locales/ shares: the same key set.
+// Guards what every locale in src/locales/ has to get right about a string it translates:
+// the key is there, it is not blank, and it keeps the {n} placeholders of the English original.
// English is the source language and has no locale file, so a key that exists in only
// some locales falls back to English silently, mid-sentence, with nothing failing
// anywhere. This catches that at review time instead of in the app.
@@ -28,29 +29,43 @@ for (const file of files) {
console.error(`${file}: no default-exported object`)
process.exit(1)
}
- locales.set(file.replace(/\.js$/, ''), new Set(Object.keys(dict)))
+ locales.set(file.replace(/\.js$/, ''), dict)
}
// How many locales carry each key — 1 means the key was added to a single file only,
// which is the usual shape of the bug and worth naming separately from plain gaps.
const seen = new Map()
-for (const keys of locales.values()) for (const k of keys) seen.set(k, (seen.get(k) || 0) + 1)
+for (const dict of locales.values()) for (const k of Object.keys(dict)) seen.set(k, (seen.get(k) || 0) + 1)
const union = [...seen.keys()]
+// t('…{0}…', x) substitutes by index, so a translation that loses a {0} drops the number out of
+// the sentence and one that invents a {1} prints the braces at the reader. The KEY is the English
+// original — English ships no pack — so the key is the reference. By multiset, not in order: a
+// translation is free to move the placeholders around the sentence, never to change the set.
+const marks = s => [...String(s).matchAll(/\{\d+\}/g)].map(m => m[0]).sort().join(' ')
+
let failed = false
-for (const [lang, keys] of locales) {
+for (const [lang, dict] of locales) {
+ const keys = new Set(Object.keys(dict))
const missing = union.filter(k => !keys.has(k))
const orphans = union.filter(k => keys.has(k) && seen.get(k) === 1)
- if (missing.length || orphans.length) {
+ // A blank value is worse than no translation: the English fallback only runs for a key that
+ // is ABSENT, so an empty string reaches the screen as an empty screen.
+ const blank = Object.entries(dict).filter(([, v]) => typeof v !== 'string' || !v.trim()).map(([k]) => k)
+ const mangled = Object.entries(dict).filter(([k, v]) => typeof v === 'string' && marks(v) !== marks(k))
+ if (missing.length || orphans.length || blank.length || mangled.length) {
failed = true
console.error(`\n${lang}.js: ${keys.size}/${union.length} keys`)
for (const k of missing) console.error(` missing: ${JSON.stringify(k)}`)
for (const k of orphans) console.error(` only here: ${JSON.stringify(k)}`)
+ for (const k of blank) console.error(` blank: ${JSON.stringify(k)}`)
+ for (const [k, v] of mangled)
+ console.error(` placeholders: ${JSON.stringify(k)} has [${marks(k) || '—'}], ${JSON.stringify(v)} has [${marks(v) || '—'}]`)
}
}
if (failed) {
- console.error('\nLocale key sets differ. Every locale must carry the same keys.')
+ console.error('\nEvery locale must carry the same keys, each translated to something, with the source string\'s {n} placeholders.')
process.exit(1)
}
diff --git a/frontend/scripts/check-locales.test.mjs b/frontend/scripts/check-locales.test.mjs
new file mode 100644
index 000000000..4d5d2d887
--- /dev/null
+++ b/frontend/scripts/check-locales.test.mjs
@@ -0,0 +1,78 @@
+/* check-locales.mjs, run the way CI runs it.
+ *
+ * The script is a program, and what CI gets from it is an exit code, so it is executed rather
+ * than imported: a copy of the real file is dropped into a throwaway tree laid out the way it
+ * expects (scripts/ beside src/locales/) with two tiny packs in it. Copying keeps the fixture
+ * from having to be one of the app's 14 real locales, and the file under test is still the file
+ * that ships, byte for byte. */
+import { describe, expect, it } from 'vitest'
+import { spawnSync } from 'node:child_process'
+import { copyFileSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
+import { tmpdir } from 'node:os'
+import { dirname, join } from 'node:path'
+import { fileURLToPath } from 'node:url'
+
+const here = dirname(fileURLToPath(import.meta.url))
+const script = join(here, 'check-locales.mjs')
+
+const run = packs => {
+ const dir = mkdtempSync(join(tmpdir(), 'check-locales-'))
+ try {
+ mkdirSync(join(dir, 'scripts'))
+ mkdirSync(join(dir, 'src', 'locales'), { recursive: true })
+ copyFileSync(script, join(dir, 'scripts', 'check-locales.mjs'))
+ for (const [lang, dict] of Object.entries(packs))
+ writeFileSync(join(dir, 'src', 'locales', `${lang}.js`), `export default ${JSON.stringify(dict, null, 2)}\n`)
+ const r = spawnSync(process.execPath, [join(dir, 'scripts', 'check-locales.mjs')], { encoding: 'utf8' })
+ return { code: r.status, out: r.stdout + r.stderr }
+ } finally { rmSync(dir, { recursive: true, force: true }) }
+}
+
+const de = { Save: 'Speichern', 'Next pass starts {0}': 'Der nächste Durchgang beginnt {0}' }
+
+describe('check-locales.mjs', () => {
+ it('passes packs that agree', () => {
+ const r = run({ de, fr: { Save: 'Enregistrer', 'Next pass starts {0}': 'Le prochain cycle commence {0}' } })
+ expect(r.out).toContain('in sync')
+ expect(r.code).toBe(0)
+ })
+
+ it('fails a translation that dropped the placeholder', () => {
+ // {0} is substituted by index: without it the date simply never appears in the sentence.
+ const r = run({ de, fr: { Save: 'Enregistrer', 'Next pass starts {0}': 'Le prochain cycle commence bientôt' } })
+ expect(r.code).toBe(1)
+ expect(r.out).toContain('placeholders')
+ expect(r.out).toContain('Next pass starts {0}')
+ })
+
+ it('fails a translation that invented a placeholder', () => {
+ const r = run({ de: { ...de, Save: 'Speichern {1}' }, fr: { Save: 'Enregistrer', 'Next pass starts {0}': 'Le prochain cycle commence {0}' } })
+ expect(r.code).toBe(1)
+ expect(r.out).toContain('placeholders')
+ })
+
+ it('accepts a translation that moved the placeholders around', () => {
+ const src = '{0} of {1}'
+ const r = run({ de: { [src]: '{1}: Nummer {0}' }, fr: { [src]: '{0} sur {1}' } })
+ expect(r.code).toBe(0)
+ })
+
+ it('fails a blank translation, which the English fallback never covers', () => {
+ const r = run({ de, fr: { Save: ' ', 'Next pass starts {0}': 'Le prochain cycle commence {0}' } })
+ expect(r.code).toBe(1)
+ expect(r.out).toContain('blank')
+ })
+
+ it('still fails a key one pack does not carry', () => {
+ const r = run({ de, fr: { Save: 'Enregistrer' } })
+ expect(r.code).toBe(1)
+ expect(r.out).toContain('missing')
+ })
+
+ // The app's own packs, so the suite carries the same check CI does rather than only the rule.
+ it('passes the locale packs this app ships', () => {
+ const r = spawnSync(process.execPath, [script], { encoding: 'utf8' })
+ expect(r.stdout + r.stderr).toContain('in sync')
+ expect(r.status).toBe(0)
+ })
+})