Freeze experimental social/admin UX on test12 for reference only; see docs/archive/test12-NEVER-DEPLOY.md. Co-authored-by: Cursor <cursoragent@cursor.com>
156 lines
4.2 KiB
TypeScript
156 lines
4.2 KiB
TypeScript
"use client";
|
||
|
||
import {
|
||
createContext,
|
||
useCallback,
|
||
useContext,
|
||
useEffect,
|
||
useMemo,
|
||
useState,
|
||
type ReactNode,
|
||
} from "react";
|
||
import {
|
||
detectAudience,
|
||
isComHostname,
|
||
LANG_UNLOCK_KEY,
|
||
LOCALE_STORAGE_KEY,
|
||
type AudienceLane,
|
||
type AudienceProfile,
|
||
} from "~/modules/audience";
|
||
import { resolveUiLocale } from "~/lib/detect-ui-locale";
|
||
import { dictionaries, type Dictionary, type Locale } from "~/lib/i18n";
|
||
|
||
type LocaleContextValue = {
|
||
locale: Locale;
|
||
/** 是否按中文用户特殊对待(支付 / 关注 / 运营位),≠ UI 语言 */
|
||
isChina: boolean;
|
||
audience: AudienceLane;
|
||
audienceProfile: AudienceProfile | null;
|
||
ready: boolean;
|
||
t: Dictionary;
|
||
setLocale: (locale: Locale) => void;
|
||
};
|
||
|
||
const LocaleContext = createContext<LocaleContextValue | null>(null);
|
||
// UI preference is independent from audience/payment detection.
|
||
const UI_LOCALE_KEY = "nomadro-ui-locale";
|
||
function preferredLocale(): Locale | null {
|
||
try {
|
||
const value = window.localStorage.getItem(UI_LOCALE_KEY);
|
||
return value && value in dictionaries ? value as Locale : null;
|
||
} catch { return null; }
|
||
}
|
||
|
||
function bootLocale(): Locale {
|
||
if (typeof window === "undefined") {
|
||
return isComHostname() ? "en" : "zh";
|
||
}
|
||
try {
|
||
const preferred = preferredLocale();
|
||
if (preferred) return preferred;
|
||
const profile = detectAudience();
|
||
return resolveUiLocale({
|
||
isChinaUser: profile.isChina,
|
||
localeHint: profile.localeHint,
|
||
});
|
||
} catch {
|
||
return isComHostname() ? "en" : "zh";
|
||
}
|
||
}
|
||
|
||
export function LocaleProvider({ children }: { children: ReactNode }) {
|
||
const [locale, setLocaleState] = useState<Locale>(bootLocale);
|
||
const [audienceProfile, setAudienceProfile] = useState<AudienceProfile | null>(
|
||
null,
|
||
);
|
||
const [ready, setReady] = useState(false);
|
||
|
||
useEffect(() => {
|
||
let cancelled = false;
|
||
try {
|
||
// 隐藏语言开关:清掉历史「显式解锁」;支付跟受众,UI 跟浏览器+中文用户兜底
|
||
try {
|
||
window.sessionStorage.removeItem(LANG_UNLOCK_KEY);
|
||
window.localStorage.removeItem(LOCALE_STORAGE_KEY);
|
||
window.localStorage.removeItem("nomadro-audience-override");
|
||
} catch {
|
||
/* ignore */
|
||
}
|
||
|
||
const profile = detectAudience();
|
||
const ui = preferredLocale() || resolveUiLocale({
|
||
isChinaUser: profile.isChina,
|
||
localeHint: profile.localeHint,
|
||
});
|
||
if (!cancelled) {
|
||
setAudienceProfile(profile);
|
||
setLocaleState(ui);
|
||
setReady(true);
|
||
}
|
||
} catch {
|
||
if (!cancelled) {
|
||
setLocaleState(bootLocale());
|
||
setAudienceProfile(null);
|
||
setReady(true);
|
||
}
|
||
}
|
||
return () => {
|
||
cancelled = true;
|
||
};
|
||
}, []);
|
||
|
||
useEffect(() => {
|
||
if (!ready) return;
|
||
document.documentElement.lang = dictionaries[locale].htmlLang;
|
||
try {
|
||
if (audienceProfile) {
|
||
document.documentElement.dataset.audience = audienceProfile.audience;
|
||
document.documentElement.dataset.china = audienceProfile.isChina
|
||
? "1"
|
||
: "0";
|
||
}
|
||
} catch {
|
||
// ignore
|
||
}
|
||
}, [locale, ready, audienceProfile]);
|
||
|
||
/** Changing display language never changes the audience lane. */
|
||
const setLocale = useCallback((next: Locale) => {
|
||
setLocaleState(next);
|
||
try { window.localStorage.setItem(UI_LOCALE_KEY, next); } catch { /* session preference still works */ }
|
||
}, []);
|
||
|
||
const value = useMemo<LocaleContextValue>(() => {
|
||
const audience: AudienceLane =
|
||
audienceProfile?.audience ?? (isComHostname() ? "intl" : "cn");
|
||
const isChina = audience === "cn";
|
||
return {
|
||
locale,
|
||
isChina,
|
||
audience,
|
||
audienceProfile,
|
||
ready,
|
||
t: dictionaries[locale],
|
||
setLocale,
|
||
};
|
||
}, [locale, ready, setLocale, audienceProfile]);
|
||
|
||
return (
|
||
<LocaleContext.Provider value={value}>{children}</LocaleContext.Provider>
|
||
);
|
||
}
|
||
|
||
export function useLocale() {
|
||
const ctx = useContext(LocaleContext);
|
||
if (!ctx) {
|
||
throw new Error("useLocale must be used within LocaleProvider");
|
||
}
|
||
return ctx;
|
||
}
|
||
|
||
/** 功能分流用:中文特殊线 vs 国际线 */
|
||
export function useAudience() {
|
||
const { audience, isChina, audienceProfile, ready } = useLocale();
|
||
return { audience, isChina, audienceProfile, ready };
|
||
}
|