i18n: split the pure core out so the lib is Node-loadable
The pure training helpers (history, progression, onerm, muscles, format, exercises) are deliberately self-contained — that is what lets them ship with their own vitest suites and got them reused across the React and mobile builds. They were one Vite-only call away from being loadable from plain Node too: `import.meta.glob` for the lazy locale packs, run at the top of i18n.js. Anything that imported `t` or `dateLocale` via the chain (i.e. every helper in src/lib) inherited the dependency. Splitting the runtime-agnostic state, constants and readers (t, dateLocale, instrFor, getLang, LANGS, INSTR_LANGS, DATE_LOCALES) into i18n-core.js lets plain Node require them. i18n.js keeps the Vite-only bits (the async setLang that lazy-loads locale packs via import.meta.glob and the React subscription hook useLang) and re-exports everything from core, so none of the 23 existing import sites change. exercises.js had one direct `import.meta.env` access for IMG_BASE/GIF_BASE — guarded with `?.` so the module resolves without Vite present. format.js and exercises.js import i18n-core directly; everything else still imports i18n.js. No public exports changed. 92/92 vitest tests pass; vite build still code-splits the locale packs the same way.
This commit is contained in:
1 parent
ab0c98ba85
commit
fadeb17b6c
4 files changed
+94
-41
No files matched your search
@@ -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
|
||||
|
||||
|
||||
@@ -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')
|
||||
|
||||
@@ -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
|
||||
}
|
||||
+25
-36
@@ -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)
|
||||
}
|
||||
Reference in new issue
Block a user