Files
Momento/memento-note/lib/i18n/LanguageProvider.tsx
Antigravity 80ccc1f6de
All checks were successful
CI / Lint, Unit Tests & Build (push) Successful in 7m14s
CI / Deploy production (on server) (push) Successful in 1m25s
feat: dashboard Second Brain, essai 7 jours et vérification e-mail
Rendre le dashboard actionnable (inbox, peek, carte mentale), aligner la facturation sur l’essai 7 jours, et bloquer le login e-mail tant que l’adresse n’est pas confirmée.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-30 07:19:36 +00:00

126 lines
4.4 KiB
TypeScript

'use client'
import { createContext, useContext, useEffect, useState, useCallback, useRef, useMemo } from 'react'
import type { ReactNode } from 'react'
import { SupportedLanguage, loadTranslations, getTranslationValue, Translations } from './load-translations'
// Static imports for SSR-safe initial translations (prevents hydration mismatch)
import enTranslations from '@/locales/en.json'
type LanguageContextType = {
language: SupportedLanguage
setLanguage: (lang: SupportedLanguage) => void
t: (key: string, params?: Record<string, string | number>) => string
translations: Translations
}
const LanguageContext = createContext<LanguageContextType | undefined>(undefined)
const RTL_LANGUAGES: SupportedLanguage[] = ['ar', 'fa']
const SUPPORTED_LANGS: SupportedLanguage[] = ['en', 'fr', 'es', 'de', 'fa', 'it', 'pt', 'ru', 'zh', 'ja', 'ko', 'ar', 'hi', 'nl', 'pl']
function updateDocumentDirection(lang: SupportedLanguage) {
document.documentElement.lang = lang
document.documentElement.dir = RTL_LANGUAGES.includes(lang) ? 'rtl' : 'ltr'
}
export function LanguageProvider({ children, initialLanguage = 'en', initialTranslations }: {
children: ReactNode
initialLanguage?: SupportedLanguage
initialTranslations?: Translations
}) {
// Use server-provided initialLanguage and translations for the first render
// to ensure client hydration matches server HTML exactly.
const [language, setLanguageState] = useState<SupportedLanguage>(initialLanguage)
// Start with server-provided translations or fallback to English ONLY if it's the requested language
const [translations, setTranslations] = useState<Translations>(() => {
if (initialTranslations) return initialTranslations
return enTranslations as unknown as Translations
})
const cacheRef = useRef<Map<SupportedLanguage, Translations>>(new Map())
// Initialize cache with initial translations to prevent redundant loading
if (initialTranslations && !cacheRef.current.has(initialLanguage)) {
cacheRef.current.set(initialLanguage, initialTranslations)
}
const isFirstRender = useRef(true)
// Load saved preference from cookie only (explicit picker). localStorage
// without cookie used to override note-based detection with a stale 'en'.
useEffect(() => {
const cookie = document.cookie
.split(';')
.map(s => s.trim())
.find(s => s.startsWith('user-language='))
?.split('=')[1] as SupportedLanguage | undefined
if (cookie && SUPPORTED_LANGS.includes(cookie) && cookie !== initialLanguage) {
setLanguageState(cookie)
}
}, [initialLanguage])
// Always reload locale JSON on the client so new i18n keys are picked up
// (SSR initialTranslations may be stale until the next deploy).
useEffect(() => {
let cancelled = false
const loadLang = async () => {
const loaded = await loadTranslations(language)
if (cancelled) return
cacheRef.current.set(language, loaded)
setTranslations(loaded)
if (!isFirstRender.current) updateDocumentDirection(language)
isFirstRender.current = false
}
loadLang()
return () => {
cancelled = true
}
}, [language])
const setLanguage = useCallback((lang: SupportedLanguage) => {
setLanguageState(lang)
localStorage.setItem('user-language', lang)
document.cookie = `user-language=${lang};path=/;max-age=${60 * 60 * 24 * 365};samesite=lax`
updateDocumentDirection(lang)
}, [])
const t = useCallback((key: string, params?: Record<string, string | number>) => {
if (!translations) return key
let value: any = getTranslationValue(translations, key)
if (value === key && language !== 'en') {
value = getTranslationValue(enTranslations as unknown as Translations, key)
}
if (params && typeof value === 'string') {
Object.entries(params).forEach(([param, paramValue]) => {
value = value.replace(`{${param}}`, String(paramValue))
})
}
return typeof value === 'string' ? value : key
}, [translations, language])
const value = useMemo(() => ({ language, setLanguage, t, translations }), [language, setLanguage, t, translations])
return (
<LanguageContext.Provider value={value}>
{children}
</LanguageContext.Provider>
)
}
export function useLanguage() {
const context = useContext(LanguageContext)
if (context === undefined) {
throw new Error('useLanguage must be used within a LanguageProvider')
}
return context
}