mirror of
https://github.com/fancive/obsidian-parallel-reader.git
synced 2026-07-22 06:53:43 +00:00
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
28 lines
1 KiB
TypeScript
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;
|
|
});
|
|
}
|