/** * The lookup: a key in, a string out. Pure, with no platform access * (`Intl.PluralRules` is core ECMAScript, not DOM). */ import { interpolate, type Locale, type TranslateOptions, type TranslationDictionary, } from './contract'; import { embedpdfStringsFor } from './strings'; /** Walk a dotted path (`'commands.zoom.in'`) through a dictionary. */ function lookup( dictionary: TranslationDictionary | undefined, key: string, ): string | TranslationDictionary | undefined { if (!dictionary) return undefined; let node: string | TranslationDictionary | undefined = dictionary; for (const part of key.split('.')) { if (node === undefined || typeof node === 'string') return undefined; node = node[part]; } return node; } /** The language's plural form for a count; `'one'` or `'other'` when the runtime has no rules for it. */ function pluralCategory(locale: string, count: number): string { try { return new Intl.PluralRules(locale).select(count); } catch { return count === 1 ? 'one' : 'other'; } } /** A string is the answer; a branch needs a plural form, picked by `params.count`. */ function resolveNode( node: string | TranslationDictionary | undefined, locale: string, params?: Readonly>, ): string | undefined { if (typeof node === 'string') return node; if (node !== undefined && typeof params?.count === 'number') { const branch = node[pluralCategory(locale, params.count)] ?? node['other']; if (typeof branch === 'string') return branch; } return undefined; } /** What a lookup reads: the two languages it tries, and the app's strings. */ export interface TranslationSources { readonly locale: string; readonly fallbackLocale: string; /** The app's loaded languages, by code. */ readonly locales: Readonly>; } export interface TranslateResult { readonly text: string; /** False when no language has the key: `text` is then the fallback or the key. */ readonly found: boolean; } /** A key in one language: the app's string, then EmbedPDF's own. */ const stringIn = ( sources: TranslationSources, code: string, key: string, params?: Readonly>, ): string | undefined => resolveNode(lookup(sources.locales[code]?.translations, key), code, params) ?? resolveNode(lookup(embedpdfStringsFor(code), key), code, params); /** * A key's string: in the current language (the key, then `options.fallbackKey`), then the same in * the fallback language, then `options.fallback` (with its slots filled too), then the key itself. */ export function translate( sources: TranslationSources, key: string, options?: TranslateOptions, ): TranslateResult { const params = options?.params; const fallbackKey = options?.fallbackKey; const inLanguage = (code: string) => stringIn(sources, code, key, params) ?? (fallbackKey === undefined ? undefined : stringIn(sources, code, fallbackKey, params)); const text = inLanguage(sources.locale) ?? inLanguage(sources.fallbackLocale); if (text !== undefined) return { text: interpolate(text, options?.params), found: true }; if (options?.fallback !== undefined) return { text: interpolate(options.fallback, options.params), found: false }; return { text: key, found: false }; }