Skip to content

Localization

The viewer's UI strings resolve through the shared pptx.* dictionary (English built in). Svelte has no blessed i18n runtime, so this binding ships a small, dependency-free translation layer under the pptx-svelte-viewer/i18n entry point: dictionary lookup by locale with English fallback, interpolation, and a humanised fallback for missing keys.

Registering a locale

Register dictionaries (or override individual strings) with registerTranslations, then set the locale prop:

ts
// e.g. in your app entry, before the viewer mounts
import { registerTranslations } from 'pptx-svelte-viewer/i18n';

registerTranslations('fr', {
	'pptx.statusBar.slideOf': 'Diapositive {{current}} sur {{total}}',
	'pptx.toolbar.next': 'Suivant',
	// ...any subset; unset keys fall back to English
});
svelte
<PowerPointViewer source={bytes} locale="fr" />

Later registrations for the same locale are merged over earlier ones, so hosts can layer a full dictionary first and override individual keys afterwards. Register dictionaries before mounting the viewer so the labels are available on first render.

Typed dictionaries

TranslationDictionary is a plain Record<string, string>. For compile-time key checking, annotate your dictionary with the exported TranslationKey union:

ts
import type { TranslationKey } from 'pptx-svelte-viewer/i18n';

const fr: Partial<Record<TranslationKey, string>> = {
	'pptx.statusBar.slideOf': 'Diapositive {{current}} sur {{total}}',
};
registerTranslations('fr', fr);

The Svelte demo registers complete French, Spanish, and German reference dictionaries through this same API.

The fallback chain

translate resolves a key in this order:

  1. The exact locale's dictionary (fr-CA)
  2. The base language's dictionary (fr)
  3. The built-in English dictionary (translationsEn)
  4. A humanised label derived from the key (keyToLabel), so missing keys never render as raw dotted paths

Interpolation

Messages use the shared placeholder convention; parameters are passed as the second argument to the translator:

ts
import { translate } from 'pptx-svelte-viewer/i18n';

translate('en', 'pptx.statusBar.slideOf', { current: 2, total: 10 });
// "Slide 2 of 10"

Unknown placeholders are left verbatim.

The built-in language picker

File > Options > Language offers the user a locale choice at runtime, driven by three props (see Component Props):

  • defaultLocale: initial selection; falls back to the localStorage-persisted choice, then the locale prop.
  • availableLocales: the choices offered (LocaleCatalogEntry[]). Defaults to every locale registered via registerTranslations, labelled from the shared LOCALE_CATALOG (en, fr, es, de ship with labels; other codes get their code as label).
  • onLocaleChange: fired with the selected code. Supplying it hands persistence to the host; without it the choice is written to localStorage automatically.
ts
interface LocaleCatalogEntry {
	/** BCP-47-ish locale code, e.g. 'en', 'fr'. */
	code: string;
	/** English display name. */
	label: string;
	/** The locale's own name for itself, e.g. 'Français'. */
	nativeLabel: string;
}

Picking is not registering

The picker only switches which registered dictionary is read. Registering the dictionary itself is a separate step (registerTranslations); a picked locale with no dictionary falls back to English.

Helper reference

All from pptx-svelte-viewer/i18n:

ExportKindPurpose
registerTranslationsfunction(locale, dictionary) => void; merges over any existing dictionary for that locale.
getRegisteredLocalesfunction() => string[]; 'en' plus everything registered.
translatefunction(locale, key, params?) => string; full fallback chain.
createTranslatorfunction(getLocale) => Translator; a translator bound to a lazily-read locale.
interpolatefunction(message, params?) => string; substitution only.
translationsEnconstThe complete shared English dictionary.
LOCALE_CATALOGconstBuilt-in locale labels (en, fr, es, de).
keyToLabelfunctionHumanises a pptx.* key; the last-resort fallback.
TranslationDictionary, Translator, TranslationKey, LocaleCatalogEntrytypeThe types used above.

registerTranslations, TranslationDictionary, and Translator are also re-exported from the package root for convenience.

Released under the Apache-2.0 License.