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.
JavaScript apple-watchbodyweightdockerfitnessfitness-trackergymhealthiosmcpnodejspasskeysprogressive-web-applicationspwareactself-hostedself-hostingvitewebauthnweightliftingworkout-tracker
i18n: add de-CH as a derived locale
Mirko Köster702a40aac89b
diff --git a/frontend/src/lib/active-workout-order.test.js b/frontend/src/lib/active-workout-order.test.jsindex 64cc28655..dff7657da 100644--- a/frontend/src/lib/active-workout-order.test.js+++ b/frontend/src/lib/active-workout-order.test.js@@ -1,6 +1,6 @@import { describe, expect, it } from 'vitest'import { canMoveActiveWorkoutUnit, moveActiveWorkoutUnit } from './active-workout-order.js'-import { LANGS } from './i18n-core.js'+import { LANGS, DERIVED_LOCALES } from './i18n-core.js'import { PT_BR_OVERRIDES } from '../locales/pt-BR.js'const entry = (id, extra = {}) => ({@@ -68,7 +68,9 @@ describe('active workout whole-unit order', () => {describe('active workout move locale coverage', () => {const packs = import.meta.glob('../locales/*.js', { eager: true, import: 'default' })- const localeCodes = Object.keys(LANGS).filter(code => code !== 'en')+ // English is the source language and has no pack. Derived locales (de-CH) have none either:+ // they transform their base language's pack at load time, and are checked separately below.+ const localeCodes = Object.keys(LANGS).filter(code => code !== 'en' && !DERIVED_LOCALES[code])it('defines both visible move labels in every current locale pack', () => {expect(Object.keys(packs)).toHaveLength(localeCodes.length)diff --git a/frontend/src/lib/i18n-core.js b/frontend/src/lib/i18n-core.jsindex ce9a568c1..15e3d0f1e 100644--- a/frontend/src/lib/i18n-core.js+++ b/frontend/src/lib/i18n-core.js@@ -4,18 +4,55 @@// loads, the React subscription hook) live in i18n.js and re-export from here.export const LANGS = {- en: 'English', de: 'Deutsch', es: 'Español', fr: 'Français', it: 'Italiano',- pt: 'Português (Portugal)', 'pt-BR': 'Português (Brasil)', pl: 'Polski',+ en: 'English', de: 'Deutsch', 'de-CH': 'Deutsch (Schweiz)', es: 'Español', fr: 'Français',+ it: 'Italiano', pt: 'Português (Portugal)', 'pt-BR': 'Português (Brasil)', pl: 'Polski',tr: 'Türkçe', ru: 'Русский', zh: '中文',ko: '한국어', hi: 'हिन्दी', th: 'ไทย', hu: 'Magyar'}export const INSTR_LANGS = ['en', 'es', 'fr', 'it', 'tr', 'ru', 'zh', 'hi', 'pl', 'ko', 'pt-BR', 'hu']export const EXERCISE_NAME_LANGS = ['pt-BR', 'hu']export const DATE_LOCALES = {- en: 'en-GB', de: 'de-DE', es: 'es-ES', fr: 'fr-FR', it: 'it-IT', pt: 'pt-PT', 'pt-BR': 'pt-BR',+ en: 'en-GB', de: 'de-DE', 'de-CH': 'de-CH', es: 'es-ES', fr: 'fr-FR', it: 'it-IT',+ pt: 'pt-PT', 'pt-BR': 'pt-BR',pl: 'pl-PL', tr: 'tr-TR', ru: 'ru-RU', zh: 'zh-CN', ko: 'ko-KR', hi: 'hi-IN', th: 'th-TH', hu: 'hu-HU'}+// Locales derived from another language by a pure text transform rather than carried as their+// own pack. Swiss Standard German has no ß — every one is written ss — so de-CH is de with a+// single substitution. Deriving it keeps one German source of truth: a hand-maintained de-CH+// would be 98.7% identical to de.js (16 of 1265 values differ), and check-locales.mjs would+// then require every future German string to be written twice, forever.+//+// The transform is exact in this direction ONLY. Going back needs vowel length — "Maße" and+// "Masse" both collapse to "Masse" — so de is always the base and never the derivative.+//+// Note this covers orthography, not vocabulary: a Swiss-specific word choice (Velo for+// Fahrrad) would need a real pack. None of the current strings contain one.+export const DERIVED_LOCALES = {+ 'de-CH': { base: 'de', transform: s => s.replace(/ß/g, 'ss') }+}++// The language whose packs a locale actually loads: a derived locale reads its base's, every+// other language its own. Used for the INSTR_LANGS/EXERCISE_NAME_LANGS membership tests too,+// so de-CH gains instructions and exercise names exactly when de does, with no second entry+// to remember to add.+export const baseLang = l => DERIVED_LOCALES[l]?.base || l++// Applies a derived locale's transform to a loaded pack, returning it unchanged for a language+// that is not derived. Packs are trees of strings: the locale pack is flat { source: target },+// instruction packs are { exId: [steps] }, exercise-name packs { exId: name }.+export function derivePack(l, pack) {+ const transform = DERIVED_LOCALES[l]?.transform+ if (!transform || !pack) return pack+ const walk = v =>+ typeof v === 'string' ? transform(v)+ : Array.isArray(v) ? v.map(walk)+ : v && typeof v === 'object'+ ? Object.fromEntries(Object.entries(v).map(([k, inner]) => [k, walk(inner)]))+ : v+ return walk(pack)+}+let lang = 'en' // set only by _setLangState, called from i18n.js setLanglet dict = {} // current locale pack (empty = English fallback)let instr = null // { exId: [steps] } for the current language, null = English@@ -63,8 +100,10 @@ export const exerciseNameSearchText = ex => {export function _setLangState(newLang, newDict, newInstr, newExerciseNames) {lang = LANGS[newLang] ? newLang : 'en'dict = lang === 'en' ? {} : (newDict || {})- instr = lang === 'en' || !INSTR_LANGS.includes(lang) ? null : (newInstr || null)- exerciseNames = lang === 'en' || !EXERCISE_NAME_LANGS.includes(lang) ? null : (newExerciseNames || null)+ instr = lang === 'en' || !INSTR_LANGS.includes(baseLang(lang)) ? null : (newInstr || null)+ exerciseNames = lang === 'en' || !EXERCISE_NAME_LANGS.includes(baseLang(lang))+ ? null+ : (newExerciseNames || null)version++return version}diff --git a/frontend/src/lib/i18n-core.test.js b/frontend/src/lib/i18n-core.test.jsnew file mode 100644index 000000000..cfafb54c1--- /dev/null+++ b/frontend/src/lib/i18n-core.test.js@@ -0,0 +1,100 @@+import { describe, it, expect } from 'vitest'+import {+ LANGS, INSTR_LANGS, EXERCISE_NAME_LANGS, DATE_LOCALES, DERIVED_LOCALES,+ baseLang, derivePack, dateLocale, getLang, t, _setLangState+} from './i18n-core.js'+import de from '../locales/de.js'++describe('baseLang', () => {+ it('maps a derived locale to the language whose packs it loads', () => {+ expect(baseLang('de-CH')).toBe('de')+ })++ it('leaves every other language as itself', () => {+ expect(baseLang('de')).toBe('de')+ expect(baseLang('pt-BR')).toBe('pt-BR')+ expect(baseLang('en')).toBe('en')+ expect(baseLang('nonsense')).toBe('nonsense')+ })++ it('keeps derived locales in step with their base for instructions and names', () => {+ // The membership tests run on the base, so de-CH gains instruction and exercise-name packs+ // exactly when de does — there is no second list entry to remember.+ expect(INSTR_LANGS.includes(baseLang('de-CH'))).toBe(INSTR_LANGS.includes('de'))+ expect(EXERCISE_NAME_LANGS.includes(baseLang('de-CH'))).toBe(EXERCISE_NAME_LANGS.includes('de'))+ })+})++describe('derivePack', () => {+ it('rewrites ß as ss for Swiss German', () => {+ expect(derivePack('de-CH', { a: 'Gesäß', b: 'Füße', c: 'schließen' }))+ .toEqual({ a: 'Gesäss', b: 'Füsse', c: 'schliessen' })+ })++ it('leaves legitimate ss alone', () => {+ expect(derivePack('de-CH', { a: 'Arbeitssätze', b: 'Schlüssel', c: 'Latissimus' }))+ .toEqual({ a: 'Arbeitssätze', b: 'Schlüssel', c: 'Latissimus' })+ })++ it('walks instruction packs, which nest steps in arrays', () => {+ expect(derivePack('de-CH', { '0007': ['Setz dich aufs Gerät.', 'Spann das Gesäß an.'] }))+ .toEqual({ '0007': ['Setz dich aufs Gerät.', 'Spann das Gesäss an.'] })+ })++ it('returns the pack untouched for a language that is not derived', () => {+ const pack = { a: 'Gesäß' }+ expect(derivePack('de', pack)).toBe(pack)+ expect(derivePack('en', pack)).toBe(pack)+ })++ it('passes a null pack through', () => {+ expect(derivePack('de-CH', null)).toBe(null)+ })++ it('derives the whole German locale with no ß left and no key lost', () => {+ // Control: the test is only meaningful while de itself still contains ß.+ expect(Object.values(de).some(v => /ß/.test(v))).toBe(true)++ const swiss = derivePack('de-CH', de)+ expect(Object.keys(swiss)).toEqual(Object.keys(de))+ expect(Object.values(swiss).filter(v => /ß/.test(v))).toEqual([])+ expect(swiss.glutes).toBe('Gesäss')+ // Only the ß values move; everything else is identical to the German pack.+ const changed = Object.keys(de).filter(k => de[k] !== swiss[k])+ expect(changed.every(k => /ß/.test(de[k]))).toBe(true)+ })+})++describe('de-CH as a selectable language', () => {+ it('is offered in the picker and formats numbers Swiss-style', () => {+ expect(LANGS['de-CH']).toBeTruthy()+ expect(DATE_LOCALES['de-CH']).toBe('de-CH')+ // The reason de-CH is worth having at all: German formatting gets the weights wrong here.+ expect(new Intl.NumberFormat(DATE_LOCALES['de-CH']).format(1234.5)).toBe("1'234.5")+ expect(new Intl.NumberFormat(DATE_LOCALES.de).format(1234.5)).toBe('1.234,5')+ })++ it('stays the active language rather than collapsing to its base', () => {+ _setLangState('de-CH', derivePack('de-CH', de), null, null)+ expect(getLang()).toBe('de-CH')+ expect(dateLocale()).toBe('de-CH')+ expect(t('glutes')).toBe('Gesäss')+ _setLangState('en', {}, null, null)+ })++ it('ships no locale pack of its own, so nothing can shadow the derivation', () => {+ const packs = import.meta.glob('../locales/*.js', { eager: true, import: 'default' })+ for (const code of Object.keys(DERIVED_LOCALES)) {+ expect(packs[`../locales/${code}.js`], `${code} is derived and must not have a pack`).toBeUndefined()+ }+ // Control: a language that is not derived does have one.+ expect(packs['../locales/de.js']).toBeTruthy()+ })++ it('declares de as its base, not the other way round', () => {+ // ss → ß is not mechanical (Maße and Masse both collapse to Masse), so the German pack+ // must never be generated from the Swiss one.+ expect(DERIVED_LOCALES['de-CH'].base).toBe('de')+ expect(DERIVED_LOCALES.de).toBeUndefined()+ })+})diff --git a/frontend/src/lib/i18n.js b/frontend/src/lib/i18n.jsindex 8d3b35ba3..71069331a 100644--- a/frontend/src/lib/i18n.js+++ b/frontend/src/lib/i18n.js@@ -5,12 +5,13 @@import { useSyncExternalStore } from 'react'import {- LANGS, INSTR_LANGS, EXERCISE_NAME_LANGS, DATE_LOCALES,- getLang, dateLocale, t, instrFor, exerciseNameFor, exerciseNameSearchText, getVersion, _setLangState+ LANGS, INSTR_LANGS, EXERCISE_NAME_LANGS, DATE_LOCALES, DERIVED_LOCALES,+ getLang, dateLocale, t, instrFor, exerciseNameFor, exerciseNameSearchText, getVersion,+ baseLang, derivePack, _setLangState} from './i18n-core.js'export {- LANGS, INSTR_LANGS, EXERCISE_NAME_LANGS, DATE_LOCALES,+ LANGS, INSTR_LANGS, EXERCISE_NAME_LANGS, DATE_LOCALES, DERIVED_LOCALES,getLang, dateLocale, t, instrFor, exerciseNameFor, exerciseNameSearchText}@@ -27,15 +28,19 @@ const notify = () => { subs.forEach(f => f()) }export async function setLang(l) {if (!LANGS[l]) l = 'en'if (l === getLang() && getVersion() > 0) return+ // A derived locale (de-CH) ships no packs of its own: it loads its base language's and+ // transforms the strings on the way through. `l` stays the selected language throughout, so+ // dateLocale() still reports de-CH and formats numbers Swiss-style.+ const base = baseLang(l)let dict = {}, instr = null, exerciseNames = null- try { dict = l === 'en' ? {} : (await localePacks['../locales/' + l + '.js']()).default } catch (e) { dict = {} }- try { instr = l === 'en' || !INSTR_LANGS.includes(l) ? null : (await instrPacks['../instr/' + l + '.js']()).default } catch (e) { instr = null }+ try { dict = base === 'en' ? {} : (await localePacks['../locales/' + base + '.js']()).default } catch (e) { dict = {} }+ try { instr = base === 'en' || !INSTR_LANGS.includes(base) ? null : (await instrPacks['../instr/' + base + '.js']()).default } catch (e) { instr = null }try {- exerciseNames = l === 'en' || !EXERCISE_NAME_LANGS.includes(l)+ exerciseNames = base === 'en' || !EXERCISE_NAME_LANGS.includes(base)? null- : (await exerciseNamePacks['../exercise-names/' + l + '.js']()).default+ : (await exerciseNamePacks['../exercise-names/' + base + '.js']()).default} catch (e) { exerciseNames = null }- _setLangState(l, dict, instr, exerciseNames)+ _setLangState(l, derivePack(l, dict), derivePack(l, instr), derivePack(l, exerciseNames))notify()}diff --git a/frontend/src/sheets.swap.test.jsx b/frontend/src/sheets.swap.test.jsxindex 1e112d804..bfa3cc740 100644--- a/frontend/src/sheets.swap.test.jsx+++ b/frontend/src/sheets.swap.test.jsx@@ -1,5 +1,5 @@// @vitest-environment happy-dom-import { LANGS } from './lib/i18n-core.js'+import { LANGS, DERIVED_LOCALES } from './lib/i18n-core.js'import { act } from 'react'import { beforeEach, describe, expect, it, vi } from 'vitest'import { swapActiveWorkoutExercise } from './sheets.jsx'@@ -78,7 +78,10 @@ describe('active exercise swap locale coverage', () => {const packs = import.meta.glob('./locales/*.js', { eager: true, import: 'default' })it('defines every new prompt in all twelve locale packs', () => {- expect(Object.keys(packs)).toHaveLength(Object.keys(LANGS).length - 1)+ // Minus English (the source language) and minus any derived locale, which transforms its+ // base language's pack at load time instead of shipping one.+ const packed = Object.keys(LANGS).filter(code => code !== 'en' && !DERIVED_LOCALES[code])+ expect(Object.keys(packs)).toHaveLength(packed.length)Object.entries(packs).forEach(([path, pack]) => {required.forEach(key => expect(pack, `${path} is missing ${key}`).toHaveProperty(key))})diff --git a/frontend/src/views/RoutineEdit.move.test.jsx b/frontend/src/views/RoutineEdit.move.test.jsxindex 301d7d829..c716977e9 100644--- a/frontend/src/views/RoutineEdit.move.test.jsx+++ b/frontend/src/views/RoutineEdit.move.test.jsx@@ -2,7 +2,7 @@import { readFileSync } from 'node:fs'import { resolve } from 'node:path'import React, { act } from 'react'-import { LANGS } from '../lib/i18n-core.js'+import { LANGS, DERIVED_LOCALES } from '../lib/i18n-core.js'import { createRoot } from 'react-dom/client'import { MemoryRouter, Route, Routes } from 'react-router-dom'import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'@@ -217,7 +217,7 @@ describe('routine move-control locale coverage', () => {const packs = import.meta.glob('../locales/*.js', { eager: true, import: 'default' })it('defines both accessible names in every non-English locale pack', () => {- expect(Object.keys(packs)).toHaveLength(Object.keys(LANGS).length - 1)+ expect(Object.keys(packs)).toHaveLength(Object.keys(LANGS).filter(c => c !== 'en' && !DERIVED_LOCALES[c]).length)Object.entries(packs).forEach(([path, pack]) => {expect(pack, `${path} is missing Move up`).toHaveProperty('Move up')expect(pack, `${path} is missing Move down`).toHaveProperty('Move down')diff --git a/frontend/src/views/Workout.remove.test.jsx b/frontend/src/views/Workout.remove.test.jsxindex aa6666e39..4e0b85307 100644--- a/frontend/src/views/Workout.remove.test.jsx+++ b/frontend/src/views/Workout.remove.test.jsx@@ -6,7 +6,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'import Workout, { removeActiveExercise } from './Workout.jsx'import { DEF, useStore } from '../store/useStore.js'import { useUI } from '../store/useUI.js'-import { LANGS } from '../lib/i18n-core.js'+import { LANGS, DERIVED_LOCALES } from '../lib/i18n-core.js'vi.mock('../lib/sound.js', () => ({ beep: vi.fn(), vibrate: vi.fn() }))vi.mock('../lib/api.js', () => ({ api: vi.fn(() => Promise.resolve({})) }))@@ -205,10 +205,12 @@ describe('remove-exercise locale coverage', () => {'Which exercise in this superset do you want to remove?']const packs = import.meta.glob('../locales/*.js', { eager: true, import: 'default' })- // Every non-English language has its own pack (English is the source, so it has none) —- // derived from LANGS rather than hardcoded so adding a language doesn't silently+ // Every non-English language has its own pack (English is the source, so it has none),+ // except a derived locale such as de-CH, which transforms its base language's pack at load+ // time — computed from LANGS rather than hardcoded so adding a language doesn't silently// understate this test's own coverage.- const nonEnglishLangCount = Object.keys(LANGS).length - 1+ const nonEnglishLangCount = Object.keys(LANGS)+ .filter(code => code !== 'en' && !DERIVED_LOCALES[code]).lengthit('defines every new prompt in every non-English locale pack', () => {expect(Object.keys(packs)).toHaveLength(nonEnglishLangCount)