From 1673302b09762b50c9b0956838a76afc661fc71a Mon Sep 17 00:00:00 2001 From: Bastien Chanot Date: Fri, 22 May 2026 06:16:49 +0200 Subject: [PATCH] feat(i18n): SVG editor header navbar + full FR/EN translation Compact header (Brand + LanguageSwitcher) above the SVG editor. All 58 hardcoded strings in JSX template and vanilla JS replaced with t() calls via TFn passed to initSvgEditor. Editor re-inits on locale change. CSS drop overlay uses data-drop-label attr for translated text. Co-Authored-By: Claude --- src/app/routes/SvgEditorRoute.tsx | 442 ++++++++++++++++-------------- src/styles/svg-editor.css | 5 +- 2 files changed, 246 insertions(+), 201 deletions(-) diff --git a/src/app/routes/SvgEditorRoute.tsx b/src/app/routes/SvgEditorRoute.tsx index 23564127..010bf4b5 100644 --- a/src/app/routes/SvgEditorRoute.tsx +++ b/src/app/routes/SvgEditorRoute.tsx @@ -1,11 +1,23 @@ import { useEffect, useRef } from 'react' import { Link } from 'react-router-dom' +import Brand from '@/components/brand/Brand' +import LanguageSwitcher from '@/components/brand/LanguageSwitcher' +import { useConfig } from '@/app/providers/ConfigProvider' +import { useT } from '@/i18n/useT' +import { useLocaleStore } from '@/state/localeStore' +import { getDict } from '@/i18n/index' import '@/styles/svg-editor.css' +type TFn = (key: string) => string + export default function SvgEditorRoute() { const canvasRef = useRef(null) const canvasInnerRef = useRef(null) const sidebarRef = useRef(null) + const cleanupRef = useRef<(() => void) | null>(null) + const cfg = useConfig() + const t = useT() + const locale = useLocaleStore((s) => s.locale) useEffect(() => { const meta = document.createElement('meta') @@ -23,179 +35,204 @@ export default function SvgEditorRoute() { const sidebar = sidebarRef.current if (!canvas || !canvasInner || !sidebar) return - const cleanup = initSvgEditor(canvas, canvasInner, sidebar) + const dict = getDict(locale) + const tFn: TFn = (key: string) => dict[key] ?? key + const cleanup = initSvgEditor(canvas, canvasInner, sidebar, tFn) + cleanupRef.current = cleanup - return cleanup - }, []) + return () => { + cleanup() + cleanupRef.current = null + } + // Re-init when locale changes so all vanilla JS strings update + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [locale]) return ( -
-
-
-
-
+
+
+
+ + + + {cfg.tagline} +
+ +
-
-
-
- - ← - -
-

SVG Anatomy Editor

-
Points, zones & clickable categories
+
+
+
+
+
+ +
+
+
+ + ← + +
+

{t('svg_editor.title')}

+
{t('svg_editor.subtitle')}
+
-
-
+ -
-
-

File

- - - -
- -
-

Mode

- - - -
- -
- - - - -
-
- -
-

Selected element

-
- — + + +
+ + +
+ + - This zone opens a detail view - -
- - +
+

+ {t('svg_editor.categories')}{' '} + {t('svg_editor.shared_color')} +

+
+
+ + +
-
- - - - - - - +
+

{t('svg_editor.selected_element')}

+
+ —
-
-
- -
- - -
-
-
-
- Polygon editing: -
- Drag a yellow handle to move it -
- Click a white mid-handle to insert a vertex -
- Alt+click or right-click a yellow handle to delete it -
- -
-
- -
-
-
-

- Elements (0) -

- -
+
+ +
+ + +
+
+ +
+ + + + + + + +
+
+
+ +
+ + +
+
+
+
+ {t('svg_editor.polygon_editing')} +
+ {t('svg_editor.polygon_drag')} +
+ {t('svg_editor.polygon_insert')} +
+ {t('svg_editor.polygon_delete')} +
+ +
+
+ +
+
+ +
+

+ {t('svg_editor.elements')} (0) +

+ +
+
@@ -207,6 +244,7 @@ function initSvgEditor( canvas: HTMLDivElement, canvasInner: HTMLDivElement, _sidebar: HTMLDivElement, + t: TFn, ): () => void { const ac = new AbortController() const sig = { signal: ac.signal } @@ -232,8 +270,8 @@ function initSvgEditor( const CATEGORY_TEMPLATES: Record = { articulation: { color: '#fb5607', name: 'Articulations' }, - charniere: { color: '#d00000', name: 'Charnières vertébrales' }, - 'cadran-crane': { color: '#06a77d', name: 'Cadrans du crâne' }, + charniere: { color: '#d00000', name: 'Charnieres vertebrales' }, + 'cadran-crane': { color: '#06a77d', name: 'Cadrans du crane' }, 'os-overlay': { color: '#f72585', name: 'Os overlay' }, 'spine-zone': { color: '#7209b7', name: 'Zones colonne' }, } @@ -294,22 +332,22 @@ function initSvgEditor( icon.textContent = '📐' const h2 = document.createElement('h2') - h2.textContent = 'SVG Anatomy Editor' + h2.textContent = t('svg_editor.welcome_title') const p = document.createElement('p') - p.textContent = 'Upload an SVG file to begin. If your file already has points/zones with ' + p.textContent = t('svg_editor.welcome_text') const code1 = document.createElement('code') code1.textContent = 'data-anno-id' const code2 = document.createElement('code') code2.textContent = 'data-anno-category' p.appendChild(code1) - p.appendChild(document.createTextNode(' and ')) + p.appendChild(document.createTextNode(' & ')) p.appendChild(code2) - p.appendChild(document.createTextNode(', they will be detected and configured automatically.')) + p.appendChild(document.createTextNode(t('svg_editor.welcome_text_suffix'))) const btn = document.createElement('button') btn.className = 'primary welcome-btn' - btn.textContent = 'Choose an SVG file' + btn.textContent = t('svg_editor.choose_file') btn.addEventListener('click', () => { const fi = document.getElementById('svg-editor-file-import') as HTMLInputElement | null fi?.click() @@ -317,7 +355,7 @@ function initSvgEditor( const hint = document.createElement('div') hint.className = 'welcome-hint' - hint.textContent = 'or drag & drop an SVG file anywhere' + hint.textContent = t('svg_editor.welcome_drag_hint') welcome.append(icon, h2, p, btn, hint) canvasInner.appendChild(welcome) @@ -332,7 +370,7 @@ function initSvgEditor( getEl('svg-editor-count').textContent = '0' getEl('svg-editor-element-list').textContent = '' getEl('svg-editor-edit-section').style.display = 'none' - getEl('svg-editor-file-btn').textContent = 'Choose an SVG file' + getEl('svg-editor-file-btn').textContent = t('svg_editor.choose_file') } function setupGlobalDragDrop() { @@ -351,11 +389,11 @@ function initSvgEditor( const reader = new FileReader() reader.onload = (ev) => { loadSVG(ev.target?.result as string) - getEl('svg-editor-file-btn').textContent = 'Change SVG file' + getEl('svg-editor-file-btn').textContent = t('svg_editor.change_file') } reader.readAsText(file, 'UTF-8') } else { - alert('Drop an SVG file.') + alert(t('svg_editor.alert_drop_svg')) } }, sig) } @@ -365,7 +403,7 @@ function initSvgEditor( const doc = parser.parseFromString(svgText, 'image/svg+xml') const parsedSvg = doc.querySelector('svg') if (!parsedSvg) { - alert('Invalid file: no SVG element found.') + alert(t('svg_editor.alert_invalid_svg')) showWelcomeScreen() return } @@ -374,7 +412,7 @@ function initSvgEditor( canvasInner.appendChild(document.importNode(parsedSvg, true)) svgRoot = canvasInner.querySelector('svg') if (!svgRoot) { - alert('Invalid file: no SVG element found.') + alert(t('svg_editor.alert_invalid_svg')) showWelcomeScreen() return } @@ -580,7 +618,7 @@ function initSvgEditor( selected.setAttribute('points', pointsToStr(points)) showPolygonHandles(selected) } else { - alert('A polygon must have at least 3 vertices.') + alert(t('svg_editor.alert_min_3_vertices')) } e.preventDefault() e.stopPropagation() @@ -736,7 +774,7 @@ function initSvgEditor( const reader = new FileReader() reader.onload = (ev) => { loadSVG(ev.target?.result as string) - getEl('svg-editor-file-btn').textContent = 'Change SVG file' + getEl('svg-editor-file-btn').textContent = t('svg_editor.change_file') } reader.readAsText(file, 'UTF-8') ;(e.target as HTMLInputElement).value = '' @@ -771,7 +809,7 @@ function initSvgEditor( function setMode(m: string) { if (!svgRoot && m !== 'select') { - alert('Upload an SVG first.') + alert(t('svg_editor.alert_upload_first')) return } mode = m @@ -940,7 +978,7 @@ function initSvgEditor( .getAttribute('points')! .split(/\s+/) .filter((p) => p).length - txt = `polygon (${pts} vertices)` + txt = t('svg_editor.polygon_vertices').replace('{count}', String(pts)) } getEl('svg-editor-coords-display').textContent = txt } @@ -1045,7 +1083,7 @@ function initSvgEditor( function deleteSelected() { if (!selected) return - if (!confirm('Delete this element?')) return + if (!confirm(t('svg_editor.confirm_delete_element'))) return const id = selected.getAttribute('data-anno-id')! const cat = selected.getAttribute('data-anno-category') const parent = selected.parentNode as Element | null @@ -1073,6 +1111,8 @@ function initSvgEditor( } const id = 'point-' + Date.now().toString(36) + const frName = t('svg_editor.new_point_fr') + const enName = t('svg_editor.new_point_en') const hit = document.createElementNS('http://www.w3.org/2000/svg', 'circle') hit.setAttribute('cx', String(Math.round(x))) @@ -1081,14 +1121,14 @@ function initSvgEditor( hit.setAttribute('fill', 'transparent') hit.setAttribute('pointer-events', 'all') hit.setAttribute('data-anno-id', id) - hit.setAttribute('data-anno-name-fr', 'Nouveau point') - hit.setAttribute('data-anno-name-en', 'New point') + hit.setAttribute('data-anno-name-fr', frName) + hit.setAttribute('data-anno-name-en', enName) hit.setAttribute('data-anno-category', cat) if (CATEGORIES[cat]?.name) hit.setAttribute('data-anno-category-name', CATEGORIES[cat].name) if (CATEGORIES[cat]?.special) hit.setAttribute('data-anno-special', CATEGORIES[cat].special!) hit.setAttribute('style', 'cursor:pointer') const t1 = document.createElementNS('http://www.w3.org/2000/svg', 'title') - t1.textContent = 'Nouveau point' + t1.textContent = frName hit.appendChild(t1) overlayG.appendChild(hit) @@ -1103,13 +1143,13 @@ function initSvgEditor( vis.setAttribute('pointer-events', 'none') vis.setAttribute('id', id) vis.setAttribute('data-anno-id', id) - vis.setAttribute('data-anno-name-fr', 'Nouveau point') - vis.setAttribute('data-anno-name-en', 'New point') + vis.setAttribute('data-anno-name-fr', frName) + vis.setAttribute('data-anno-name-en', enName) vis.setAttribute('data-anno-category', cat) if (CATEGORIES[cat]?.name) vis.setAttribute('data-anno-category-name', CATEGORIES[cat].name) if (CATEGORIES[cat]?.special) vis.setAttribute('data-anno-special', CATEGORIES[cat].special!) const t2 = document.createElementNS('http://www.w3.org/2000/svg', 'title') - t2.textContent = 'Nouveau point' + t2.textContent = frName vis.appendChild(t2) overlayG.appendChild(vis) @@ -1159,7 +1199,7 @@ function initSvgEditor( function finalizePolygon() { if (!svgRoot) return if (polygonPoints.length < 3) { - alert('Need at least 3 points.') + alert(t('svg_editor.alert_min_3')) return } @@ -1179,6 +1219,8 @@ function initSvgEditor( } const id = memo ? memo.id : 'zone-' + Date.now().toString(36) + const frName = memo ? memo.fr || t('svg_editor.new_zone_fr') : t('svg_editor.new_zone_fr') + const enName = memo ? memo.en || t('svg_editor.new_zone_en') : t('svg_editor.new_zone_en') const poly = document.createElementNS('http://www.w3.org/2000/svg', 'polygon') poly.setAttribute( 'points', @@ -1192,17 +1234,17 @@ function initSvgEditor( poly.setAttribute('style', 'cursor:pointer') poly.setAttribute('id', id) poly.setAttribute('data-anno-id', id) - poly.setAttribute('data-anno-name-fr', memo ? memo.fr || 'Nouvelle zone' : 'Nouvelle zone') - poly.setAttribute('data-anno-name-en', memo ? memo.en || 'New zone' : 'New zone') + poly.setAttribute('data-anno-name-fr', frName) + poly.setAttribute('data-anno-name-en', enName) poly.setAttribute('data-anno-category', cat) if (CATEGORIES[cat]?.name) poly.setAttribute('data-anno-category-name', CATEGORIES[cat].name) if (CATEGORIES[cat]?.special) poly.setAttribute('data-anno-special', CATEGORIES[cat].special!) - const t = document.createElementNS('http://www.w3.org/2000/svg', 'title') - t.textContent = + const titleEl = document.createElementNS('http://www.w3.org/2000/svg', 'title') + titleEl.textContent = poly.getAttribute('data-anno-name-fr') + ' / ' + poly.getAttribute('data-anno-name-en') - poly.appendChild(t) + poly.appendChild(titleEl) overlayG.appendChild(poly) cancelPolygon() @@ -1243,7 +1285,7 @@ function initSvgEditor( if (Object.keys(CATEGORIES).length === 0) { const emptyMsg = document.createElement('div') emptyMsg.style.cssText = 'font-size:12px; color:var(--color-ink-muted); padding:8px; text-align:center;' - emptyMsg.textContent = 'No categories yet. Use the form below to add one.' + emptyMsg.textContent = t('svg_editor.no_categories') list.appendChild(emptyMsg) return } @@ -1394,7 +1436,7 @@ Current: ${current || '(none)'}` const name = (getEl('svg-editor-new-cat-name') as HTMLInputElement).value.trim() const color = (getEl('svg-editor-new-cat-color') as HTMLInputElement).value if (!name) { - alert('Give the category a name.') + alert(t('svg_editor.alert_cat_name_required')) return } const slug = name @@ -1404,12 +1446,12 @@ Current: ${current || '(none)'}` .replace(/[^a-z0-9]+/g, '-') .replace(/^-|-$/g, '') if (!slug) { - alert('Invalid name.') + alert(t('svg_editor.alert_cat_invalid_name')) return } if (CATEGORIES[slug]) { - alert('This category already exists.') + alert(t('svg_editor.alert_cat_exists')) return } @@ -1424,8 +1466,10 @@ Current: ${current || '(none)'}` const count = svgRoot.querySelectorAll( `[data-anno-category="${cat}"]:not([fill="transparent"])`, ).length - let msg = `Delete category "${CATEGORIES[cat].name}"?` - if (count > 0) msg += `\n\nIt contains ${count} element(s) that will also be deleted.` + let msg = t('svg_editor.confirm_delete_cat').replace('{name}', CATEGORIES[cat].name) + if (count > 0) { + msg += t('svg_editor.confirm_delete_cat_count').replace('{count}', String(count)) + } if (!confirm(msg)) return svgRoot.querySelectorAll(`[data-anno-category="${cat}"]`).forEach((svgEl) => { @@ -1566,9 +1610,9 @@ Current: ${current || '(none)'}` m.setAttribute('r', '6') m.classList.add('polygon-midhandle') m.setAttribute('data-edge-idx', String(i)) - const t = document.createElementNS('http://www.w3.org/2000/svg', 'title') - t.textContent = 'Click to insert a vertex here' - m.appendChild(t) + const titleEl = document.createElementNS('http://www.w3.org/2000/svg', 'title') + titleEl.textContent = t('svg_editor.handle_insert') + m.appendChild(titleEl) handlesG.appendChild(m) } @@ -1579,9 +1623,9 @@ Current: ${current || '(none)'}` h.setAttribute('r', '8') h.classList.add('polygon-handle') h.setAttribute('data-vertex-idx', String(i)) - const t = document.createElementNS('http://www.w3.org/2000/svg', 'title') - t.textContent = 'Drag to move | Alt+click or right-click to delete' - h.appendChild(t) + const titleEl = document.createElementNS('http://www.w3.org/2000/svg', 'title') + titleEl.textContent = t('svg_editor.handle_drag') + h.appendChild(titleEl) handlesG!.appendChild(h) }) } @@ -1593,7 +1637,7 @@ Current: ${current || '(none)'}` function restartCurrentPolygon() { if (!selected || selected.tagName.toLowerCase() !== 'polygon') return - if (!confirm('Erase this polygon and redraw it?')) return + if (!confirm(t('svg_editor.confirm_redraw'))) return _polygonRestartMemo = { id: selected.getAttribute('data-anno-id') || '', fr: selected.getAttribute('data-anno-name-fr') || '', @@ -1646,7 +1690,7 @@ Current: ${current || '(none)'}` function exportSVG() { if (!svgRoot) { - alert('No SVG loaded. Upload a file first.') + alert(t('svg_editor.alert_no_svg_loaded')) return } const clone = svgRoot.cloneNode(true) as SVGSVGElement diff --git a/src/styles/svg-editor.css b/src/styles/svg-editor.css index 2081be6f..35e7a5e3 100644 --- a/src/styles/svg-editor.css +++ b/src/styles/svg-editor.css @@ -1,6 +1,7 @@ .svg-editor-root { display: flex; - height: 100vh; + flex: 1; + min-height: 0; overflow: hidden; font-family: var(--font-sans); color: var(--color-ink); @@ -426,7 +427,7 @@ .svg-editor-canvas.dragging-over { background: color-mix(in srgb, var(--color-accent) 8%, var(--color-surface)); } .svg-editor-canvas.dragging-over .welcome { color: var(--color-accent); } .svg-editor-canvas.dragging-over::after { - content: 'Drop SVG here'; + content: attr(data-drop-label); position: absolute; inset: 10px; display: flex;