Sub topic 9
Formatting for a Locale
This chapter assumes you can write Angular standalone components, use template interpolation, and read basic TypeScript. It also assumes you know how to apply built-in pipes with parameters, including date, number, percent and currency. Here you will see how those pipes decide on a regional format, and how to change that decision.
What a locale controls
A locale is an identifier such as en-US, de or fr-CA. It combines a language with an optional region. The built-in formatting pipes read locale data to decide:
- Month and weekday names, such as
JanuaryorJanuar. - The order of date parts, such as
1/15/2024or15.1.2024. - The decimal and grouping separators in numbers, such as
1,234.5or1.234,5. - The position and spacing of the currency symbol, such as
$1,234.50or1.234,50 €. - Default date patterns for names like
shortandmediumDate.
Angular's default locale is en-US. That data is bundled with @angular/common, so the pipes work with no setup. Every other locale must be registered before use.
How the pipes pick a locale
The date, number, percent and currency pipes resolve their locale in this order:
- The explicit locale argument passed to the pipe in the template.
- The
LOCALE_IDvalue from dependency injection. - The default,
en-US.
The LOCALE_ID token is the application-wide setting. The pipe argument is a per-use override.
The block above is intentionally not used. The real diagram follows.
Registering locale data and setting LOCALE_ID
Two steps are needed to switch the whole application to another locale. First, register the locale data with registerLocaleData. Second, provide LOCALE_ID.
import { registerLocaleData } from '@angular/common';
import localeDe from '@angular/common/locales/de';
import { ApplicationConfig, LOCALE_ID } from '@angular/core';
registerLocaleData(localeDe);
export const appConfig: ApplicationConfig = {
providers: [{ provide: LOCALE_ID, useValue: 'de' }],
};registerLocaleData must run before the first pipe formats a value. Calling it at the top of app.config.ts, as above, satisfies that because the config loads before bootstrapping.
With this configuration, a template such as the following produces German output:
<p>{{ createdAt | date:'longDate' }}</p>
<p>{{ 1234567.891 | number:'1.2-2' }}</p>
<p>{{ 1234.5 | currency:'EUR' }}</p>For createdAt set to 15 January 2024, the output is 15. Januar 2024, 1.234.567,89 and 1.234,50 €. The format strings stay the same. Only the data behind them changes.
Overriding the locale for one pipe
The last parameter of each of these pipes is the locale. The position differs per pipe, so check the signature.
| Pipe | Signature (parameters in order) |
|---|---|
| `date` | `format`, `timezone`, `locale` |
| `number` | `digitsInfo`, `locale` |
| `percent` | `digitsInfo`, `locale` |
| `currency` | `currencyCode`, `display`, `digitsInfo`, `locale` |
For example, to show a French price in an otherwise English application:
<p>{{ price | currency:'EUR':'symbol':'1.2-2':'fr' }}</p>
<p>{{ createdAt | date:'fullDate':undefined:'fr' }}</p>Pass undefined to skip a parameter you want to leave at its default. The date example skips timezone this way.
The locale used in a per-pipe override still needs registered data. A common pattern is to register several locales at startup:
import { registerLocaleData } from '@angular/common';
import localeFr from '@angular/common/locales/fr';
import localeDe from '@angular/common/locales/de';
import localeJa from '@angular/common/locales/ja';
registerLocaleData(localeFr);
registerLocaleData(localeDe);
registerLocaleData(localeJa);Each locale file adds some kilobytes to your bundle, so register only what the application can display.
Currency details that depend on the locale
The currency pipe separates two things that people often confuse:
- The currency code (
USD,EUR,JPY) says which currency the amount is in. You supply it. - The locale says how to write the amount. The pipe reads it.
Changing the locale never converts an amount. {{ 10 | currency:'USD' }} with the de locale gives 10,00 $. The value stays 10 US dollars. Only the layout changes.
The display parameter controls the symbol:
| Value | Result for `USD` in `en-US` |
|---|---|
| `'code'` | `USD10.00` |
| `'symbol'` (default) | `$10.00` |
| `'symbol-narrow'` | `$10.00` |
| `'name'` | `10.00 US dollars` |
The difference between symbol and symbol-narrow shows up for currencies that share a symbol. For example, CAD is CA$ in symbol form and $ in symbol-narrow form in en-US.
The default number of decimal digits also depends on the currency. JPY has none, so {{ 1500 | currency:'JPY' }} renders ¥1,500 without a decimal part. Passing digitsInfo overrides this.
Date patterns and the locale
Named formats such as short, medium, long, full, shortDate and mediumDate are looked up from locale data. Their layout therefore changes with the locale. A custom pattern is literal:
<p>{{ createdAt | date:'yyyy-MM-dd' }}</p>
<p>{{ createdAt | date:'MMMM d, y' }}</p>The first line looks the same in every locale. The second keeps your word order, but the month name is still localized. In German, MMMM becomes Januar while your pattern still places the month before the day. If you want a layout that follows regional convention, prefer a named format over a hand-written one.
Be careful with two pattern letters. y is the calendar year and Y is the week-based year. Near 1 January, Y can give a different year. Use y unless you are formatting week numbers.
Time zones are separate from locales
A locale does not change the time zone. The date pipe uses the time zone of the browser unless you give the timezone parameter, for example 'UTC' or '+0200'.
<p>{{ createdAt | date:'short':'UTC':'de' }}</p>You can also set a default time zone for all date pipes with the DATE_PIPE_DEFAULT_OPTIONS token:
import { DATE_PIPE_DEFAULT_OPTIONS } from '@angular/common';
import { ApplicationConfig } from '@angular/core';
export const appConfig: ApplicationConfig = {
providers: [
{ provide: DATE_PIPE_DEFAULT_OPTIONS, useValue: { timezone: 'UTC', dateFormat: 'mediumDate' } },
],
};Choosing the locale at run time
LOCALE_ID is read when a pipe is created, and a value provided with useValue is fixed. If users can switch language while the app is running, you have three common options.
- Per-pipe argument. Keep the current locale in a signal and pass it to each pipe. This needs no reload, but you must repeat the argument everywhere.
- A wrapper pipe. Write a custom pipe that reads the current locale from a service and calls the built-in pipe's
transform. This keeps templates short. Make it impure or have it take the locale as an input so it refreshes when the locale changes. - Reload with a different
LOCALE_ID. ProvideLOCALE_IDwith a factory that reads a stored preference. The user changes language, the page reloads, and the new value takes effect.
The per-pipe approach looks like this:
import { DatePipe, registerLocaleData } from '@angular/common';
import localeDe from '@angular/common/locales/de';
import localeFr from '@angular/common/locales/fr';
import { Component, signal } from '@angular/core';
registerLocaleData(localeDe);
registerLocaleData(localeFr);
@Component({
selector: 'app-locale-demo',
standalone: true,
imports: [DatePipe],
template: `
<button (click)="locale.set('de')">Deutsch</button>
<button (click)="locale.set('fr')">Français</button>
<p>{{ today | date:'fullDate':undefined:locale() }}</p>
`,
})
export class LocaleDemoComponent {
locale = signal('de');
today = new Date();
}Because the template reads locale(), the expression is re-evaluated when the signal changes. The pure date pipe sees a new argument and runs again.
Locale data for Angular's i18n
Angular's built-in internationalization (ng add @angular/localize) builds one application per locale and sets LOCALE_ID for each build. In that setup, the build registers locale data for you. The manual registerLocaleData steps in this chapter apply when you manage the locale yourself, as in a single build with a language switcher.
Common errors
NG0701: Missing locale data for the locale "xx". You used a locale without callingregisterLocaleDatafor it. Import the matching file from@angular/common/locales/and register it.- Output still in English after providing
LOCALE_ID. The provider is missing from the config that bootstraps the app, or the value is misspelled. Check the exact identifier, such asdeversusde-DE. - Locale file not found. Import paths follow the locale code, so use
@angular/common/locales/de, notde-DE. For a regional file, use for example@angular/common/locales/fr-CA. - Assuming the locale converts currency. It only formats. Convert amounts in your own code or on the server.
- A hand-written pattern that ignores regional order. Use a named format, or accept that the layout is yours.
Exercise
An English-language app sets no LOCALE_ID. A template contains {{ 1234.5 | currency:'EUR':'symbol':'1.2-2':'de' }} and the app never calls registerLocaleData for German. What happens, and what two changes would make the output 1.234,50 €?
Show answer
Angular throws a missing locale data error (NG0701) because the de data is not registered. Import localeDe from @angular/common/locales/de and call registerLocaleData(localeDe) before the pipe runs. The explicit 'de' argument then selects German formatting, so no LOCALE_ID is needed for this one pipe. Alternatively, provide LOCALE_ID as 'de' and drop the argument.
Exercise
A page shows {{ 25 | currency:'USD' }} with LOCALE_ID set to de. A colleague says the value is now converted to euros. Is that right? What does the page show?
Show answer
No. The locale only changes the layout. The currency code USD still says the amount is in US dollars, so the page shows 25,00 $. Converting to euros needs an exchange rate and your own code.