Files
forumlify/lib/i18n.js
T
lezi-fun 0562dd735a feat: i18n (i18next + react-i18next) zh/en + hydration fixes
- i18next + react-i18next: zh/en locales, language detection
  (localStorage > navigator > zh), settings language switcher
- Components i18n'd: Navbar, Feed, NewPost, Modals, SettingsPage,
  SettingsProfile, AccountSecurity, RecoveryCodes
- Fix hydration mismatch (spinner restart): forum name via cookie
  (Server Component reads cookie, SSR outputs name, no inline script
  DOM injection); remove createPortal in RecoveryCodes modal; i18n
  sync init (initImmediate) so SSR/CSR text matches
- LoadingScreen: React-state name rendering (no script-injected nodes)
- Fix double spinner in Feed; DM icon emoji → SVG in ConversationList
2026-08-11 11:32:15 +08:00

42 lines
1.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use client';
// i18n 配置:i18next + react-i18next
// - 模块顶层无条件 init(SSR Node 与客户端都执行):SSR 输出用默认 zh,
// 与客户端初始语言一致 → 无 hydration mismatch
// - initI18n() 在客户端按 localStorage/浏览器语言切换;SSR 时 no-op
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import zh from '@/locales/zh.json';
import en from '@/locales/en.json';
i18n.use(initReactI18next).init({
resources: {
zh: { translation: zh },
en: { translation: en },
},
lng: 'zh',
fallbackLng: 'zh',
interpolation: { escapeValue: false },
react: { useSuspense: false },
// 同步初始化:SSR 渲染时 t() 立即返回翻译(否则返回 key → hydration mismatch)
initImmediate: false,
});
// 客户端初始化:按 localStorage / 浏览器语言切换(SSR 阶段 no-op)
export function initI18n() {
if (typeof window === 'undefined') return i18n;
try {
const saved = localStorage.getItem('forumlify-lang');
const nav = (navigator.language || 'zh').toLowerCase();
const lng = saved || (nav.startsWith('en') ? 'en' : 'zh');
if (i18n.language !== lng) i18n.changeLanguage(lng);
} catch (e) {}
return i18n;
}
// 切换语言(保存到 localStorage)
export function setAppLanguage(lng) {
if (typeof window !== 'undefined') localStorage.setItem('forumlify-lang', lng);
return i18n.changeLanguage(lng);
}