Files
yuanzonghao 0a7076d5b9 fix(i18n): overhaul i18n with [locale] routing, SSR translations, and hreflang SEO
Rewrites the i18n system introduced in PR #94 to use Next.js App Router
[locale] dynamic segments with SSR-rendered translations and proper
middleware locale routing.

- Add middleware locale detection: / rewrites to /zh-CN/ internally,
  /en and /ja pass through, /zh-CN/... redirects to bare path
- Move all 7 pages under app/[locale]/ with SSR translation injection
- Fix server→client serialization: pre-evaluate function-valued
  translations (makeSerializable) to eliminate hydration flash
- Fix language switch key flash: use hard navigation with localStorage-
  only persistence, avoiding React state update before page reload
- Add <link rel="alternate" hreflang> tags for multilingual SEO
- Fix Supabase setAll overwriting locale rewrite response
- Trim locales from 22 to 3 (zh-CN/en/ja), delete 19 incomplete files
- LLM-translate 240 firstact game preset JSONs (en + ja, landscape +
  portrait) and story titles via gemini-3.5-flash
- Delete 11 one-off migration scripts and outdated i18n docs
- Add useLocalePath hook and navigation utilities

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-06-18 23:16:17 +08:00

61 lines
1.6 KiB
TypeScript

// Supported locales for InfiPlot
export const DEFAULT_LOCALE = "zh-CN" as const;
export type Locale = "zh-CN" | "en" | "ja";
export const LOCALE_NAMES: Record<Locale, string> = {
"zh-CN": "简体中文",
"en": "English",
"ja": "日本語",
};
export const LOCALES: Locale[] = Object.keys(LOCALE_NAMES) as Locale[];
// Locale storage key
export const LOCALE_STORAGE_KEY = "infiplot:locale";
// Get locale from localStorage or browser language
export function getInitialLocale(): Locale {
if (typeof window === "undefined") return DEFAULT_LOCALE;
try {
const stored = localStorage.getItem(LOCALE_STORAGE_KEY);
if (stored && LOCALES.includes(stored as Locale)) {
return stored as Locale;
}
} catch {
// ignore localStorage errors
}
// Try to match browser language
const browserLang = navigator.language;
const exactMatch = LOCALES.find((l) => l === browserLang);
if (exactMatch) return exactMatch;
// Try base language match (e.g., "zh" for "zh-TW")
const baseLang = browserLang.split("-")[0];
if (baseLang) {
const baseMatch = LOCALES.find((l) => l.startsWith(baseLang));
if (baseMatch) return baseMatch;
}
return DEFAULT_LOCALE;
}
// Save locale to localStorage
export function setLocale(locale: Locale): void {
if (typeof window === "undefined") return;
try {
localStorage.setItem(LOCALE_STORAGE_KEY, locale);
} catch {
// ignore localStorage errors
}
}
// Get RTL locales (right-to-left languages)
export const RTL_LOCALES: Set<Locale> = new Set();
export function isRTL(locale: Locale): boolean {
return RTL_LOCALES.has(locale);
}