fix(presentation): persist the chosen locale in preferences (R-8.3)
The locale lived only in LocaleProvider state, so every remount (reload, or the scope fence after a session change) started again from ko-KR. The theme already persisted through preferences.setColorScheme; the locale now uses the same path (D-5): - contracts/storage-keys.ts: LOCALE key next to COLOR_SCHEME (localStorage, public-preference, opaque-string-v1). - application preferences port: additive getLocale()/setLocale(). The application treats the tag as opaque; presentation owns the locale list and normalizes what it reads back. - LocaleProvider: optional onLocaleChange; the provider stays storage-free so standalone uses and tests are unchanged. - AppRouter mounts LocaleProvider from the stored locale and persists changes (T46 moves this above the scope fence). Verified: RED "stores the chosen locale and starts the next mount from it" failed before (nothing written to LOCALE) and passes after. check:architecture, check:types:app, check:types:test, lint, test:component pass; check:registries PASS (compatibility unchanged from baseline); test:unit passes except the pre-existing removal-fixture test (needs generated artifacts/ files absent from a fresh worktree). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
b1a8269d65
commit
e5ef6ba236
@@ -41,6 +41,15 @@ export function createApplication(
|
||||
const normalized = normalizeColorSchemePreference(preference);
|
||||
return outputPorts.preferences.write("COLOR_SCHEME", normalized);
|
||||
},
|
||||
getLocale(): string | undefined {
|
||||
const result = outputPorts.preferences.read("LOCALE");
|
||||
return result.ok && typeof result.value === "string"
|
||||
? result.value
|
||||
: undefined;
|
||||
},
|
||||
setLocale(locale: string) {
|
||||
return outputPorts.preferences.write("LOCALE", locale);
|
||||
},
|
||||
});
|
||||
|
||||
const diagnostics = Object.freeze({
|
||||
|
||||
@@ -54,6 +54,9 @@ export type ApplicationApi = Readonly<{
|
||||
setColorScheme(
|
||||
preference: ColorSchemePreference,
|
||||
): ReturnType<StoragePort["write"]>;
|
||||
/** The stored locale tag, unvalidated; presentation owns the locale list. */
|
||||
getLocale(): string | undefined;
|
||||
setLocale(locale: string): ReturnType<StoragePort["write"]>;
|
||||
}>;
|
||||
diagnostics: Readonly<{
|
||||
reportRenderFailure(report: RenderFailureReport): void;
|
||||
|
||||
@@ -45,6 +45,19 @@ export const STORAGE_REGISTRY = Object.freeze({
|
||||
migration: "discard",
|
||||
quotaFallback: "memory",
|
||||
}),
|
||||
/** The chosen UI locale. Presentation normalizes whatever it reads back. */
|
||||
LOCALE: defineStorageKey({
|
||||
logicalName: "LOCALE",
|
||||
scope: "preference",
|
||||
name: "locale",
|
||||
backend: "localStorage",
|
||||
classification: "public-preference",
|
||||
schemaVersion: 1,
|
||||
valueCodec: "opaque-string-v1",
|
||||
ttl: null,
|
||||
migration: "discard",
|
||||
quotaFallback: "memory",
|
||||
}),
|
||||
CHUNK_RELOAD_GUARD: defineStorageKey({
|
||||
logicalName: "CHUNK_RELOAD_GUARD",
|
||||
scope: "release",
|
||||
|
||||
@@ -74,9 +74,12 @@ const LocaleContext = createContext<LocaleContextValue>(DEFAULT_CONTEXT);
|
||||
export function LocaleProvider({
|
||||
children,
|
||||
initialLocale = "ko-KR",
|
||||
onLocaleChange,
|
||||
}: Readonly<{
|
||||
children: React.ReactNode;
|
||||
initialLocale?: SupportedLocale;
|
||||
/** Persists a user's choice; the provider itself stays storage-free. */
|
||||
onLocaleChange?: (locale: SupportedLocale) => void;
|
||||
}>) {
|
||||
const [locale, setLocaleState] = useState<SupportedLocale>(() =>
|
||||
normalizeLocale(initialLocale),
|
||||
@@ -92,7 +95,10 @@ export function LocaleProvider({
|
||||
() => ({
|
||||
locale,
|
||||
direction,
|
||||
setLocale: setLocaleState,
|
||||
setLocale(next) {
|
||||
setLocaleState(next);
|
||||
onLocaleChange?.(next);
|
||||
},
|
||||
message: (key, ...args) => formatMessage(locale, key, ...args),
|
||||
resolve: (key) => resolveMessage(locale, key),
|
||||
date: (dateValue, options) => formatDate(locale, dateValue, options),
|
||||
@@ -105,7 +111,7 @@ export function LocaleProvider({
|
||||
selectPlural(locale, pluralValue, choices),
|
||||
select: (selectValue, choices) => selectMessage(selectValue, choices),
|
||||
}),
|
||||
[direction, locale],
|
||||
[direction, locale, onLocaleChange],
|
||||
);
|
||||
|
||||
return (
|
||||
|
||||
@@ -39,10 +39,11 @@ import { ChunkRecoveryBoundary } from "../boundaries/chunk-recovery-boundary.tsx
|
||||
import { ContextErrorBoundary } from "../../platform/errors/context-error-boundary.tsx";
|
||||
import { FeatureErrorBoundary } from "../../platform/errors/feature-error-boundary.tsx";
|
||||
import { Button, PageHeader } from "../design-system/index.ts";
|
||||
import { LocaleProvider, useLocale } from "../i18n/index.ts";
|
||||
import { LocaleProvider, normalizeLocale, useLocale } from "../i18n/index.ts";
|
||||
import { AppShell } from "../layouts/app-shell.tsx";
|
||||
import {
|
||||
useApplicationDiagnostics,
|
||||
useApplicationPreferences,
|
||||
useApplicationRecovery,
|
||||
useApplicationRuntime,
|
||||
} from "../providers/application-provider.tsx";
|
||||
@@ -501,6 +502,7 @@ export function AppRouter({
|
||||
basename = "/",
|
||||
buildId = "local-build",
|
||||
}: Readonly<{ basename?: string; buildId?: string }>) {
|
||||
const preferences = useApplicationPreferences();
|
||||
const router = useMemo(
|
||||
() =>
|
||||
createBrowserRouter(createRegisteredRoutes(buildId), {
|
||||
@@ -509,7 +511,10 @@ export function AppRouter({
|
||||
[basename, buildId],
|
||||
);
|
||||
return (
|
||||
<LocaleProvider>
|
||||
<LocaleProvider
|
||||
initialLocale={normalizeLocale(preferences.getLocale() ?? "ko-KR")}
|
||||
onLocaleChange={preferences.setLocale}
|
||||
>
|
||||
<ThemeProvider>
|
||||
<SessionProvider>
|
||||
<RouterProvider router={router} />
|
||||
|
||||
@@ -16,6 +16,9 @@ import {
|
||||
useLocale,
|
||||
type SupportedLocale,
|
||||
} from "../../src/presentation/i18n/index.ts";
|
||||
import { ApplicationProvider } from "../../src/presentation/providers/application-provider.tsx";
|
||||
import { AppRouter } from "../../src/presentation/routes/app-router.tsx";
|
||||
import { createTestApplication } from "../helpers/create-test-application.ts";
|
||||
|
||||
function LocaleHarness() {
|
||||
const { direction, locale, message, setLocale } = useLocale();
|
||||
@@ -118,3 +121,39 @@ describe("locale provider runtime", () => {
|
||||
expect(screen.queryByRole("img")).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
describe("persisted locale preference", () => {
|
||||
it("stores the chosen locale and starts the next mount from it", async () => {
|
||||
const user = userEvent.setup();
|
||||
const stored = new Map<string, unknown>();
|
||||
const application = createTestApplication({
|
||||
preferences: {
|
||||
read: (name) => ({ ok: true as const, value: stored.get(name) }),
|
||||
write: (name, value) => {
|
||||
stored.set(name, value);
|
||||
return { ok: true as const };
|
||||
},
|
||||
remove: (name) => {
|
||||
stored.delete(name);
|
||||
return { ok: true as const };
|
||||
},
|
||||
},
|
||||
});
|
||||
const mount = () =>
|
||||
render(
|
||||
<ApplicationProvider application={application}>
|
||||
<AppRouter />
|
||||
</ApplicationProvider>,
|
||||
);
|
||||
window.history.pushState({}, "", "/missing");
|
||||
|
||||
const first = mount();
|
||||
await user.selectOptions(await screen.findByLabelText("언어"), "en-US");
|
||||
expect(stored.get("LOCALE")).toBe("en-US");
|
||||
first.unmount();
|
||||
|
||||
mount();
|
||||
expect(await screen.findByLabelText("Language")).toHaveValue("en-US");
|
||||
expect(document.documentElement).toHaveAttribute("lang", "en-US");
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user