Localization: 12 UI languages + exercise instructions in 10 (#7)
Adds a Language setting (Settings → Appearance) that translates the whole app: a tiny dependency-free i18n layer (English source strings as keys, per-language dictionaries in frontend/src/locales/) plus per-language exercise-instruction packs generated from the upstream dataset (frontend/src/instr/, via scripts/build-instructions.mjs). UI: en, de, es, fr, it, pt, pl, tr, ru, zh, ko, hi. Instructions: all of those except de/pt (not in the upstream dataset — fall back to English). Body-part/equipment/muscle labels and date formats are localized too; exercise names stay English (upstream limitation). Everything is lazy-loaded per language (Vite code-splitting), so the initial bundle is unchanged; the choice syncs with the profile like the theme. Closes #7.
This commit is contained in:
1 parent
b9f8d18bbd
commit
c05b0756ec
44 files changed
+3891
-274
No files matched your search
@@ -1,4 +1,5 @@
|
||||
// Formatting + date helpers (ported from the vanilla app, unit taken from the store where needed).
|
||||
import { dateLocale } from './i18n.js'
|
||||
export const todayISO = () => {
|
||||
const d = new Date()
|
||||
return d.getFullYear() + '-' + String(d.getMonth() + 1).padStart(2, '0') + '-' + String(d.getDate()).padStart(2, '0')
|
||||
@@ -13,7 +14,7 @@ export const MONTHS_LONG = ['January', 'February', 'March', 'April', 'May', 'Jun
|
||||
|
||||
export function fmtDate(iso, long) {
|
||||
const d = new Date(iso + 'T12:00:00')
|
||||
return d.toLocaleDateString('en-GB', long ? { weekday: 'short', day: 'numeric', month: 'short' } : { day: 'numeric', month: 'short' })
|
||||
return d.toLocaleDateString(dateLocale(), long ? { weekday: 'short', day: 'numeric', month: 'short' } : { day: 'numeric', month: 'short' })
|
||||
}
|
||||
export function fmtDur(ms) {
|
||||
const m = Math.floor(ms / 60000)
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
// 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.
|
||||
import { useSyncExternalStore } from 'react'
|
||||
|
||||
// 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'
|
||||
}
|
||||
|
||||
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
|
||||
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 || []
|
||||
|
||||
export async function setLang(l) {
|
||||
if (!LANGS[l]) l = 'en'
|
||||
if (l === lang && version > 0) return
|
||||
lang = l
|
||||
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 }
|
||||
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)
|
||||
}
|
||||
Reference in new issue
Block a user