Skip to content
joinery / catalog

@joinery/i18n

A vendor-neutral client for locale messages: a framework-agnostic core plus an optional Nuxt module that wires a TMS into @nuxtjs/i18n behind a cached server route. Vendors: internal (our own translations server) and tolgee.

📦 @joinery/i18n

A vendor-neutral client for locale messages: a framework-agnostic core plus an optional Nuxt module that wires a TMS into @nuxtjs/i18n behind a cached server route. Vendors: internal (our own translations server) and tolgee.

ImportContainsDepends on
@joinery/i18nDomain, ports, adapters, vendor registry, cache keyofetch, ohash
@joinery/i18n/nuxtThe Nuxt module — see src/nuxt/README.md@nuxt/kit, @nuxtjs/i18n, h3, nitropack
@joinery/i18n/testingtolgeeHandlers(baseURL, translations), MSW handlers faking Tolgeemsw

The second and third entries' dependencies are optional peers. The structure and the vendor contract mirror @joinery/content; keep the two matching.

Structure

src/core/domain/        Locale, TranslationMap, errors, vendor config — imports nothing
src/core/ports/         HttpClient and TranslationProvider
src/core/adapters/      OfetchHttpClient, InternalProvider, TolgeeProvider
src/core/registry.ts    vendor name → provider, and the config type
src/core/translationsKey.ts  the cache key
src/nuxt/               the Nuxt module
src/testing/            fake vendors for consumers' e2e

Vendors

A vendor is one entry in core/registry.ts, which drives both the runtime lookup and the config type. options holds credentials only, and is required when the provider declares it, forbidden when it doesn't:

{ name: "internal", project: "external", baseURL }               // options not allowed
{ name: "tolgee", project: "1234", baseURL, options: { token } } // options required

Adding a vendor (the i18n-new-vendor skill walks it):

  1. Write core/adapters/providers/<Name>Provider.ts extending TranslationProvider, overriding getTranslations. File, class and registry key share the name.
  2. If it takes options, override configProblems() — see validation.
  3. Add one line to providers.

The ports

interface HttpClient {
    get<T>(url: string, options?: RequestOptions): Promise<HttpResponse<T>>
}

abstract class TranslationProvider<T extends object = object> {
    abstract getTranslations(locale: Locale): Promise<TranslationMap>
    protected configProblems(): string[]
}

The generic is what the registry reads to make options required or forbidden. Every failed request, including a 200 that isn't JSON, becomes an UpstreamError.

Validation

A provider validates in its constructor: project non-empty, baseURL absolute, and the vendor's options through configProblems() (tolgee needs a token). Every problem is reported at once in one MisconfiguredVendorError (a 500). It's hand-written rather than a schema library, since it only checks a few strings whose shape the types already guarantee.

Framework-agnostic use

const http = new OfetchHttpClient(ofetch.create({ baseURL }));
const provider = await createProvider({ name: "internal", project: "external", baseURL }, http);
const messages = await provider.getTranslations("en-GB");

The core doesn't cache. translationsKey(vendor, locale) is the identity to cache under: the vendor config minus options (credentials aren't identity), hashed to word characters behind a readable vendor_locale_ prefix, because Nitro strips everything else from a key.

Fake vendors

setupServer(...tolgeeHandlers(baseURL, { "es-ES": messages })).listen();

Serves GET <baseURL>/v2/projects/:project/translations/:locale, and a 404 for a locale it wasn't given. There's no fake for internal; run the real server instead.

Errors

All extend TranslationsError and carry their own statusCode.

ErrorStatusRaised when
UndefinedLocaleError404No locale, or one the consumer never declared
UndefinedVendorError500Config names an unregistered vendor
MisconfiguredVendorError500The vendor's config can't work; problems lists why
UndefinedLocaleProviderError500The vendor doesn't hold a locale the config claims
UpstreamError502The vendor failed. Always 502: the locale is checked first, so no upstream answer is the caller's fault
TranslationsUnavailableError502Any other read failure, such as a mapping bug

Known gaps

GapNote
No locale fallback or local overridesAdd a service over the port when there's behaviour to put in it
Vendor plumbing duplicated with @joinery/contentDeliberate until a third consumer (D2)