29 lines
743 B
TypeScript
29 lines
743 B
TypeScript
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.
|
|
}
|
|
},
|
|
}))
|