From b047d352c164948daadce10c043398e2ba852b66 Mon Sep 17 00:00:00 2001 From: Bastien Chanot Date: Sun, 12 Jul 2026 16:23:07 +0200 Subject: [PATCH] feat(species): locale-aware species labels via labelEn MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit B-6: SpeciesManifest only carried labelFr — EN users saw 'Cheval'. labelEn required on the manifest, speciesLabel(s, locale) helper in registry; AnimalForm dropdown + SvgEditor picker localized (locale passed into initSvgEditor, re-inits on locale change). Scaffolder template + ADDING-A-SPECIES.md + registry test updated. --- docs/ADDING-A-SPECIES.md | 3 ++- scripts/scaffold-species.mjs | 9 ++++++--- src/app/routes/SvgEditorRoute.tsx | 14 ++++++++------ src/components/report/AnimalForm.tsx | 6 ++++-- src/domain/species/equine/index.ts | 1 + src/domain/species/registry.ts | 6 ++++++ src/domain/species/types.ts | 2 ++ tests/unit/species.registry.test.ts | 1 + 8 files changed, 30 insertions(+), 12 deletions(-) diff --git a/docs/ADDING-A-SPECIES.md b/docs/ADDING-A-SPECIES.md index b7d1954a..c0cc099f 100644 --- a/docs/ADDING-A-SPECIES.md +++ b/docs/ADDING-A-SPECIES.md @@ -113,9 +113,10 @@ files by absolute path starting with `/species/...`. import type { SpeciesManifest } from '@/domain/species/types' import { CANINE_VERTEBRAL_FORMULA } from './vertebrae' -export const canine: SpeciesManifest = { +const canine: SpeciesManifest = { id: 'canine', labelFr: 'Chien', + labelEn: 'Dog', mainSvg: '/species/canine/Dog.svg', details: { head: '/species/canine/Dog_head.svg', diff --git a/scripts/scaffold-species.mjs b/scripts/scaffold-species.mjs index 5b08ed77..c6f03e51 100755 --- a/scripts/scaffold-species.mjs +++ b/scripts/scaffold-species.mjs @@ -65,16 +65,17 @@ function capitalize(s) { return s.charAt(0).toUpperCase() + s.slice(1) } -function indexTs({ id, labelFr, mainSvgPath, details, formulaConstName }) { +function indexTs({ id, labelFr, labelEn, mainSvgPath, details, formulaConstName }) { const detailLines = details .map((d) => ` ${d.key}: '${d.path}',`) .join('\n') return `import type { SpeciesManifest } from '@/domain/species/types' import { ${formulaConstName} } from './vertebrae' -export const ${id}: SpeciesManifest = { +const ${id}: SpeciesManifest = { id: '${id}', labelFr: '${labelFr.replace(/'/g, "\\'")}', + labelEn: '${labelEn.replace(/'/g, "\\'")}', mainSvg: '${mainSvgPath}', details: { ${detailLines} @@ -83,6 +84,8 @@ ${detailLines} findings: () => import('./pathologies'), } +// Default export is the convention picked up by the registry's +// import.meta.glob() auto-discovery. export default ${id} ` } @@ -184,7 +187,7 @@ async function main() { const formulaConstName = `${id.replace(/-/g, '_').toUpperCase()}_VERTEBRAL_FORMULA` const speciesDir = resolve(PROJECT_ROOT, 'src/domain/species', id) const targets = { - 'index.ts': indexTs({ id, labelFr, mainSvgPath, details, formulaConstName }), + 'index.ts': indexTs({ id, labelFr, labelEn, mainSvgPath, details, formulaConstName }), 'vertebrae.ts': vertebraeTs({ formulaConstName, labelFr, formula }), 'pathologies.ts': pathologiesTs(), 'pathologies.json': pathologiesJsonTemplate({ id, labelFr, labelEn }), diff --git a/src/app/routes/SvgEditorRoute.tsx b/src/app/routes/SvgEditorRoute.tsx index 8ae1c383..e234961e 100644 --- a/src/app/routes/SvgEditorRoute.tsx +++ b/src/app/routes/SvgEditorRoute.tsx @@ -4,7 +4,8 @@ import AppHeader from '@/components/brand/AppHeader' import { useT } from '@/i18n/useT' import { useLocaleStore } from '@/state/localeStore' import { getDict } from '@/i18n/index' -import { getAllSpecies } from '@/domain/species/registry' +import type { Locale } from '@/i18n/types' +import { getAllSpecies, speciesLabel } from '@/domain/species/registry' import '@/styles/svg-editor.css' type TFn = (key: string) => string @@ -35,7 +36,7 @@ export default function SvgEditorRoute() { const dict = getDict(locale) const tFn: TFn = (key: string) => dict[key] ?? key - const cleanup = initSvgEditor(canvas, canvasInner, sidebar, tFn) + const cleanup = initSvgEditor(canvas, canvasInner, sidebar, tFn, locale) cleanupRef.current = cleanup return () => { @@ -248,6 +249,7 @@ function initSvgEditor( canvasInner: HTMLDivElement, _sidebar: HTMLDivElement, t: TFn, + locale: Locale, ): () => void { const ac = new AbortController() const sig = { signal: ac.signal } @@ -401,13 +403,13 @@ function initSvgEditor( } function buildProjectPicker(container: HTMLElement) { - const speciesLabel = document.createElement('label') - speciesLabel.textContent = t('svg_editor.species') + const speciesLabelEl = document.createElement('label') + speciesLabelEl.textContent = t('svg_editor.species') const speciesSel = document.createElement('select') speciesList.forEach((sp) => { const opt = document.createElement('option') opt.value = sp.id - opt.textContent = sp.labelFr + opt.textContent = speciesLabel(sp, locale) speciesSel.appendChild(opt) }) @@ -459,7 +461,7 @@ function initSvgEditor( }) }) - container.append(speciesLabel, speciesSel, viewLabel, viewSel, loadBtn) + container.append(speciesLabelEl, speciesSel, viewLabel, viewSel, loadBtn) } function buildUploadPicker(container: HTMLElement) { diff --git a/src/components/report/AnimalForm.tsx b/src/components/report/AnimalForm.tsx index c3a04108..effc2527 100644 --- a/src/components/report/AnimalForm.tsx +++ b/src/components/report/AnimalForm.tsx @@ -2,8 +2,9 @@ import type { Animal, Owner } from '@/domain/schema/animal' import Label from '@/components/ui/Label' import Input from '@/components/ui/Input' import Select from '@/components/ui/Select' -import { getAllSpecies } from '@/domain/species/registry' +import { getAllSpecies, speciesLabel } from '@/domain/species/registry' import { useT } from '@/i18n/useT' +import { useLocaleStore } from '@/state/localeStore' export interface AnimalFormProps { animal: Animal @@ -14,6 +15,7 @@ export interface AnimalFormProps { export default function AnimalForm({ animal, owner, onChangeAnimal, onChangeOwner }: AnimalFormProps) { const t = useT() + const locale = useLocaleStore((s) => s.locale) const species = getAllSpecies() const setA = (patch: Partial) => onChangeAnimal({ ...animal, ...patch }) return ( @@ -29,7 +31,7 @@ export default function AnimalForm({ animal, owner, onChangeAnimal, onChangeOwne onChange={(e) => setA({ speciesId: e.target.value })} > {species.map((s) => ( - + ))} diff --git a/src/domain/species/equine/index.ts b/src/domain/species/equine/index.ts index 4be81161..cac33232 100644 --- a/src/domain/species/equine/index.ts +++ b/src/domain/species/equine/index.ts @@ -4,6 +4,7 @@ import { EQUINE_VERTEBRAL_FORMULA } from './vertebrae' const equine: SpeciesManifest = { id: 'equine', labelFr: 'Cheval', + labelEn: 'Horse', mainSvg: '/species/equine/Horse.svg', details: { head: '/species/equine/Horse_head.svg', diff --git a/src/domain/species/registry.ts b/src/domain/species/registry.ts index b4e36a65..59257da8 100644 --- a/src/domain/species/registry.ts +++ b/src/domain/species/registry.ts @@ -1,4 +1,5 @@ import type { SpeciesManifest } from './types' +import type { Locale } from '@/i18n/types' /** * Auto-discovers every species manifest in `src/domain/species/*\/index.ts` @@ -49,3 +50,8 @@ export function getAllSpecies(): SpeciesManifest[] { a.labelFr.localeCompare(b.labelFr, 'fr'), ) } + +/** Display label of a species for the given locale. */ +export function speciesLabel(s: SpeciesManifest, locale: Locale): string { + return locale === 'en' ? s.labelEn : s.labelFr +} diff --git a/src/domain/species/types.ts b/src/domain/species/types.ts index bd606e38..134767dd 100644 --- a/src/domain/species/types.ts +++ b/src/domain/species/types.ts @@ -17,6 +17,8 @@ export interface SpeciesManifest { id: string /** Display name in French */ labelFr: string + /** Display name in English */ + labelEn: string /** Absolute public path to the main anatomy SVG */ mainSvg: string /** diff --git a/tests/unit/species.registry.test.ts b/tests/unit/species.registry.test.ts index f1a2b040..1d90e9a0 100644 --- a/tests/unit/species.registry.test.ts +++ b/tests/unit/species.registry.test.ts @@ -6,6 +6,7 @@ describe('species registry', () => { const eq = getSpecies('equine') expect(eq).toBeDefined() expect(eq?.labelFr).toBe('Cheval') + expect(eq?.labelEn).toBe('Horse') expect(eq?.mainSvg).toBe('/species/equine/Horse.svg') expect(eq?.details.head).toBe('/species/equine/Horse_head.svg') expect(eq?.vertebralFormula).toEqual({ C: 7, T: 18, L: 6, S: 5, Cd: 18 })