nomadro/src/components/locale-provider.tsx
Eric ffa456afdf Ship social join profile + SeaweedFS avatars, notify/coffee, and .com audience stack.
Pay-then-profile join desk with live card, S3 upload fallback, hydration-safe drafts; document on test9 README.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-04 02:25:05 +08:00

145 lines
3.7 KiB
TypeScript
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";
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);
function bootLocale(): Locale {
if (typeof window === "undefined") {
return isComHostname() ? "en" : "zh";
}
try {
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 = 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]);
/** 无 UI 开关;保留 API 供测试 / 脚本,不再写 unlock */
const setLocale = useCallback((next: Locale) => {
setLocaleState(next);
}, []);
const value = useMemo<LocaleContextValue>(() => {
const audience: AudienceLane =
audienceProfile?.audience ?? (locale === "zh" ? "cn" : "intl");
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 };
}