Signals-first runtime i18n for Angular 16+: SSR-safe, hydration-friendly, ICU-lite, and configurable fallback chains.
Website
The marketing/docs site for this package family lives at apps/site (an Nx Angular application with static prerendering), deployed to Cloudflare Pages at https://i18n.ashwinsathian.com. See apps/site/README.md for local dev, build, and test commands.
Features
provideRuntimeI18n()installsI18nService,I18nPipe, and the optionalI18nCompatService(RxJS bridge)- Deterministic SSR → CSR via TransferState snapshots (see
apps/demo-ssr) - Per-key fallback chains: active lang → custom
fallbacks[]→defaultLang - Catalog caching modes:
none,memory, orstorage(localStorage revalidation) - DX helpers:
lang()signal plusgetCurrentLang(),getLoadedLangs(), andhasKey() - Hydration-safe: no DOM mutations before Angular stability
Install
npm i @ngx-runtime-i18n/angular @ngx-runtime-i18n/core
Usage
// app.config.ts
import { ApplicationConfig } from '@angular/core';
import { provideRuntimeI18n } from '@ngx-runtime-i18n/angular';
export const appConfig: ApplicationConfig = {
providers: [
provideRuntimeI18n(
{
defaultLang: 'en',
supported: ['en', 'hi', 'de'],
fallbacks: ['de'],
fetchCatalog: (lang, signal) =>
fetch(`/i18n/${lang}.json`, { signal }).then((r) => {
if (!r.ok) throw new Error(`Failed to load catalog: ${lang}`);
return r.json();
}),
onMissingKey: (key) => key,
},
{
localeLoaders: {
en: () => import('@angular/common/locales/global/en'),
hi: () => import('@angular/common/locales/global/hi'),
de: () => import('@angular/common/locales/global/de'),
},
options: {
autoDetect: true,
storageKey: '@ngx-runtime-i18n:lang',
cacheMode: 'storage',
cacheKeyPrefix: '@ngx-runtime-i18n:catalog:',
preferNavigatorBase: true,
},
}
),
],
};
<!-- Template -->
<h1>{{ 'hello.user' | i18n:{ name: username } }}</h1>
<p>{{ 'cart.items' | i18n:{ count: items().length } }}</p>
<small>Fallback → {{ 'legacy.title' | i18n }}</small>
// Component
import { Component, effect, inject, signal } from '@angular/core';
import { I18nPipe, I18nService } from '@ngx-runtime-i18n/angular';
@Component({
standalone: true,
imports: [I18nPipe],
template: `
<button (click)="switch('de')">Deutsch</button>
<div>{{ i18n.t('hello.user', { name: 'Ashwin' }) }}</div>
`,
})
export class ToolbarComponent {
i18n = inject(I18nService);
loaded = signal<string[]>([]);
constructor() {
effect(() => {
this.i18n.lang(); // subscribe to the signal
this.loaded.set(this.i18n.getLoadedLangs());
});
}
async switch(lang: string) {
if (this.i18n.ready()) await this.i18n.setLang(lang);
}
}
Need RxJS? Inject I18nCompatService for lang$, ready$, and t() without signals.
DX helpers
I18nService exposes synchronous utilities for tooling and debugging:
getCurrentLang(): snapshot of the active language without reading the signalgetLoadedLangs(): list of catalogs currently cached in memoryhasKey(key, lang = current): determine if a key exists before rendering
Pair these with Angular effect()/computed() to display diagnostics in dev tools:
const lang = this.i18n.getCurrentLang();
const loaded = this.i18n.getLoadedLangs();
const missingLegacy = !this.i18n.hasKey('legacy.title');
Fallback chains & catalog caching
- Configure
RuntimeI18nConfig.fallbacks?: string[]. Lookup order is active language, then each fallback in order, thendefaultLang. Missing keys then flow toonMissingKey. RuntimeI18nOptions.cacheModecontrols persistence:none: keep only the active fallback chain in memorymemory(default): cache every loaded language for the sessionstorage: hydrate catalogs fromlocalStorage, then refresh them in the background (cacheKeyPrefixcontrols storage keys)
- Server environments never touch
localStorage; hydration stays deterministic when you seed TransferState.
SSR example
The repo ships with apps/demo-ssr, a full Express + Angular SSR demo. For each request, the server picks a language from ?lang, a lang cookie or Accept-Language, reads the catalogs and passes them to handle() as the request context. provideRuntimeI18nSsr() seeds TransferState from that, and the browser hydrates in the same language. Run it locally:
nx serve demo-ssr # dev server, http://localhost:4200
nx build demo-ssr && node dist/apps/demo-ssr/server/server.mjs # production build, http://localhost:4000
Demos
nx serve demo # CSR demo with caching + fallbacks
nx serve demo-ssr # SSR + hydration demo
Catalog JSON lives under apps/demo*/public/i18n/<lang>.json.
Packages
| Package | Description | npm |
|---|---|---|
@ngx-runtime-i18n/core |
Framework-agnostic primitives (ICU-lite formatter, shared types). | Published |
@ngx-runtime-i18n/angular |
Angular wrapper (signals, SSR-safe service, pipes). | Published |
@ngx-runtime-i18n/primeng |
Optional PrimeNG adapter that mirrors runtime language changes. | Published |
@ngx-runtime-i18n/material |
Optional Angular Material adapter (paginator, sort, stepper, datepicker labels). | Published |
@ngx-runtime-i18n/schematics |
ng add schematic that scaffolds provideRuntimeI18n() into an existing project. |
Published |
@ngx-runtime-i18n/cli |
CLI for extracting translation keys from source and validating catalogs. | Published |
Docs
- Angular package README
- Core package README
- PrimeNG adapter README
- Material adapter README
- Schematics README
- CLI README
- Changelog
- Contributing
License
MIT © Ashwin Sathian
