feat(i18n): add localeStore with localStorage persistence

This commit is contained in:
Bastien Chanot committed 2026-05-20 06:55:43 +02:00
1 parent 0a05c479a0
commit a57047505d
3 files changed
+53

No files matched your search

+28
View File
@@ -0,0 +1,28 @@
import { create } from 'zustand'
import type { Locale } from '@/i18n/types'
export const LOCALE_STORAGE_KEY = 'osteo:locale'
function readInitial(): Locale {
if (typeof localStorage === 'undefined') return 'fr'
const raw = localStorage.getItem(LOCALE_STORAGE_KEY)
return raw === 'fr' || raw === 'en' ? raw : '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.
}
},
}))