For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /guide/i18n/index.md.

Internationalization

Translate your app with Angular's @angular/localize. Mark text with i18n or $localize, extract it with ng extract-i18n, and load the right translation when the app starts. One bundle serves every language.

What's different on Lynx

  • Translations load at runtime. Angular's build-time translation, with one build per language, isn't supported. Your app calls loadTranslations() before it starts.
  • The language comes from the host app. AngularLynx reads appLocale from the host app's GlobalData. The iOS and Android apps that ng build --platform generates send the phone's language as a tag such as 'fr-FR'. Lynx Explorer sends none, so there your app uses the sourceLocale from angular.json.
  • Lynx has no Intl. $localize and Angular's pipes don't need it. For code that calls Intl, the build can add the APIs it uses.
  • Switching language means restarting the app, for most text. Template i18n text and LOCALE_ID don't change after they're first used. Some strings can switch live.

Set up

ng generate @blotch/angular-lynx:add-i18n

This command:

  • installs @angular/localize, at the same version as @angular/core
  • adds "i18n": { "sourceLocale": "en-US" } to your project in angular.json
  • adds @angular/localize/init to the build's polyfills, so $localize exists before your code runs
  • adds @angular/localize to types in tsconfig.app.json, so TypeScript knows $localize
  • adds provideLocale() to your app config
  • sets up ng extract-i18n to write your messages as JSON to src/locale
src/app/app.config.ts
import { type ApplicationConfig } from '@angular/core';
import {
  provideLocale,
  provideRenderer,
  provideRouter,
} from '@blotch/angular-lynx';
import { routes } from './app.routes';

export const appConfig: ApplicationConfig = {
  providers: [
    provideLocale(),
    provideRenderer(),
    provideRouter(routes),
  ],
};

provideLocale() sets Angular's LOCALE_ID from appLocale. It falls back to sourceLocale, then to en-US:

  • When the host app sends no appLocale.
  • When Angular has no locale data for appLocale. Angular's date and number pipes would throw for it, so provideLocale() falls back and logs a warning.

So a phone set to a language your app doesn't support gets your source language, as a native app does. Register the data of each language you support with registerLocaleData().

Mark text for translation

src/app/greeting.ts
import { Component, computed, signal } from '@angular/core';
import { LYNX_ELEMENTS } from '@blotch/angular-lynx';

@Component({
  selector: 'app-greeting',
  imports: [LYNX_ELEMENTS],
  template: `
    <text i18n="@@greeting">Hello, world!</text>
    <text>{{ welcome() }}</text>
  `,
})
export class Greeting {
  readonly userName = signal('Ada');
  readonly welcome = computed(
    () => $localize`:@@welcome:Welcome, ${this.userName()}:userName:!`,
  );
}
  • i18n on an element marks its text for translation. @@greeting sets the message ID.
  • $localize marks a string in TypeScript. :@@welcome: sets its ID, and :userName: names the placeholder for ${this.userName()}.
  • Give every message an @@ ID. Translation files are keyed by ID, and a custom ID stays the same when you edit the English text.

Extract the messages

ng extract-i18n

It writes src/locale/messages.json with every message in your source language:

src/locale/messages.json
{
  "locale": "en-US",
  "translations": {
    "greeting": "Hello, world!",
    "welcome": "Welcome, {$userName}!"
  }
}

Copy it to one file per language, such as messages.fr.json, and translate the values. Keep placeholders like {$userName} as they are.

src/locale/messages.fr.json
{
  "locale": "fr",
  "translations": {
    "greeting": "Bonjour le monde !",
    "welcome": "Bienvenue, {$userName} !"
  }
}

Why JSON

The JSON format's translations object is exactly what loadTranslations() takes. That's why add-i18n picks it over Angular's default, XLIFF.

To extract XLIFF for a translation tool, pass --format xlf. Then convert each translated file to an object yourself.

The extract-i18n target

ng extract-i18n takes Angular's options. add-i18n sets two of them on the extract-i18n target in angular.json:

angular.json
"extract-i18n": {
  "builder": "@blotch/angular-lynx-build:extract-i18n",
  "options": {
    "format": "json",
    "outputPath": "src/locale"
  }
}

The builder runs Angular's own extractor. Angular CLI can't pick an extractor for the AngularLynx build by itself, so an app needs this target:

  • ng add writes it.
  • ng update @blotch/angular-lynx adds it to apps made with 22.2.2 or older.

Load a translation

Call loadTranslations() in main.ts, before bootstrapApplication():

src/main.ts
import { loadTranslations } from '@angular/localize';
import { bootstrapApplication, getLynxAppLocale } from '@blotch/angular-lynx';
import { App } from './app/app';
import { appConfig } from './app/app.config';
import fr from './locale/messages.fr.json';

if (getLynxAppLocale()?.startsWith('fr')) {
  loadTranslations(fr.translations);
}

bootstrapApplication(App, appConfig);
  • getLynxAppLocale() reads appLocale without Angular's dependency injection, so it works before the app starts. It returns undefined when the host app hasn't set one.
  • Match the language, not the whole tag. The phone may say 'fr-FR', 'fr-CA' or 'fr'.
  • Load before bootstrap. Angular translates template text and $localize strings when it creates each component. It uses whatever was loaded at that moment.
  • Messages without a translation show the source text and log a warning.

Next steps

  • Read and switch the locale: show the current locale, and change language while the app runs.
  • Use Intl: add the Intl APIs Lynx lacks, for code that formats or sorts by locale.