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.

perf(i18n): fetch a language's three packs side by side

Duarte Santos37b865d15aa5
diff --git a/frontend/src/lib/i18n-packs-parallel.test.js b/frontend/src/lib/i18n-packs-parallel.test.js
new file mode 100644
index 000000000..ca3c7e83e
--- /dev/null
+++ b/frontend/src/lib/i18n-packs-parallel.test.js
@@ -0,0 +1,29 @@
+// @vitest-environment happy-dom
+/* A start in another language than English loads three packs (the words, the exercise steps and
+ the exercise names). They used to be awaited one after the other, three round trips before
+ Home could draw; they are fetched side by side now, and still all land. */
+import { describe, expect, it, vi } from 'vitest'
+
+const log = vi.hoisted(() => [])
+vi.mock('../locales/de.js', async () => {
+ log.push('words')
+ await new Promise(r => setTimeout(r, 30))
+ log.push('words done')
+ return { default: { Plan: 'Plan (de)' } }
+})
+vi.mock('../instr/de.js', () => { log.push('steps'); return { default: {} } })
+vi.mock('../exercise-names/de.js', () => { log.push('names'); return { default: {} } })
+
+import { setLang, t, getLang } from './i18n.js'
+
+describe('setLang', () => {
+ it('fetches the language packs side by side', async () => {
+ await setLang('de')
+ expect(getLang()).toBe('de')
+ expect(t('Plan')).toBe('Plan (de)')
+ expect(log.indexOf('steps')).toBeGreaterThan(-1)
+ expect(log.indexOf('names')).toBeGreaterThan(-1)
+ expect(log.indexOf('steps')).toBeLessThan(log.indexOf('words done'))
+ expect(log.indexOf('names')).toBeLessThan(log.indexOf('words done'))
+ })
+})
diff --git a/frontend/src/lib/i18n.js b/frontend/src/lib/i18n.js
index b19c6aaf1..12ed1fec3 100644
--- a/frontend/src/lib/i18n.js
+++ b/frontend/src/lib/i18n.js
@@ -65,14 +65,14 @@ export async function setLang(l, showEn, enOnly) {
lastShowEn = show
lastEnOnly = only
const base = baseLang(l)
- let dict = {}, instr = null, exerciseNames = 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 = base === 'en' || !EXERCISE_NAME_LANGS.includes(base)
- ? null
- : (await exerciseNamePacks['../exercise-names/' + base + '.js']()).default
- } catch (e) { exerciseNames = null }
+ // The three packs are fetched side by side: one after the other, a start in any language but
+ // English waited three round trips before Home could draw.
+ const pack = async (packs, key, fallback) => { try { return (await packs[key]()).default } catch (e) { return fallback } }
+ const [dict, instr, exerciseNames] = await Promise.all([
+ base === 'en' ? {} : pack(localePacks, '../locales/' + base + '.js', {}),
+ base === 'en' || !INSTR_LANGS.includes(base) ? null : pack(instrPacks, '../instr/' + base + '.js', null),
+ base === 'en' || !EXERCISE_NAME_LANGS.includes(base) ? null : pack(exerciseNamePacks, '../exercise-names/' + base + '.js', null),
+ ])
_setLangState(l, derivePack(l, dict), derivePack(l, instr), derivePack(l, exerciseNames), show, only)
if (detailsLoaded) loadDetails()
document.documentElement.lang = l