import { useState, useEffect } from 'react'
import { setLocale, getLocale, type Locale } from '@/lib/i18n'
const LOCALE_STORAGE_KEY = 'zapwall-locale'
interface LocaleButtonProps {
locale: Locale
label: string
currentLocale: Locale
onClick: (locale: Locale) => void
}
function LocaleButton({ locale, label, currentLocale, onClick }: LocaleButtonProps) {
const isActive = currentLocale === locale
return (
)
}
export function LanguageSelector() {
const [currentLocale, setCurrentLocale] = useState(getLocale())
useEffect(() => {
// Load saved locale from IndexedDB
const loadLocale = async () => {
try {
const { storageService } = await import('@/lib/storage/indexedDB')
const savedLocale = await storageService.get(LOCALE_STORAGE_KEY, 'app_storage')
if (savedLocale && (savedLocale === 'fr' || savedLocale === 'en')) {
setLocale(savedLocale)
setCurrentLocale(savedLocale)
}
} catch (e) {
console.error('Error loading locale:', e)
}
}
void loadLocale()
}, [])
const handleLocaleChange = async (locale: Locale) => {
setLocale(locale)
setCurrentLocale(locale)
try {
const { storageService } = await import('@/lib/storage/indexedDB')
await storageService.set(LOCALE_STORAGE_KEY, locale, 'app_storage')
} catch (e) {
console.error('Error saving locale:', e)
}
// Force page reload to update all translations
window.location.reload()
}
return (
)
}