feat(i18n): add localeStore with localStorage persistence
This commit is contained in:
1 parent
0a05c479a0
commit
a57047505d
3 files changed
+53
No files matched your search
@@ -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.
|
||||
}
|
||||
},
|
||||
}))
|
||||
Reference in new issue
Block a user