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.

fix(i18n): descriptions loaded for one language never land under another

Duarte Santose5e546642c60
diff --git a/frontend/src/lib/i18n-core.js b/frontend/src/lib/i18n-core.js
index 28e00f869..d306b0796 100644
--- a/frontend/src/lib/i18n-core.js
+++ b/frontend/src/lib/i18n-core.js
@@ -182,14 +182,18 @@ export function _setDetailsLoader(fn) { detailsLoader = fn; detailsAsked = false
// Called once the English packs (and the language's descriptions, if any) have loaded. A pack
// that did not come (offline, before the worker had it) stays missing, not empty: an empty one
-// would show no steps until the next reload.
-export function _setDetails(newEnInstr, newEnDescs, newDescs) {
+// would show no steps until the next reload. `forLang` is the language the descriptions were
+// loaded for: switched away from while they loaded, they are not this language's and are not
+// kept (the English packs are the same for every language). False then, and the caller loads
+// the current language's own.
+export function _setDetails(newEnInstr, newEnDescs, newDescs, forLang = lang) {
enInstr = newEnInstr || enInstr
enDescs = newEnDescs || enDescs
if (enInstr && enDescs) clearRetry()
- descs = lang === 'en' ? null : (newDescs || null)
+ const current = forLang === lang
+ if (current) descs = lang === 'en' ? null : (newDescs || null)
version++
- return version
+ return current
}
// Built-in catalogue names are bilingual when a translated name pack is active. A pack need not
@@ -244,7 +248,10 @@ export const exerciseNameSearchText = ex => {
// exported as setLang because loading packs requires import.meta.glob, which is Vite-only.
// `dict`, `instr` and `exerciseNames` may be null to reset to their English fallbacks.
export function _setLangState(newLang, newDict, newInstr, newExerciseNames, showEn = true, enOnlyFlag = false) {
+ const was = lang
lang = LANGS[newLang] ? newLang : 'en'
+ // The old language's descriptions are not this one's: English until its own have loaded.
+ if (lang !== was) descs = null
dict = lang === 'en' ? {} : (newDict || {})
instr = lang === 'en' || !INSTR_LANGS.includes(baseLang(lang)) ? null : (newInstr || null)
exerciseNames = lang === 'en' || !EXERCISE_NAME_LANGS.includes(baseLang(lang))
diff --git a/frontend/src/lib/i18n-details-lang.test.js b/frontend/src/lib/i18n-details-lang.test.js
new file mode 100644
index 000000000..b41ae4f91
--- /dev/null
+++ b/frontend/src/lib/i18n-details-lang.test.js
@@ -0,0 +1,23 @@
+/* The first details load and a language switch can overlap: setLang reloads the descriptions
+ only once a load has finished, so one that started under German and finished under French
+ used to put German descriptions under French. They are kept only for the language they were
+ loaded for; the loader then loads the current language's own. */
+import { describe, expect, it } from 'vitest'
+import * as core from './i18n-core.js'
+
+const ex = { id: '9999', st: [] }
+
+describe('a language switch while the details load', () => {
+ it('keeps the descriptions of one language from landing under another', () => {
+ core._setLangState('de', {}, null, null, true, false)
+ expect(core._setDetails({ 9999: ['Lift.'] }, { 9999: 'A lift.' }, { 9999: 'Ein Heben.' }, 'de')).toBe(true)
+ expect(core.descFor(ex)).toBe('Ein Heben.')
+ core._setLangState('fr', {}, null, null, true, false)
+ // German descriptions that finished loading after the switch to French
+ expect(core._setDetails({ 9999: ['Lift.'] }, { 9999: 'A lift.' }, { 9999: 'Ein Heben.' }, 'de')).toBe(false)
+ expect(core.descFor(ex)).not.toBe('Ein Heben.')
+ expect(core.instrFor(ex)).toEqual(['Lift.']) // the English steps are every language's
+ expect(core._setDetails(null, null, { 9999: 'Un levé.' }, 'fr')).toBe(true)
+ expect(core.descFor(ex)).toBe('Un levé.')
+ })
+})
diff --git a/frontend/src/lib/i18n.js b/frontend/src/lib/i18n.js
index f1a57d33b..fb4c1d2ac 100644
--- a/frontend/src/lib/i18n.js
+++ b/frontend/src/lib/i18n.js
@@ -27,7 +27,8 @@ const descPacks = import.meta.glob('../exercise-desc/*.js')
// the descriptions whenever the language changes after that.
let detailsLoaded = false
async function loadDetails() {
- const base = baseLang(getLang())
+ const l = getLang()
+ const base = baseLang(l)
const load = async (packs, key) => { try { return packs[key] ? (await packs[key]()).default : null } catch (e) { return null } }
const [en, enDesc, desc] = await Promise.all([
load(instrPacks, '../instr/en.js'),
@@ -36,9 +37,12 @@ async function loadDetails() {
])
// Either English pack missing is a failed load (both always exist): core asks again later.
if (en && enDesc) detailsLoaded = true
- _setDetails(en, enDesc, derivePack(getLang(), desc))
+ // The language changed while this loaded (setLang only reloads once a load has finished): the
+ // descriptions are the old language's, so the new one's are loaded instead.
+ const current = _setDetails(en, enDesc, derivePack(l, desc), l)
if (!en || !enDesc) _detailsFailed()
notify()
+ if (!current) return loadDetails()
}
_setDetailsLoader(loadDetails)