Files
nanobot/webui/src/channel-plugins/locale-registry.ts
T
chengyongruandGitHub 462a0dfb0f refactor(channels): make built-in channels self-contained (#4908)
* refactor(channels): own setup and instance contracts

* refactor(channels): isolate management contracts

* refactor(channels): normalize activation contracts

* fix(channels): enforce management contracts

* refactor(channels): finish setup ownership migration

* fix(channels): harden management contracts

* fix(channels): enforce lazy loading and runtime ownership

* fix(feishu): make multi-instance startup idempotent

* fix(webui): render channel setup contracts cleanly

* fix(feishu): stop websocket clients cleanly

* fix(channels): enforce persistence and activation gates

* fix(channels): preserve global feature action scope

* fix(channels): apply defaults for single plugins

* fix(channels): enforce management contract boundaries

* refactor(feishu): remove identity helper indirection

* fix(channels): preserve management setup contracts

* refactor(channels): generalize instance settings UI

* refactor(channels): package channel plugins with web UI metadata

* refactor(channels): make built-ins self-contained packages

* test(channels): colocate tests with channel packages

* fix(dingtalk): use official brand icon

* feat(channels): colocate webui translations

* docs(channels): clarify plugin ownership

* test(exec): remove output wait race

* refactor(channels): unify plugin descriptors

* fix(channels): enforce descriptor-owned contracts

* refactor(channels): finish package-owned plugin setup

* refactor(channels): use repository-owned packages only

* fix(channels): self-describe dependencies and runtime state

* fix(channels): warn about legacy entry points
2026-07-19 23:30:49 +08:00

66 lines
2.1 KiB
TypeScript

import type { ChannelMessages } from "@/channel-plugins/i18n";
import { channelNamespace } from "@/channel-plugins/i18n";
import {
supportedLocales,
type SupportedLocale,
} from "@/i18n/config";
type ChannelMessagesModule = {
default?: ChannelMessages;
};
const modules = import.meta.glob<ChannelMessagesModule>(
"../../../nanobot/channels/*/webui/locales/*.json",
{ eager: true },
);
const translationsByChannel = new Map<string, Map<SupportedLocale, ChannelMessages>>();
const supportedLocaleCodes = new Set<string>(supportedLocales.map(({ code }) => code));
for (const [modulePath, module] of Object.entries(modules)) {
const messages = module.default;
if (!messages) continue;
const match = modulePath.match(/nanobot\/channels\/([^/]+)\/webui\/locales\/([^/]+)\.json$/);
if (!match) {
throw new Error(`Cannot derive channel locale identity from '${modulePath}'`);
}
const [, channel, locale] = match;
if (!supportedLocaleCodes.has(locale)) {
throw new Error(`Channel '${channel}' has unsupported locale '${locale}'`);
}
const translations = translationsByChannel.get(channel) ?? new Map();
if (translations.has(locale as SupportedLocale)) {
throw new Error(`Channel '${channel}' registers locale '${locale}' more than once`);
}
translations.set(locale as SupportedLocale, messages);
translationsByChannel.set(channel, translations);
}
export function channelLocaleNamespaces(): string[] {
return [...translationsByChannel.keys()].map(channelNamespace);
}
export function channelLocaleResources(locale: SupportedLocale): Record<string, unknown> {
return Object.fromEntries(
[...translationsByChannel.keys()].map((channel) => [
channelNamespace(channel),
channelLocaleMessages(channel, locale) ?? {},
]),
);
}
export function channelLocaleMessages(
channel: string,
locale: SupportedLocale,
): ChannelMessages | undefined {
const translations = translationsByChannel.get(channel);
return translations?.get(locale) ?? translations?.get("en");
}
export function registeredChannelLocales(): ReadonlyMap<
string,
ReadonlyMap<SupportedLocale, ChannelMessages>
> {
return translationsByChannel;
}