diff --git a/src/anatomy/labels.ts b/src/anatomy/labels.ts index 326a3aca..fe2f0fed 100644 --- a/src/anatomy/labels.ts +++ b/src/anatomy/labels.ts @@ -1,4 +1,5 @@ import type { Locale } from '@/i18n/types' +import { pickLocaleLabel } from '@/i18n/pickLocaleLabel' import type { Bone } from './types' /** @@ -14,6 +15,5 @@ export function boneLabel( fallback: string, ): string { if (!bone) return fallback - if (locale === 'en') return bone.en || bone.fr || fallback - return bone.fr || bone.en || fallback + return pickLocaleLabel(bone.fr, bone.en, locale, fallback) } diff --git a/src/components/canvas/AnatomyCanvas.tsx b/src/components/canvas/AnatomyCanvas.tsx index f627710c..17a410f4 100644 --- a/src/components/canvas/AnatomyCanvas.tsx +++ b/src/components/canvas/AnatomyCanvas.tsx @@ -6,6 +6,7 @@ import { } from '@/anatomy/svgConvention' import type { BoneState } from '@/anatomy/types' import { useLocaleStore } from '@/state/localeStore' +import { pickLocaleLabel } from '@/i18n/pickLocaleLabel' export interface AnatomyCanvasProps { src: string @@ -69,7 +70,7 @@ export default function AnatomyCanvas({ if (!tip) return const fr = el.getAttribute('data-anno-name-fr') ?? '' const en = el.getAttribute('data-anno-name-en') ?? '' - const label = locale === 'en' ? en || fr : fr || en + const label = pickLocaleLabel(fr, en, locale) if (!label) { tip.style.opacity = '0'; return } tip.textContent = label tip.style.opacity = '1' diff --git a/src/i18n/pickLocaleLabel.ts b/src/i18n/pickLocaleLabel.ts new file mode 100644 index 00000000..38779dae --- /dev/null +++ b/src/i18n/pickLocaleLabel.ts @@ -0,0 +1,16 @@ +import type { Locale } from './types' + +/** + * Resolves a bilingual label for the active locale. Falls back to the + * opposite language when the preferred one is missing/empty, then to the + * provided fallback, so callers never render an empty string. + */ +export function pickLocaleLabel( + fr: string | undefined, + en: string | undefined, + locale: Locale, + fallback = '', +): string { + if (locale === 'en') return en || fr || fallback + return fr || en || fallback +} diff --git a/src/pdf/render/observationPage.ts b/src/pdf/render/observationPage.ts index 76ce4032..19fa7d9e 100644 --- a/src/pdf/render/observationPage.ts +++ b/src/pdf/render/observationPage.ts @@ -5,6 +5,7 @@ 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 { pickLocaleLabel } from '@/i18n/pickLocaleLabel' import type { ZoneLabelPair } from '@/pdf/export' import { drawHeader, type HeaderAssets } from './header' import { rasterizeSvg } from './svgToPng' @@ -353,9 +354,7 @@ export async function addObservationPage( const zonesText = observation.zones .map((z) => { const pair = zoneLabels.get(z.boneId) - const label = pair - ? (locale === 'en' ? pair.en || pair.fr : pair.fr || pair.en) || z.boneId - : z.boneId + const label = pickLocaleLabel(pair?.fr, pair?.en, locale, z.boneId) const v = z.vertebrae?.length ? ` (${z.vertebrae.map((x) => `${x.region}${x.index}`).join(', ')})` : ''