← Back to Angular, page 4

angular-0075 · Medium mid

How does internationalisation (i18n) work in Angular, including the i18n attribute, ICU plural expressions, $localize and locale-aware pipes?

Angular's built-in i18n marks text with the i18n attribute or $localize, extracts it to a translation file with ng extract-i18n, and the build produces one app per locale with the translations compiled in. ICU expressions handle plural and select forms, and pipes such as date and currency follow the locale.

Full answer

Internationalisation (i18n) makes an app ready for several languages. Localisation is adding the actual translations.

Build-time workflow with Angular's own tools:

  1. Mark text in templates with the i18n attribute: <h1 i18n="@@welcomeTitle">Welcome</h1>. The text after @@ is a custom id that keeps the id stable when the text changes. Attributes use i18n-title, i18n-placeholder, and so on.
  2. In TypeScript use the tagged template $localize: $localize`:@@saveLabel:Save`. It needs the @angular/localize package (added with ng add @angular/localize).
  3. Plurals and gender use ICU expressions in the template: {count, plural, =0 {No lessons} =1 {One lesson} other {{{count}} lessons}}. Different languages have different plural rules, and ICU lets the translator write them.
  4. ng extract-i18n creates a source file (XLIFF, XMB, or JSON). Translators produce one file per language.
  5. Declare locales in angular.json under i18n (source locale and each translation file) and build with "localize": true. The build creates a separate bundle per locale, with translations inserted at compile time. This is fast at runtime, but you deploy one folder per locale and switching language reloads the page.

Locale data: pipes such as date, currency, number, and percent use LOCALE_ID. Localised builds set it automatically. For a runtime-only approach you call registerLocaleData(...) and provide LOCALE_ID.

Alternatives: runtime libraries such as Transloco or ngx-translate load JSON files and switch language without a reload, at the cost of a small runtime and no compile-time check. Choose built-in i18n for performance and strictness, and a runtime library when users must change language instantly or translations change often.

Do not build sentences by joining pieces. Word order differs between languages, so translate whole messages with placeholders.

typescript
import '@angular/localize/init';
import { CurrencyPipe, DatePipe, registerLocaleData } from '@angular/common';
import localeHi from '@angular/common/locales/hi';
import { Component, LOCALE_ID, signal } from '@angular/core';

// angular.json (excerpt):
//   "i18n": { "sourceLocale": "en-US", "locales": { "hi": "src/locale/messages.hi.xlf" } }
//   "build": { "configurations": { "hi": { "localize": ["hi"] } } }

registerLocaleData(localeHi); // only needed for runtime locale switching

@Component({
  selector: 'app-lesson-list',
  imports: [DatePipe, CurrencyPipe],
  providers: [{ provide: LOCALE_ID, useValue: 'en-US' }],
  template: `
    <h1 i18n="@@lessonsTitle">Your lessons</h1>

    <p i18n="@@lessonCount">
      {count(), plural, =0 {No lessons yet} =1 {One lesson} other {{{ count() }} lessons}}
    </p>

    <input i18n-placeholder="@@searchPlaceholder" placeholder="Search lessons" />

    <p>{{ price | currency: 'INR' }} on {{ starts | date: 'longDate' }}</p>
    <button (click)="count.set(count() + 1)">{{ addLabel }}</button>
  `,
})
export class LessonListComponent {
  readonly count = signal(0);
  readonly price = 499;
  readonly starts = new Date(2026, 11, 14);
  readonly addLabel = $localize`:@@addLessonLabel:Add a lesson`;
}

Angular 20.3, type-checked. The `angular.json` settings are shown in the comment. Run `ng extract-i18n` to produce the translation source file.

Key points

  • Mark text with `i18n`, `i18n-attr`, or `$localize`, then run `ng extract-i18n`.
  • ICU expressions handle plural and select forms.
  • The built-in approach compiles one bundle per locale. Runtime libraries switch without reload.
  • Pipes use `LOCALE_ID`. Never concatenate translated fragments.

Common mistakes

  • Joining translated words into a sentence in code, which breaks in other languages.
  • Forgetting custom ids, so every text edit invalidates the translation.
  • Hard-coding date or currency formats instead of using pipes.

Follow-ups

  • How would you let a user change language without reloading?
  • How are plurals different in Arabic or Russian compared with English?
  • How do you serve each locale's build from the server?

Tips

  • Describe both workflows (compile time and runtime) and give a rule for choosing.
  • Mention ICU for plurals. It shows you have dealt with real translation.
  • Mention custom ids and the translator hand-off.

Evaluation rubric

  • Knows the i18n attribute, $localize, extraction, and per-locale builds.
  • Understands ICU and locale-aware pipes.
  • Can compare with runtime libraries.