Files
faunosteo/src/state/localeStore.ts
T
Bastien ChanotandClaude Opus 4.7 9cf8a66dad feat(i18n): navigator-detect default locale + locale-aware bone labels
- localeStore.readInitial now consults navigator.language(s) when
  localStorage is empty (first fr/en hit, FR fallback). Persisted user
  choice still wins.
- New helper src/anatomy/labels.ts (boneLabel(b, locale, fallback))
  with cross-language fallback so missing labels never render blank.
- AnatomyCanvas rewrites native SVG <title> tooltips per locale on
  mount and on locale change (upstream SVG ships titles as "FR / EN"
  concat; we replace with the active-locale label only). Detail-popup
  trigger titles ("🔍 Zoom: …") left intact.
- DetailModal + EditorRoute detail title prefix + vertebra picker
  title now route through useT()/boneLabel.
- ObservationCard.describeZone takes locale, picks bone.fr / bone.en.
- PDF: buildZoneLabels returns Map<id, { fr, en }> instead of FR-only;
  observationPage picks per locale with cross-language fallback.
- Tests: FindingsTreePicker + VertebraPicker reset locale to 'fr' in
  beforeEach (navigator-detect may land on EN in happy-dom).
- pdf.zoneLabels test updated to assert the new pair shape.

154/154 tests pass, lint clean, typecheck clean, build OK.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-20 07:56:22 +02:00

51 lines
1.4 KiB
TypeScript

import { create } from 'zustand'
import type { Locale } from '@/i18n/types'
export const LOCALE_STORAGE_KEY = 'osteo:locale'
function detectNavigatorLocale(): Locale | undefined {
if (typeof navigator === 'undefined') return undefined
const langs: string[] = Array.isArray(navigator.languages)
? [...navigator.languages]
: []
if (typeof navigator.language === 'string') langs.push(navigator.language)
for (const tag of langs) {
const primary = tag.toLowerCase().split(/[-_]/)[0]
if (primary === 'fr') return 'fr'
if (primary === 'en') return 'en'
}
return undefined
}
/**
* Locale resolution priority:
* 1. Explicit localStorage value (user previously toggled)
* 2. Navigator language list (first fr/en hit on the user's prefs)
* 3. 'fr' fallback (single-praticien project default)
*/
function readInitial(): Locale {
if (typeof localStorage !== 'undefined') {
const raw = localStorage.getItem(LOCALE_STORAGE_KEY)
if (raw === 'fr' || raw === 'en') return raw
}
return detectNavigatorLocale() ?? 'fr'
}
export interface LocaleStore {
locale: Locale
setLocale: (next: Locale) => void
}
export const useLocaleStore = create<LocaleStore>((set) => ({
locale: readInitial(),
setLocale: (next) => {
set({ locale: next })
if (typeof localStorage === 'undefined') return
try {
localStorage.setItem(LOCALE_STORAGE_KEY, next)
} catch {
// Quota / private mode — in-memory only.
}
},
}))