diff --git a/src/application/create-application.ts b/src/application/create-application.ts index 9bd931c2..fccb38da 100644 --- a/src/application/create-application.ts +++ b/src/application/create-application.ts @@ -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({ diff --git a/src/application/ports/in/application-api.ts b/src/application/ports/in/application-api.ts index 755de994..f4f7f2bb 100644 --- a/src/application/ports/in/application-api.ts +++ b/src/application/ports/in/application-api.ts @@ -54,6 +54,9 @@ export type ApplicationApi = Readonly<{ setColorScheme( preference: ColorSchemePreference, ): ReturnType; + /** The stored locale tag, unvalidated; presentation owns the locale list. */ + getLocale(): string | undefined; + setLocale(locale: string): ReturnType; }>; diagnostics: Readonly<{ reportRenderFailure(report: RenderFailureReport): void; diff --git a/src/contracts/storage-keys.ts b/src/contracts/storage-keys.ts index e2be748e..fb71082e 100644 --- a/src/contracts/storage-keys.ts +++ b/src/contracts/storage-keys.ts @@ -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", diff --git a/src/presentation/i18n/locale-provider.tsx b/src/presentation/i18n/locale-provider.tsx index 23d2540f..8e6f8862 100644 --- a/src/presentation/i18n/locale-provider.tsx +++ b/src/presentation/i18n/locale-provider.tsx @@ -74,9 +74,12 @@ const LocaleContext = createContext(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(() => 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 ( diff --git a/src/presentation/routes/app-router.tsx b/src/presentation/routes/app-router.tsx index f39fe05a..3c31dbb5 100644 --- a/src/presentation/routes/app-router.tsx +++ b/src/presentation/routes/app-router.tsx @@ -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 ( - + diff --git a/tests/component/locale-platform.test.tsx b/tests/component/locale-platform.test.tsx index 8f9a1637..843ad15c 100644 --- a/tests/component/locale-platform.test.tsx +++ b/tests/component/locale-platform.test.tsx @@ -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(); + 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( + + + , + ); + 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"); + }); +});