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.

i18n: split the pure core out so the lib is Node-loadable

Pengboifadeb17b6c67
diff --git a/frontend/src/lib/exercises.js b/frontend/src/lib/exercises.js
index 20d82d278..451929f78 100644
--- a/frontend/src/lib/exercises.js
+++ b/frontend/src/lib/exercises.js
@@ -1,5 +1,7 @@
import { EXDB } from './exercises-data.js'
-import { t } from './i18n.js'
+// Imports i18n-core (not i18n.js) so this stays loadable from plain Node — the MCP server
+// reuses EXIDX/musclesOf/etc. without dragging in the React + Vite-only i18n.js.
+import { t } from './i18n-core.js'
export { EXDB }
export const EXIDX = {}
@@ -28,9 +30,11 @@ export const allExercises = st => [...(st.customEx || []), ...EXDB]
// Media normally sits next to the app (img/ and gif/, mounted into the web container).
// A build can point them somewhere else — the demo build pulls them off a CDN instead of
-// shipping ~140 MB of images into the deployment.
-const IMG_BASE = import.meta.env.VITE_IMG_BASE || 'img/'
-const GIF_BASE = import.meta.env.VITE_GIF_BASE || 'gif/'
+// shipping ~140 MB of images into the deployment. `import.meta.env` is undefined in plain
+// Node; guard with `?.` so this module is loadable from Node without Vite (see i18n-core.js).
+const ENV = import.meta.env || {}
+const IMG_BASE = ENV.VITE_IMG_BASE || 'img/'
+const GIF_BASE = ENV.VITE_GIF_BASE || 'gif/'
export const imgSrc = ex => IMG_BASE + ex.img
export const gifSrc = ex => GIF_BASE + ex.gif
diff --git a/frontend/src/lib/format.js b/frontend/src/lib/format.js
index de25f401f..8eb4a460d 100644
--- a/frontend/src/lib/format.js
+++ b/frontend/src/lib/format.js
@@ -1,5 +1,7 @@
// Formatting + date helpers (ported from the vanilla app, unit taken from the store where needed).
-import { dateLocale, t } from './i18n.js'
+// Imports i18n-core (not i18n.js) so this pure helper stays loadable from plain Node — that's
+// what lets the MCP server reuse it instead of reimplementing number/date formatting.
+import { dateLocale, t } from './i18n-core.js'
export const todayISO = () => {
const d = new Date()
return d.getFullYear() + '-' + String(d.getMonth() + 1).padStart(2, '0') + '-' + String(d.getDate()).padStart(2, '0')
diff --git a/frontend/src/lib/i18n-core.js b/frontend/src/lib/i18n-core.js
new file mode 100644
index 000000000..7bc5447fe
--- /dev/null
+++ b/frontend/src/lib/i18n-core.js
@@ -0,0 +1,58 @@
+// Pure, runtime-agnostic core of the i18n module.
+//
+// Split out of i18n.js so the pure-function training helpers (format.js, exercises.js and
+// everything they import transitively — history, progression, onerm, muscles, demoSeed) can
+// be required from plain Node too. That reuse is what lets the MCP server compute 1RM, muscle
+// balance and progression prescriptions without re-implementing them — the same code that
+// produces the numbers shown in the UI produces the numbers an LLM answers with.
+//
+// Everything browser-specific (React subscription hook, Vite's import.meta.glob lazy-loads of
+// locale packs) stays in i18n.js, which re-exports these readers verbatim.
+
+export const LANGS = {
+ en: 'English', de: 'Deutsch', es: 'Español', fr: 'Français', it: 'Italiano',
+ pt: 'Português', pl: 'Polski', tr: 'Türkçe', ru: 'Русский', zh: '中文',
+ ko: '한국어', hi: 'हिन्दी'
+}
+export const INSTR_LANGS = ['en', 'es', 'fr', 'it', 'tr', 'ru', 'zh', 'hi', 'pl', 'ko']
+export const DATE_LOCALES = {
+ en: 'en-GB', de: 'de-DE', es: 'es-ES', fr: 'fr-FR', it: 'it-IT', pt: 'pt-PT',
+ pl: 'pl-PL', tr: 'tr-TR', ru: 'ru-RU', zh: 'zh-CN', ko: 'ko-KR', hi: 'hi-IN'
+}
+
+// Mutable module state. `lang`/`dict`/`instr` are the readers' inputs; the browser-side
+// `setLang` (in i18n.js) is the only writer, so they stay private to this module.
+let lang = 'en'
+let dict = {}
+let instr = null // { exId: [steps] } for the current language, null = English
+let version = 0
+
+export const getLang = () => lang
+export const dateLocale = () => DATE_LOCALES[lang] || 'en-GB'
+export const getVersion = () => version
+
+// Translate a source string; {0},{1}… are replaced with args (also on the English fallback).
+export function t(s, ...args) {
+ let v = dict[s] || s
+ for (let i = 0; i < args.length; i++) v = v.replaceAll('{' + i + '}', args[i])
+ return v
+}
+
+// Instructions for an exercise in the current language (English steps as fallback).
+export const instrFor = ex => (instr && instr[ex.id]) || ex.st || []
+
+/**
+ * Internal setter invoked by i18n.js's `setLang` once the locale pack has been loaded.
+ * Kept here — rather than just exported as `setLang` — because loading packs requires
+ * Vite's `import.meta.glob`, which would drag this file straight back into not being
+ * Node-safe. Returns the new version so the caller can notify its React subscribers.
+ *
+ * Both `dict` and `instr` may be passed as `null` to reset to English.
+ */
+export function _setLangState(newLang, newDict, newInstr) {
+ lang = LANGS[newLang] ? newLang : 'en'
+ dict = lang === 'en' ? {} : (newDict || {})
+ instr = lang === 'en' || !INSTR_LANGS.includes(lang) ? null : (newInstr || null)
+ version++
+ return version
+}
diff --git a/frontend/src/lib/i18n.js b/frontend/src/lib/i18n.js
index ebade434d..498eadc48 100644
--- a/frontend/src/lib/i18n.js
+++ b/frontend/src/lib/i18n.js
@@ -1,56 +1,45 @@
-// Tiny dependency-free i18n. English source strings are the keys; locale files in
-// src/locales/ map them to translations and are lazy-loaded (Vite code-splits each
-// import.meta.glob entry), so the initial bundle stays English-only.
-// Exercise instructions come from separately generated packs in src/instr/ (one per
-// language, from the upstream dataset) — also lazy-loaded on language switch.
+// Browser-only shell of the i18n module.
+//
+// Pure state, constants and readers (t, dateLocale, instrFor, getLang, LANGS, INSTR_LANGS)
+// live in i18n-core.js so plain Node can reuse the training logic without a Vite environment.
+// This file adds the two pieces that genuinely need the browser: the async `setLang` (which
+// lazy-loads locale packs via import.meta.glob) and the React subscription hook `useLang`
+// (which re-renders components on language switch).
+
import { useSyncExternalStore } from 'react'
+import {
+ LANGS, INSTR_LANGS, DATE_LOCALES,
+ getLang, dateLocale, t, instrFor, getVersion, _setLangState
+} from './i18n-core.js'
-// UI languages. de/pt have no instruction pack upstream — instructions fall back to English.
-export const LANGS = {
- en: 'English', de: 'Deutsch', es: 'Español', fr: 'Français', it: 'Italiano',
- pt: 'Português', pl: 'Polski', tr: 'Türkçe', ru: 'Русский', zh: '中文',
- ko: '한국어', hi: 'हिन्दी'
-}
-export const INSTR_LANGS = ['en', 'es', 'fr', 'it', 'tr', 'ru', 'zh', 'hi', 'pl', 'ko']
-const DATE_LOCALES = {
- en: 'en-GB', de: 'de-DE', es: 'es-ES', fr: 'fr-FR', it: 'it-IT', pt: 'pt-PT',
- pl: 'pl-PL', tr: 'tr-TR', ru: 'ru-RU', zh: 'zh-CN', ko: 'ko-KR', hi: 'hi-IN'
-}
+// Re-export everything verbatim so existing callers (`import { t } from '../lib/i18n.js'`)
+// keep working unchanged — the split is invisible at the import site.
+export { LANGS, INSTR_LANGS, DATE_LOCALES, getLang, dateLocale, t, instrFor }
+// Vite code-splits each locale pack into its own chunk via import.meta.glob; instructions use
+// the same mechanism in src/instr/. Both are lazy, so the production bundle ships English only.
const localePacks = import.meta.glob('../locales/*.js')
const instrPacks = import.meta.glob('../instr/*.js')
-let lang = 'en'
-let dict = {}
-let instr = null // { exId: [steps] } for the current language, null = English
-let version = 0
+// React subscription bookkeeping — kept here, not in core, so core has zero React coupling.
const subs = new Set()
-const notify = () => { version++; subs.forEach(f => f()) }
-
-export const getLang = () => lang
-export const dateLocale = () => DATE_LOCALES[lang] || 'en-GB'
-
-// Translate a source string; {0},{1}… are replaced with args (also on the English fallback).
-export function t(s, ...args) {
- let v = dict[s] || s
- for (let i = 0; i < args.length; i++) v = v.replaceAll('{' + i + '}', args[i])
- return v
-}
-// Instructions for an exercise in the current language (English steps as fallback).
-export const instrFor = ex => (instr && instr[ex.id]) || ex.st || []
+const notify = () => { subs.forEach(f => f()) }
export async function setLang(l) {
if (!LANGS[l]) l = 'en'
- if (l === lang && version > 0) return
- lang = l
+ // _setLangState mutates core state and bumps the version; we read it back for the
+ // subscription selector below.
+ if (l === getLang() && getVersion() > 0) return
+ let dict = {}, instr = null
try {
dict = l === 'en' ? {} : (await localePacks['../locales/' + l + '.js']()).default
instr = l === 'en' || !INSTR_LANGS.includes(l) ? null : (await instrPacks['../instr/' + l + '.js']()).default
} catch (e) { dict = {}; instr = null }
+ _setLangState(l, dict, instr)
notify()
}
// Re-renders the subscribing component (and its children) whenever the language changes.
export function useLang() {
- return useSyncExternalStore(fn => { subs.add(fn); return () => subs.delete(fn) }, () => version)
+ return useSyncExternalStore(fn => { subs.add(fn); return () => subs.delete(fn) }, getVersion)
}