# @embedpdf/plugin-i18n — ./internal I18nAction [TypeAliasDeclaration] (from packages/plugin/i18n/src/model.ts): export type I18nAction = |{ type: 'I18N/SET_LOCALE'; locale: string} |{ type: 'I18N/REGISTER_LOCALE'; locale: Locale} |{ type: 'I18N/UNREGISTER_LOCALE'; locale: string} |{ type: 'I18N/ADD_TRANSLATIONS'; locale: string; translations: TranslationDictionary} |{ type: 'I18N/LOAD_STARTED'; locale: string} |{ type: 'I18N/LOAD_FAILED'; locale: string} ; I18nCapability [InterfaceDeclaration] (from packages/plugin/i18n/src/contract.ts): export interface I18nCapability{ t(key: string, options?: TranslateOptions): string; hasKey(key: string): boolean; getLocale(): string; getDirection(): 'ltr' | 'rtl'; listLocales(): readonly LocaleInfo[]; getLoadingLocale(): string | null; setLocale(code: string, options?: OperationOptions): Promise; registerLocale(locale: Locale): Unsubscribe; addTranslations(code: string, dictionary: TranslationDictionary): void; readonly onLocaleChanged: EventHook; readonly onLocaleLoadFailed: EventHook; } I18nConfig [InterfaceDeclaration] (from packages/plugin/i18n/src/contract.ts): export interface I18nConfig{ locale?: string; fallbackLocale?: string; locales?: Locale[]; loaders?: Record Promise>; } I18nHostCapability [TypeAliasDeclaration] (from packages/plugin/i18n/src/host-contract.ts): export type I18nHostCapability = I18nCapability; I18nState [InterfaceDeclaration] (from packages/plugin/i18n/src/model.ts): export interface I18nState{ readonly locale: string; readonly fallbackLocale: string; readonly locales: Readonly>; readonly loading: string | null; } I18nToken [VariableDeclaration] (from packages/plugin/i18n/src/token.ts): I18nToken = createCapabilityToken('i18n', { hint: `add i18nPlugin() from '@embedpdf/plugin-i18n' to your plugins list`, } ) Locale [InterfaceDeclaration] (from packages/plugin/i18n/src/contract.ts): export interface Locale{ readonly code: string; readonly name: string; readonly dir?: 'ltr' | 'rtl'; readonly translations: TranslationDictionary; } LocaleChangedEvent [InterfaceDeclaration] (from packages/plugin/i18n/src/contract.ts): export interface LocaleChangedEvent{ readonly locale: string; readonly previousLocale: string; } LocaleInfo [InterfaceDeclaration] (from packages/plugin/i18n/src/contract.ts): export interface LocaleInfo{ readonly code: string; readonly name: string; readonly dir: 'ltr' | 'rtl'; readonly loaded: boolean; } LocaleLoadFailedEvent [InterfaceDeclaration] (from packages/plugin/i18n/src/contract.ts): export interface LocaleLoadFailedEvent{ readonly locale: string; readonly error: PluginErrorInfo; } TranslateOptions [InterfaceDeclaration] (from packages/plugin/i18n/src/contract.ts): export interface TranslateOptions{ params?: Record; fallback?: string; } TranslationDictionary [InterfaceDeclaration] (from packages/plugin/i18n/src/contract.ts): export interface TranslationDictionary{ readonly [key: string]: string | TranslationDictionary; } createI18nCapability [FunctionDeclaration] (from packages/plugin/i18n/src/controller.ts): export function createI18nCapability( ctx: PluginContext, config: I18nConfig ={ } , ): I18nHostCapability{ const{ api, connect} = createI18nController(ctx, config); connect(); return api; } createI18nController [FunctionDeclaration] (from packages/plugin/i18n/src/controller.ts): export function createI18nController( ctx: PluginContext, config: I18nConfig ={ } , ):{ api: I18nHostCapability; connect(): void} { const report = (error: unknown) => globalThis.console?.error('[i18n] listener failed:', error); const localeChanged = createEventHook(report); const localeLoadFailed = createEventHook(report); ctx.cleanup(() =>{ localeChanged.dispose(); localeLoadFailed.dispose(); } ); const warned = new Set(); const warnOnce = (id: string, message: string) =>{ if (warned.has(id)) return; warned.add(id); console.warn(message); } ; let listedFor: I18nState['locales'] | null = null; let listed: readonly LocaleInfo[] = []; const listLocales = (): readonly LocaleInfo[] =>{ const{ locales} = ctx.getState(); if (listedFor === locales) return listed; const known: LocaleInfo[] = Object.values(locales).map((locale) => ({ code: locale.code, name: locale.name, dir: locale.dir ?? 'ltr', loaded: true, } )); for (const code of Object.keys(config.loaders ??{ } )){ if (!locales[code]) known.push({ code, name: code, dir: 'ltr', loaded: false} ); } listedFor = locales; listed = known; return listed; } ; const pending = new Map(); const settle = (code: string, outcome:{ ok: true} |{ ok: false; error: unknown} ) =>{ const waiter = pending.get(code); if (!waiter) return; pending.delete(code); if (outcome.ok) waiter.resolve(); else waiter.reject(outcome.error); } ; const supersede = () =>{ for (const [code, waiter] of pending){ pending.delete(code); waiter.reject( new PluginError('operation-cancelled', 'i18n', `switching to '${ code} ' was superseded`), ); } } ; const loaders = createLoaders(ctx, config, { settle} , (locale, error) => localeLoadFailed.emit({ locale, error: toPluginErrorInfo(toPluginError('i18n', error))} ), ); let announced = ctx.getState().locale; const announceIfChanged = () =>{ const now = ctx.getState().locale; if (now === announced || ctx.getState().loading !== null) return; const previousLocale = announced; announced = now; localeChanged.emit({ locale: now, previousLocale} ); } ; const setLocale = (code: string): Promise =>{ const state = ctx.getState(); if (state.locales[code]){ supersede(); ctx.dispatch({ type: 'I18N/SET_LOCALE', locale: code} ); announceIfChanged(); return Promise.resolve(); } if (config.loaders?.[code]){ supersede(); return new Promise((resolve, reject) =>{ pending.set(code, { resolve, reject} ); ctx.dispatch({ type: 'I18N/LOAD_STARTED', locale: code} ); } ); } return Promise.reject( new PluginError( 'not-found', 'i18n', `unknown locale '${ code} ' — not registered and no loader configured`, ), ); } ; const api ={ t: (key, options) =>{ const result = translate(ctx.getState(), key, options); if (!result.found && options?.fallback === undefined){ warnOnce( `key:${ key} `, `[i18n] missing translation "${ key} " (locale: ${ ctx.getState().locale} )`, ); } return result.text; } , hasKey: (key) => translate(ctx.getState(), key).found, getLocale: () => ctx.getState().locale, getDirection: () =>{ const{ locales, locale} = ctx.getState(); return locales[locale]?.dir ?? 'ltr'; } , listLocales, getLoadingLocale: () => ctx.getState().loading, setLocale, registerLocale: (locale): Unsubscribe =>{ ctx.dispatch({ type: 'I18N/REGISTER_LOCALE', locale} ); return () => ctx.dispatch({ type: 'I18N/UNREGISTER_LOCALE', locale: locale.code} ); } , addTranslations: (code, dictionary) =>{ if (!ctx.getState().locales[code]){ throw new PluginError('not-found', 'i18n', `unknown locale '${ code} '`); } ctx.dispatch({ type: 'I18N/ADD_TRANSLATIONS', locale: code, translations: dictionary} ); } , onLocaleChanged: localeChanged.on, onLocaleLoadFailed: localeLoadFailed.on, } satisfies I18nCapability; return{ api, connect(){ loaders.connect(); ctx.cleanup(ctx.subscribe(announceIfChanged)); } , } ; } i18nReducer [FunctionDeclaration] (from packages/plugin/i18n/src/model.ts): export function i18nReducer(state: I18nState, action: I18nAction): I18nState{ switch (action.type){ case 'I18N/SET_LOCALE':{ if (!state.locales[action.locale]) return state; if (state.locale === action.locale && state.loading === null) return state; return{ ...state, locale: action.locale, loading: null} ; } case 'I18N/REGISTER_LOCALE': return{ ...state, locales:{ ...state.locales, [action.locale.code]: action.locale} } ; case 'I18N/UNREGISTER_LOCALE':{ if (!state.locales[action.locale]) return state; const locales ={ ...state.locales} ; delete locales[action.locale]; return{ ...state, locales} ; } case 'I18N/ADD_TRANSLATIONS':{ const pack = state.locales[action.locale]; if (!pack) return state; return{ ...state, locales:{ ...state.locales, [action.locale]:{ ...pack, translations: mergeTranslations(pack.translations, action.translations), } , } , } ; } case 'I18N/LOAD_STARTED': return state.loading === action.locale ? state :{ ...state, loading: action.locale} ; case 'I18N/LOAD_FAILED': return state.loading === action.locale ?{ ...state, loading: null} : state; default: return state; } } initialI18nState [FunctionDeclaration] (from packages/plugin/i18n/src/model.ts): export function initialI18nState(config: I18nConfig): I18nState{ const fallbackLocale = config.fallbackLocale ?? 'en'; const locales: Record ={ } ; for (const locale of config.locales ?? []) locales[locale.code] = locale; const locale = config.locale ?? fallbackLocale; const needsLoad = !locales[locale] && config.loaders?.[locale] !== undefined; return{ locale, fallbackLocale, locales, loading: needsLoad ? locale : null} ; } mergeTranslations [FunctionDeclaration] (from packages/plugin/i18n/src/model.ts): export function mergeTranslations( base: TranslationDictionary, patch: TranslationDictionary, ): TranslationDictionary{ const out: Record ={ ...base} ; for (const [key, value] of Object.entries(patch)){ const current = out[key]; out[key] = typeof value === 'object' && typeof current === 'object' ? mergeTranslations(current, value) : value; } return out; }