Skip to content

@polingo/web

Browser-focused adapter that wires the core translator to the Fetch API and an optional localStorage cache.

createPolingo(options)

Creates a preloaded translator configured for browser (or edge) environments.

ts
import { createPolingo } from '@polingo/web';

const polingo = await createPolingo({
  locale: 'en',
  locales: ['en', 'es'],
  loader: { baseUrl: '/i18n' },
  cacheOptions: { ttlMs: 60_000 },
});

Options

OptionTypeDefaultDescription
localestringLocale used right after initialization.
localesstring[]Locales to preload via HTTP.
loaderWebLoaderOptions{ baseUrl: '/i18n' }Controls how catalogs are fetched.
fallbackstring'en'Fallback locale when lookups miss.
domainstring'messages'Catalog namespace.
debugbooleanfalseEmit console output for loads, misses, and fallbacks.
cachebooleantrueEnable the persistent LocalStorageCache.
cacheOptionsLocalStorageCacheOptions{}Configure cache namespace or TTL.

locales must contain at least one entry; an error is thrown otherwise so you catch misconfiguration early.

The default loader fetches catalogs from /i18n. If your JSON exports live elsewhere, override the path with loader.baseUrl or provide a custom loader.buildUrl.

WebLoader

HTTP loader used behind the scenes by createPolingo. You can instantiate it directly when composing your own translator.

ts
import { WebLoader } from '@polingo/web';
import { Translator, NoCache } from '@polingo/core';

const loader = new WebLoader({
  baseUrl: 'https://cdn.example.com/i18n',
  requestInit: { credentials: 'include' },
});

const translator = new Translator(loader, new NoCache(), {
  locale: 'en',
  fallback: 'en',
  domain: 'messages',
});

WebLoaderOptions

OptionTypeDefaultDescription
baseUrlstring'/i18n'Base path used to construct <baseUrl>/<locale>/<domain>.json.
buildUrl(locale, domain) => stringDerived from baseUrlOverride for custom catalog URLs.
fetchtypeof fetchGlobal fetchProvide a polyfill (e.g. for SSR).
requestInitRequestInitundefinedExtra options forwarded to fetch.
transformResponse(payload) => TranslationCatalogIdentityConvert custom payloads into a catalog shape.

The loader throws when the HTTP request fails (!response.ok) or when the payload cannot be coerced into a TranslationCatalog.

LocalStorageCache

Cache implementation that persists catalogs in localStorage with graceful fallback to an in-memory store when storage is unavailable or full.

ts
import { LocalStorageCache } from '@polingo/web';

const cache = new LocalStorageCache({ prefix: 'myapp', ttlMs: 5 * 60_000 });

LocalStorageCacheOptions

OptionTypeDefaultDescription
storageStorage | nullwindow.localStorage when availableSupply a custom storage implementation.
prefixstring'polingo'Prefix used for stored keys.
ttlMsnumberundefinedTime-to-live in milliseconds; when omitted, entries persist until cleared.

If serialization fails (for example due to storage limits), the cache logs a warning in debug builds and falls back to the in-memory implementation from @polingo/core.