File size: 1,280 Bytes
46252cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import type { PluginConfigSchema, PluginI18n, PluginI18nLocale } from '../services/api';

function localizeConfigSchema(
  schema: PluginConfigSchema | undefined,
  config: PluginI18nLocale['config'] | undefined,
): PluginConfigSchema | undefined {
  if (!schema?.properties || !config || typeof config !== 'object') return schema;
  const properties: PluginConfigSchema['properties'] = {};
  for (const [key, field] of Object.entries(schema.properties)) {
    const ov = config[key];
    properties[key] = ov
      ? { ...field, title: ov.title ?? field.title, description: ov.description ?? field.description }
      : field;
  }
  return { ...schema, properties };
}

/** Return a localized view of `plugin` for `lang`; identity when there is no matching override. */
export function localizePlugin<
  T extends { name: string; description?: string; configSchema?: PluginConfigSchema; i18n?: PluginI18n },
>(plugin: T, lang: string): T {
  const ov = plugin.i18n && typeof plugin.i18n === 'object' ? plugin.i18n[lang] : undefined;
  if (!ov || typeof ov !== 'object') return plugin;
  return {
    ...plugin,
    name: ov.name ?? plugin.name,
    description: ov.description ?? plugin.description,
    configSchema: localizeConfigSchema(plugin.configSchema, ov.config),
  };
}