|
| 1 | +'use client'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Lightweight i18n for the web client (French + English). A flat key → string dictionary per |
| 5 | + * language, a `t(key, vars?)` lookup with `{var}` interpolation, and a persisted language |
| 6 | + * choice (localStorage, defaulting to the browser language). Server components that can't use |
| 7 | + * the hook keep their own copy; this covers the interactive app surfaces. |
| 8 | + */ |
| 9 | +import { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react'; |
| 10 | + |
| 11 | +export type Lang = 'fr' | 'en'; |
| 12 | + |
| 13 | +type Dict = Record<string, string>; |
| 14 | + |
| 15 | +const fr: Dict = { |
| 16 | + // common |
| 17 | + 'common.cancel': 'Annuler', |
| 18 | + 'common.confirm': 'Confirmer', |
| 19 | + 'common.save': 'Enregistrer', |
| 20 | + 'common.delete': 'Supprimer', |
| 21 | + 'common.download': 'Télécharger', |
| 22 | + 'common.open': 'Ouvrir', |
| 23 | + 'common.rename': 'Renommer', |
| 24 | + 'common.move': 'Déplacer', |
| 25 | + 'common.share': 'Partager', |
| 26 | + 'common.restore': 'Restaurer', |
| 27 | + 'common.loading': 'Chargement…', |
| 28 | + 'common.signin': 'Se connecter', |
| 29 | + 'common.signout': 'Se déconnecter', |
| 30 | + // nav |
| 31 | + 'nav.spaces': 'Mes Espaces', |
| 32 | + 'nav.shares': 'Partages', |
| 33 | + 'nav.remote': 'Remote', |
| 34 | + 'nav.trash': 'Corbeille', |
| 35 | + 'nav.admin': 'Administration', |
| 36 | + 'nav.settings': 'Paramètres', |
| 37 | + 'nav.storage': 'Stockage', |
| 38 | + 'role.admin': 'Administrateur', |
| 39 | + 'role.user': 'Utilisateur', |
| 40 | + // login |
| 41 | + 'login.subtitle': 'Connectez-vous à votre cloud privé', |
| 42 | + 'login.email': 'Email', |
| 43 | + 'login.password': 'Mot de passe', |
| 44 | + 'login.totp': 'Code à deux facteurs', |
| 45 | + 'login.totpHint': 'Saisissez le code à 6 chiffres de votre application, ou un code de secours.', |
| 46 | + 'login.totpPlaceholder': '123456 ou code de secours', |
| 47 | + 'login.signingIn': 'Connexion…', |
| 48 | + 'login.verify': 'Vérifier', |
| 49 | + 'login.failed': 'Connexion impossible', |
| 50 | + 'login.codeIncorrect': 'Code incorrect. Réessayez.', |
| 51 | + 'login.quickPrompt': 'Vous avez un code Quick-Upload ?', |
| 52 | + 'login.openSource': 'Open-source (AGPLv3) · par', |
| 53 | + 'legal.about': 'À propos', |
| 54 | + 'legal.terms': 'Conditions', |
| 55 | + 'legal.privacy': 'Confidentialité', |
| 56 | + 'legal.license': 'Licence', |
| 57 | +}; |
| 58 | + |
| 59 | +const en: Dict = { |
| 60 | + 'common.cancel': 'Cancel', |
| 61 | + 'common.confirm': 'Confirm', |
| 62 | + 'common.save': 'Save', |
| 63 | + 'common.delete': 'Delete', |
| 64 | + 'common.download': 'Download', |
| 65 | + 'common.open': 'Open', |
| 66 | + 'common.rename': 'Rename', |
| 67 | + 'common.move': 'Move', |
| 68 | + 'common.share': 'Share', |
| 69 | + 'common.restore': 'Restore', |
| 70 | + 'common.loading': 'Loading…', |
| 71 | + 'common.signin': 'Sign in', |
| 72 | + 'common.signout': 'Sign out', |
| 73 | + 'nav.spaces': 'My Spaces', |
| 74 | + 'nav.shares': 'Shares', |
| 75 | + 'nav.remote': 'Remote', |
| 76 | + 'nav.trash': 'Trash', |
| 77 | + 'nav.admin': 'Administration', |
| 78 | + 'nav.settings': 'Settings', |
| 79 | + 'nav.storage': 'Storage', |
| 80 | + 'role.admin': 'Administrator', |
| 81 | + 'role.user': 'User', |
| 82 | + 'login.subtitle': 'Sign in to your private cloud', |
| 83 | + 'login.email': 'Email', |
| 84 | + 'login.password': 'Password', |
| 85 | + 'login.totp': 'Two-factor code', |
| 86 | + 'login.totpHint': 'Enter the 6-digit code from your authenticator app, or a recovery code.', |
| 87 | + 'login.totpPlaceholder': '123456 or recovery code', |
| 88 | + 'login.signingIn': 'Signing in…', |
| 89 | + 'login.verify': 'Verify', |
| 90 | + 'login.failed': 'Sign-in failed', |
| 91 | + 'login.codeIncorrect': 'Incorrect code. Try again.', |
| 92 | + 'login.quickPrompt': 'Have a Quick-Upload code?', |
| 93 | + 'login.openSource': 'Open-source (AGPLv3) · by', |
| 94 | + 'legal.about': 'About', |
| 95 | + 'legal.terms': 'Terms', |
| 96 | + 'legal.privacy': 'Privacy', |
| 97 | + 'legal.license': 'License', |
| 98 | +}; |
| 99 | + |
| 100 | +const dictionaries: Record<Lang, Dict> = { fr, en }; |
| 101 | + |
| 102 | +interface I18nCtx { |
| 103 | + lang: Lang; |
| 104 | + setLang: (l: Lang) => void; |
| 105 | + t: (key: string, vars?: Record<string, string | number>) => string; |
| 106 | +} |
| 107 | + |
| 108 | +const I18nContext = createContext<I18nCtx | null>(null); |
| 109 | + |
| 110 | +export function I18nProvider({ children }: { children: React.ReactNode }) { |
| 111 | + // Default to French on first render (matches the server) to avoid a hydration mismatch, |
| 112 | + // then adopt the saved / browser preference on mount. |
| 113 | + const [lang, setLangState] = useState<Lang>('fr'); |
| 114 | + |
| 115 | + useEffect(() => { |
| 116 | + const saved = localStorage.getItem('ocl_lang'); |
| 117 | + if (saved === 'fr' || saved === 'en') setLangState(saved); |
| 118 | + else if (navigator.language?.toLowerCase().startsWith('en')) setLangState('en'); |
| 119 | + }, []); |
| 120 | + |
| 121 | + const setLang = useCallback((l: Lang) => { |
| 122 | + setLangState(l); |
| 123 | + try { |
| 124 | + localStorage.setItem('ocl_lang', l); |
| 125 | + } catch { |
| 126 | + /* ignore */ |
| 127 | + } |
| 128 | + document.documentElement.lang = l; |
| 129 | + }, []); |
| 130 | + |
| 131 | + const t = useCallback( |
| 132 | + (key: string, vars?: Record<string, string | number>) => { |
| 133 | + let s = dictionaries[lang][key] ?? dictionaries.fr[key] ?? key; |
| 134 | + if (vars) for (const k of Object.keys(vars)) s = s.replaceAll(`{${k}}`, String(vars[k])); |
| 135 | + return s; |
| 136 | + }, |
| 137 | + [lang], |
| 138 | + ); |
| 139 | + |
| 140 | + const value = useMemo(() => ({ lang, setLang, t }), [lang, setLang, t]); |
| 141 | + return <I18nContext.Provider value={value}>{children}</I18nContext.Provider>; |
| 142 | +} |
| 143 | + |
| 144 | +export function useT(): I18nCtx { |
| 145 | + const ctx = useContext(I18nContext); |
| 146 | + if (!ctx) throw new Error('useT must be used within I18nProvider'); |
| 147 | + return ctx; |
| 148 | +} |
0 commit comments