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/locale.md.

Read and switch the locale

Show which locale your app runs in, and change language without restarting, for the strings that allow it. This builds on Internationalization.

Read the current locale

Inject LynxLocale for the locale as a signal:

src/app/locale-badge.ts
import { Component, inject } from '@angular/core';
import { LYNX_ELEMENTS, LynxLocale } from '@blotch/angular-lynx';

@Component({
  selector: 'app-locale-badge',
  imports: [LYNX_ELEMENTS],
  template: `<text>Locale: {{ locale.locale() }}</text>`,
})
export class LocaleBadge {
  readonly locale = inject(LynxLocale);
}

locale() returns appLocale as the host app sent it. Without one, it returns sourceLocale, then en-US.

Angular's pipes use LOCALE_ID instead. It also falls back when Angular has no data for appLocale (see Set up).

To format dates, numbers or currency for a locale other than en-US, register its data with registerLocaleData().

Switch language while the app runs

A $localize string inside a computed() can switch live. Make it depend on a signal, then replace the translations and change the signal:

src/app/language-picker.ts
import { Component, computed, signal } from '@angular/core';
import { clearTranslations, loadTranslations } from '@angular/localize';
import { LYNX_ELEMENTS } from '@blotch/angular-lynx';
import fr from '../locale/messages.fr.json';

@Component({
  selector: 'app-language-picker',
  imports: [LYNX_ELEMENTS],
  template: `
    <text>{{ greeting() }}</text>
    <view (bindtap)="use('fr')"><text>Français</text></view>
    <view (bindtap)="use('en')"><text>English</text></view>
  `,
})
export class LanguagePicker {
  readonly #language = signal('en');
  readonly greeting = computed(() => {
    this.#language(); // re-run when the language changes
    return $localize`:@@greeting:Hello, world!`;
  });

  use(language: string): void {
    clearTranslations();
    if (language === 'fr') {
      loadTranslations(fr.translations);
    }
    this.#language.set(language);
  }
}

These keep the language the app started with:

  • template i18n text
  • $localize strings outside a computed()
  • LOCALE_ID

To change those, the app has to start again with the new appLocale.