Files
forumlify/components/I18nInit.js
lezi-fun dce94fd5af feat: auto-detect browser language (SSR Accept-Language + cookie)
- layout (server): derive language from cookie (user choice) > browser
  Accept-Language; set <html lang> accordingly
- I18nInit → I18nProvider: sync initI18n(lng) before children render
  (SSR/CSR first render same language, no hydration mismatch)
- setAppLanguage also writes cookie so SSR first paint uses chosen lang
- Verified: en browser → lang=en, zh → zh-CN, cookie overrides
2026-08-11 17:42:08 +08:00

13 lines
517 B
JavaScript
Raw Permalink 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 客户端 Provider:在 children 渲染前同步初始化 i18n
// - lng 由 Server Component(layout)从 cookie / Accept-Language 推导传入
// - 同步 init(initImmediate:false)→ SSR 与客户端首次渲染语言一致,无 hydration mismatch
import { initI18n } from '@/lib/i18n';
export default function I18nProvider({ lng, children }) {
// 函数体在 children 渲染前执行:useTranslation 组件拿到已初始化的 i18n
initI18n(lng);
return <>{children}</>;
}