diff --git a/webui/src/channel-plugins/locale-registry.ts b/webui/src/channel-plugins/locale-registry.ts index da1941fa4..c30d17313 100644 --- a/webui/src/channel-plugins/locale-registry.ts +++ b/webui/src/channel-plugins/locale-registry.ts @@ -73,7 +73,13 @@ async function loadChannelLocale( channel: string, locale: SupportedLocale, ): Promise { - const translations = translationsByChannel.get(channel) ?? new Map(); + // Get-or-create the shared per-channel map synchronously so concurrent locale + // loads append to one Map instead of replacing each other's registration. + let translations = translationsByChannel.get(channel); + if (!translations) { + translations = new Map(); + translationsByChannel.set(channel, translations); + } const loaded = translations.get(locale); if (loaded) return loaded; @@ -87,6 +93,5 @@ async function loadChannelLocale( throw new Error(`Channel '${channel}' locale '${locale}' has no default export`); } translations.set(locale, messages); - translationsByChannel.set(channel, translations); return messages; } diff --git a/webui/src/tests/channel-locale-registry.test.ts b/webui/src/tests/channel-locale-registry.test.ts index 8249d5f68..21073be17 100644 --- a/webui/src/tests/channel-locale-registry.test.ts +++ b/webui/src/tests/channel-locale-registry.test.ts @@ -1,7 +1,7 @@ import { readFileSync } from "node:fs"; import { resolve } from "node:path"; -import { describe, expect, it } from "vitest"; +import { describe, expect, it, vi } from "vitest"; import { channelFieldMessageKey } from "@/channel-plugins/i18n"; import { registeredChannelLocales } from "@/channel-plugins/locale-registry"; @@ -113,3 +113,26 @@ describe("channel locale registry", () => { expect(i18nEntry).not.toContain("channel-plugins/registry"); }); }); + +describe("channel locale registry concurrency", () => { + it("registers every locale loaded concurrently for the same channel", async () => { + // A fresh module instance starts with an empty registry, which is the state + // startup reaches when the stored locale differs from the fallback locale. + vi.resetModules(); + const registry = await import("@/channel-plugins/locale-registry"); + + await Promise.all([ + registry.channelLocaleResources("en"), + registry.channelLocaleResources("zh-CN"), + ]); + + const registrations = registry.registeredChannelLocales(); + expect([...registrations.keys()].sort()).toEqual(expectedChannels); + for (const [channel, locales] of registrations) { + expect([...locales.keys()].sort(), `${channel} concurrent locales`).toEqual([ + "en", + "zh-CN", + ]); + } + }); +});