0a7076d5b9
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>
65 lines
1.8 KiB
TypeScript
65 lines
1.8 KiB
TypeScript
import { LOCALES } from "./config";
|
|
import type { Locale } from "./config";
|
|
|
|
/**
|
|
* Get a nested value from an object using a dot-notation path
|
|
* @example getNestedValue({ a: { b: "c" } }, "a.b") // "c"
|
|
*/
|
|
export function getNestedValue<T>(obj: T, path: string): unknown {
|
|
return path.split(".").reduce<unknown>((current, key) => {
|
|
if (current && typeof current === "object" && key in current) {
|
|
return (current as Record<string, unknown>)[key];
|
|
}
|
|
return undefined;
|
|
}, obj);
|
|
}
|
|
|
|
/**
|
|
* Format a translation string with parameters
|
|
* Supports both {{key}} syntax and simple function-based interpolation
|
|
*/
|
|
export function formatTranslation(
|
|
template: string,
|
|
params: Record<string, string | number | boolean>,
|
|
): string {
|
|
if (Object.keys(params).length === 0) return template;
|
|
|
|
return template.replace(/\{{1,2}(\w+)\}{1,2}/g, (_match, key) => {
|
|
return params[key]?.toString() ?? `{${key}}`;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Deep merge two objects
|
|
*/
|
|
export function deepMerge<T extends Record<string, unknown>>(
|
|
target: T,
|
|
source: Partial<T>,
|
|
): T {
|
|
const result = { ...target };
|
|
|
|
for (const key in source) {
|
|
if (source[key] && typeof source[key] === "object" && !Array.isArray(source[key])) {
|
|
if (result[key] && typeof result[key] === "object" && !Array.isArray(result[key])) {
|
|
result[key] = deepMerge(
|
|
result[key] as Record<string, unknown>,
|
|
source[key] as Record<string, unknown>,
|
|
) as T[Extract<keyof T, string>];
|
|
} else {
|
|
result[key] = source[key] as T[Extract<keyof T, string>];
|
|
}
|
|
} else {
|
|
result[key] = source[key] as T[Extract<keyof T, string>];
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Validate locale string
|
|
*/
|
|
export function isValidLocale(locale: string): locale is Locale {
|
|
return (LOCALES as readonly string[]).includes(locale);
|
|
}
|