fancive_obsidian-parallel-r.../src/i18n.ts
wujunchen 15b471efad refactor: extract i18n translation strings to dedicated i18n-strings.ts
Moves the STRINGS object (330 lines of zh/en translations) from i18n.ts
into a separate i18n-strings.ts file. i18n.ts now contains only the
resolveUiLanguage and translate logic (28 lines).

Change-Id: I9ddea79bddb4e8313dc16aae2a7c8d4eeff96fe1
2026-04-27 15:13:31 +08:00

28 lines
1 KiB
TypeScript

'use strict';
import { STRINGS } from './i18n-strings';
import type { PluginSettings } from './types';
export { STRINGS } from './i18n-strings';
export function resolveUiLanguage(settings: Pick<PluginSettings, 'uiLanguage'> | null): string {
const configured = settings?.uiLanguage;
if (configured === 'zh' || configured === 'en') return configured;
const nav = typeof navigator !== 'undefined' ? navigator : null;
const language = String(nav?.language || '').toLowerCase();
return language.startsWith('zh') ? 'zh' : 'en';
}
export function translate(
settings: Pick<PluginSettings, 'uiLanguage'> | null,
key: string,
vars?: Record<string, string | number>,
): string {
const lang = resolveUiLanguage(settings);
const table = STRINGS[lang] || STRINGS.en;
const fallback = STRINGS.en[key] || STRINGS.zh[key] || key;
const template = table[key] || fallback;
return String(template).replace(/\{([a-zA-Z0-9_]+)\}/g, (match, name) => {
return vars && Object.hasOwn(vars, name) ? String(vars[name]) : match;
});
}