feat(i18n): migrate site + PDF to locale-aware rendering
- formatDate(iso, locale) replaces formatDateFr in src/lib/dates.ts
- exportReportToPdf(report, { locale }) takes explicit locale option
- LanguageSwitcher segmented FR | EN toggle added; integrated in
ReportHeader, HomeRoute, NewReportRoute
- All routes (Home, NewReport, Editor, NotFound) + observation
components (FindingsTreePicker, ObservationCard, FreeTextFindingInput,
SeveritySelector, VertebraPicker, ObservationList) + report forms
(PractitionerForm, AnimalForm, ConclusionPanel, NextAppointmentPicker)
route hardcoded FR via useT()
- PDF render functions (export.ts, header.ts, footer.ts,
observationPage.ts, conclusionPage.ts) take a locale arg and read
static labels from the dict snapshot at call time
- Catalog finding leaf labels rendered via labelFr/EnSnapshot per locale
- description_fr / scope_disclaimer stay FR-only (out of scope —
vet-expert authoring needed for EN copy)
- LanguageSwitcher unit test (2) + lib.dates locale tests (7) cover
the new behavior
151/151 tests pass, lint 0, typecheck clean, vite build OK
(902KB main + 15KB lazy pathologies chunk).
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
1 parent
359104ce88
commit
d6027266bc
25 files changed
+390
-207
No files matched your search
+18
-11
@@ -1,6 +1,8 @@
|
||||
import { PDFDocument } from 'pdf-lib'
|
||||
import type { Report } from '@/domain/schema/report'
|
||||
import type { SpeciesFindings } from '@/domain/schema/findings'
|
||||
import type { Locale } from '@/i18n/types'
|
||||
import { getDict } from '@/i18n'
|
||||
import { embedFonts } from './fonts'
|
||||
import { addObservationPage } from './render/observationPage'
|
||||
import { addConclusionPage } from './render/conclusionPage'
|
||||
@@ -10,6 +12,10 @@ import { loadSvg } from '@/anatomy/svgLoader'
|
||||
import { parseSvgBones } from '@/anatomy/svgConvention'
|
||||
import { getSpecies } from '@/domain/species/registry'
|
||||
|
||||
export interface PdfExportOptions {
|
||||
locale: Locale
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a `data-anno-id → French label` map by parsing every SVG the
|
||||
* species manifest knows about. Each SVG is loaded independently so a
|
||||
@@ -36,9 +42,6 @@ export async function buildZoneLabels(report: Report): Promise<Map<string, strin
|
||||
if (b.fr) labels.set(id, b.fr)
|
||||
})
|
||||
} catch (err) {
|
||||
// Asset unavailable (404 / fetch failure / parse error). Log and
|
||||
// continue — observers downstream fall back to the raw bone id,
|
||||
// which is preferable to crashing the whole export.
|
||||
console.warn(`buildZoneLabels: skipped ${src}`, err)
|
||||
}
|
||||
}),
|
||||
@@ -50,9 +53,7 @@ export async function buildZoneLabels(report: Report): Promise<Map<string, strin
|
||||
/**
|
||||
* Loads the parsed findings catalog for a species so the PDF renderer
|
||||
* can resolve a Finding's `zoneId` (JSON catalog key) back to its
|
||||
* human-readable zone name. Returns null if the species lacks a
|
||||
* catalog or the lazy import fails — the PDF still renders, but
|
||||
* per-zone groups fall back to the raw zoneId.
|
||||
* human-readable zone name.
|
||||
*/
|
||||
async function loadSpeciesFindings(
|
||||
speciesId: string,
|
||||
@@ -68,11 +69,16 @@ async function loadSpeciesFindings(
|
||||
}
|
||||
}
|
||||
|
||||
export async function exportReportToPdf(report: Report): Promise<Uint8Array> {
|
||||
export async function exportReportToPdf(
|
||||
report: Report,
|
||||
opts: PdfExportOptions,
|
||||
): Promise<Uint8Array> {
|
||||
const { locale } = opts
|
||||
const dict = getDict(locale)
|
||||
const pdfDoc = await PDFDocument.create()
|
||||
pdfDoc.setTitle(`Bilan ostéo — ${report.animal.name}`)
|
||||
pdfDoc.setTitle(`${dict['pdf.title.suffix.bilan']}${report.animal.name}`)
|
||||
pdfDoc.setAuthor(report.practitioner.name)
|
||||
pdfDoc.setSubject("Bilan d'ostéopathie animalière")
|
||||
pdfDoc.setSubject(dict['pdf.subject'])
|
||||
pdfDoc.setCreationDate(new Date())
|
||||
pdfDoc.setModificationDate(new Date())
|
||||
|
||||
@@ -89,12 +95,13 @@ export async function exportReportToPdf(report: Report): Promise<Uint8Array> {
|
||||
fonts,
|
||||
zoneLabels,
|
||||
findingsTree,
|
||||
locale,
|
||||
)
|
||||
}
|
||||
await addConclusionPage(pdfDoc, report, fonts)
|
||||
await addConclusionPage(pdfDoc, report, fonts, locale)
|
||||
|
||||
const pages = pdfDoc.getPages()
|
||||
pages.forEach((page, idx) => drawFooter(page, fonts, idx + 1, pages.length))
|
||||
pages.forEach((page, idx) => drawFooter(page, fonts, idx + 1, pages.length, locale))
|
||||
|
||||
await attachReportJson(pdfDoc, report)
|
||||
return pdfDoc.save()
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import { rgb, PageSizes, type PDFDocument, type PDFFont } from 'pdf-lib'
|
||||
import type { EmbeddedFonts } from '@/pdf/fonts'
|
||||
import type { Report } from '@/domain/schema/report'
|
||||
import type { Locale } from '@/i18n/types'
|
||||
import { getDict } from '@/i18n'
|
||||
import { drawHeader } from './header'
|
||||
import { formatDateFr } from '@/lib/dates'
|
||||
import { formatDate } from '@/lib/dates'
|
||||
|
||||
const MARGIN_X = 36
|
||||
const EYEBROW_Y_OFFSET = 95
|
||||
@@ -39,23 +41,25 @@ export async function addConclusionPage(
|
||||
pdfDoc: PDFDocument,
|
||||
report: Report,
|
||||
fonts: EmbeddedFonts,
|
||||
locale: Locale,
|
||||
): Promise<void> {
|
||||
const dict = getDict(locale)
|
||||
const page = pdfDoc.addPage(PageSizes.A4)
|
||||
const { width, height } = page.getSize()
|
||||
const max = width - MARGIN_X * 2
|
||||
|
||||
drawHeader(page, report, fonts)
|
||||
drawHeader(page, report, fonts, locale)
|
||||
|
||||
const eyebrowY = height - EYEBROW_Y_OFFSET
|
||||
const titleY = height - TITLE_Y_OFFSET
|
||||
page.drawText('SYNTHÈSE', {
|
||||
page.drawText(dict['pdf.eyebrow.synthesis'], {
|
||||
x: MARGIN_X,
|
||||
y: eyebrowY,
|
||||
size: 9,
|
||||
font: fonts.bodyBold,
|
||||
color: COLOR_ACCENT,
|
||||
})
|
||||
page.drawText('Conclusion', {
|
||||
page.drawText(dict['pdf.title.conclusion'], {
|
||||
x: MARGIN_X,
|
||||
y: titleY,
|
||||
size: 22,
|
||||
@@ -72,17 +76,17 @@ export async function addConclusionPage(
|
||||
let cursorY = titleY - SECTION_TOP_GAP
|
||||
|
||||
const sections: Array<{ label: string; body: string; color?: typeof COLOR_INK }> = [
|
||||
{ label: 'CONCLUSION', body: report.conclusion || '—' },
|
||||
{ label: 'RECOMMANDATIONS', body: report.recommendations || '—' },
|
||||
{ label: dict['pdf.section.conclusion'], body: report.conclusion || dict['pdf.empty'] },
|
||||
{ label: dict['pdf.section.recommendations'], body: report.recommendations || dict['pdf.empty'] },
|
||||
]
|
||||
if (report.nextAppointment) {
|
||||
const txt =
|
||||
report.nextAppointment.kind === 'date'
|
||||
? formatDateFr(report.nextAppointment.date)
|
||||
? formatDate(report.nextAppointment.date, locale)
|
||||
: report.nextAppointment.text
|
||||
sections.push({
|
||||
label: 'PROCHAIN RENDEZ-VOUS',
|
||||
body: txt || '—',
|
||||
label: dict['pdf.section.next_appointment'],
|
||||
body: txt || dict['pdf.empty'],
|
||||
color: COLOR_MUTED,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { rgb, type PDFPage } from 'pdf-lib'
|
||||
import type { EmbeddedFonts } from '@/pdf/fonts'
|
||||
import type { Locale } from '@/i18n/types'
|
||||
import { appConfig } from '@/config/env'
|
||||
import { formatDateFr, todayIso } from '@/lib/dates'
|
||||
import { formatDate, todayIso } from '@/lib/dates'
|
||||
import { getDict } from '@/i18n'
|
||||
|
||||
const MARGIN_X = 36
|
||||
const FOOTER_Y = 28
|
||||
@@ -11,11 +13,13 @@ export function drawFooter(
|
||||
fonts: EmbeddedFonts,
|
||||
pageNumber: number,
|
||||
totalPages: number,
|
||||
locale: Locale,
|
||||
): void {
|
||||
const dict = getDict(locale)
|
||||
const { width } = page.getSize()
|
||||
const muted = rgb(0.42, 0.39, 0.35)
|
||||
const left = appConfig.footerName
|
||||
const center = `Exporté le ${formatDateFr(todayIso())}`
|
||||
const center = `${dict['pdf.export_date_prefix']}${formatDate(todayIso(), locale)}`
|
||||
const right = `${pageNumber} / ${totalPages}`
|
||||
|
||||
page.drawText(left, { x: MARGIN_X, y: FOOTER_Y, size: 9, font: fonts.bodyRegular, color: muted })
|
||||
|
||||
@@ -1,12 +1,20 @@
|
||||
import { rgb, type PDFPage } from 'pdf-lib'
|
||||
import type { EmbeddedFonts } from '@/pdf/fonts'
|
||||
import type { Report } from '@/domain/schema/report'
|
||||
import { formatDateFr } from '@/lib/dates'
|
||||
import type { Locale } from '@/i18n/types'
|
||||
import { getDict } from '@/i18n'
|
||||
import { formatDate } from '@/lib/dates'
|
||||
|
||||
const MARGIN_X = 36
|
||||
const TOP_Y = 800
|
||||
|
||||
export function drawHeader(page: PDFPage, report: Report, fonts: EmbeddedFonts): void {
|
||||
export function drawHeader(
|
||||
page: PDFPage,
|
||||
report: Report,
|
||||
fonts: EmbeddedFonts,
|
||||
locale: Locale,
|
||||
): void {
|
||||
const dict = getDict(locale)
|
||||
const { width } = page.getSize()
|
||||
const ink = rgb(0.12, 0.11, 0.09)
|
||||
const muted = rgb(0.42, 0.39, 0.35)
|
||||
@@ -15,7 +23,9 @@ export function drawHeader(page: PDFPage, report: Report, fonts: EmbeddedFonts):
|
||||
report.practitioner.name,
|
||||
report.practitioner.cabinet,
|
||||
report.practitioner.contact,
|
||||
report.practitioner.siret ? `SIRET ${report.practitioner.siret}` : undefined,
|
||||
report.practitioner.siret
|
||||
? `${dict['pdf.siret_prefix']}${report.practitioner.siret}`
|
||||
: undefined,
|
||||
].filter(Boolean) as string[]
|
||||
|
||||
const y = TOP_Y
|
||||
@@ -33,12 +43,14 @@ export function drawHeader(page: PDFPage, report: Report, fonts: EmbeddedFonts):
|
||||
report.animal.name,
|
||||
report.animal.breed ? `(${report.animal.breed})` : '',
|
||||
report.animal.age ? `· ${report.animal.age}` : '',
|
||||
].filter(Boolean).join(' ')
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join(' ')
|
||||
|
||||
const right = [
|
||||
animalLine,
|
||||
`Propriétaire : ${report.owner.name}`,
|
||||
formatDateFr(report.date),
|
||||
`${dict['pdf.owner_prefix']}${report.owner.name}`,
|
||||
formatDate(report.date, locale),
|
||||
]
|
||||
right.forEach((line, i) => {
|
||||
const size = i === 0 ? 11 : 9
|
||||
|
||||
@@ -3,6 +3,8 @@ import type { EmbeddedFonts } from '@/pdf/fonts'
|
||||
import type { Report } from '@/domain/schema/report'
|
||||
import type { Finding, Observation } from '@/domain/schema/observation'
|
||||
import type { SpeciesFindings } from '@/domain/schema/findings'
|
||||
import type { Locale } from '@/i18n/types'
|
||||
import { getDict } from '@/i18n'
|
||||
import { drawHeader } from './header'
|
||||
import { rasterizeSvg } from './svgToPng'
|
||||
import { loadSvg } from '@/anatomy/svgLoader'
|
||||
@@ -271,15 +273,17 @@ export async function addObservationPage(
|
||||
fonts: EmbeddedFonts,
|
||||
zoneLabels: Map<string, string>,
|
||||
findingsTree: SpeciesFindings | null,
|
||||
locale: Locale,
|
||||
): Promise<void> {
|
||||
const dict = getDict(locale)
|
||||
const page = pdfDoc.addPage(PageSizes.A4)
|
||||
const { width, height } = page.getSize()
|
||||
drawHeader(page, report, fonts)
|
||||
drawHeader(page, report, fonts, locale)
|
||||
|
||||
const eyebrowY = height - EYEBROW_Y_OFFSET
|
||||
const titleY = height - TITLE_Y_OFFSET
|
||||
const obsLabel = String(obsIndex + 1).padStart(2, '0')
|
||||
page.drawText('OBSERVATION', {
|
||||
page.drawText(dict['pdf.eyebrow.observation'], {
|
||||
x: MARGIN_X,
|
||||
y: eyebrowY,
|
||||
size: 9,
|
||||
@@ -296,7 +300,8 @@ export async function addObservationPage(
|
||||
if (observation.severity) {
|
||||
const numW = fonts.headingRegular.widthOfTextAtSize(obsLabel, 22)
|
||||
const sev = observation.severity
|
||||
const sevText = sev.toUpperCase()
|
||||
const sevLabel = dict[`severity.${sev}`] ?? sev
|
||||
const sevText = sevLabel.toUpperCase()
|
||||
const sevSize = 9
|
||||
const sevTextW = fonts.bodyBold.widthOfTextAtSize(sevText, sevSize)
|
||||
const padX = 8
|
||||
@@ -337,13 +342,13 @@ export async function addObservationPage(
|
||||
|
||||
const items: Array<{ label: string; src: string; sourceId: string | undefined }> = []
|
||||
if (hasMainZones) {
|
||||
items.push({ label: 'VUE GÉNÉRALE', src: species.mainSvg, sourceId: undefined })
|
||||
items.push({ label: dict['pdf.view.general'], src: species.mainSvg, sourceId: undefined })
|
||||
}
|
||||
for (const detailId of detailIds) {
|
||||
const detailPath = species.details[detailId]
|
||||
if (!detailPath) continue
|
||||
items.push({
|
||||
label: `VUE DÉTAIL — ${detailId.toUpperCase()}`,
|
||||
label: `${dict['pdf.view.detail_prefix']}${detailId.toUpperCase()}`,
|
||||
src: detailPath,
|
||||
sourceId: detailId,
|
||||
})
|
||||
@@ -370,7 +375,7 @@ export async function addObservationPage(
|
||||
.join(' · ')
|
||||
|
||||
const sections: Array<{ label: string; value: string }> = [
|
||||
{ label: 'ZONES', value: zonesText || '—' },
|
||||
{ label: dict['pdf.section.zones'], value: zonesText || dict['pdf.empty'] },
|
||||
]
|
||||
// Group catalog findings by their JSON zoneId so each zone gets its
|
||||
// own labelled section. Preserves first-seen order so the on-screen
|
||||
@@ -386,29 +391,36 @@ export async function addObservationPage(
|
||||
}
|
||||
|
||||
for (const [zoneId, group] of findingsByZone) {
|
||||
const zoneName = findingsTree?.zones[zoneId]?.name_fr ?? zoneId
|
||||
const zoneTree = findingsTree?.zones[zoneId]
|
||||
const zoneName = zoneTree
|
||||
? locale === 'en'
|
||||
? zoneTree.name_en
|
||||
: zoneTree.name_fr
|
||||
: zoneId
|
||||
const value = group
|
||||
.map((f) =>
|
||||
f.requiresVetReferral ? `⚠ ${f.labelFrSnapshot}` : f.labelFrSnapshot,
|
||||
)
|
||||
.map((f) => {
|
||||
const lbl = locale === 'en' ? f.labelEnSnapshot : f.labelFrSnapshot
|
||||
return f.requiresVetReferral ? `⚠ ${lbl}` : lbl
|
||||
})
|
||||
.join(' · ')
|
||||
sections.push({
|
||||
label: `CONSTATS — ${zoneName.toUpperCase()}`,
|
||||
label: `${dict['pdf.section.findings_prefix']}${zoneName.toUpperCase()}`,
|
||||
value,
|
||||
})
|
||||
}
|
||||
|
||||
if (observation.freeTextFindings.length) {
|
||||
sections.push({
|
||||
label: 'CONSTATS LIBRES',
|
||||
label: dict['pdf.section.free_findings'],
|
||||
value: observation.freeTextFindings.join(' · '),
|
||||
})
|
||||
}
|
||||
if (observation.severity) {
|
||||
sections.push({ label: 'SÉVÉRITÉ', value: observation.severity })
|
||||
const sevLabel = dict[`severity.${observation.severity}`] ?? observation.severity
|
||||
sections.push({ label: dict['pdf.section.severity'], value: sevLabel })
|
||||
}
|
||||
if (observation.comment.trim()) {
|
||||
sections.push({ label: 'COMMENTAIRE', value: observation.comment.trim() })
|
||||
sections.push({ label: dict['pdf.section.comment'], value: observation.comment.trim() })
|
||||
}
|
||||
|
||||
const maxTextW = width - MARGIN_X * 2
|
||||
|
||||
Reference in new issue
Block a user