Sub topic 9
Formatting for a locale
This chapter assumes you know basic TypeScript, Angular components with template interpolation, and standalone components with an imports array. It also assumes you have used the date, number, percent and currency pipes.
Why locale matters
The same value is written differently around the world. The number 1234.5 is 1,234.5 in the United States and 1.234,5 in Germany. Dates, currency symbols and percent signs also change.
The date, number, percent and currency pipes follow the active locale. A locale is an identifier such as en-US or de that selects these formatting rules. Angular uses en-US unless you configure something else.
How the pipes find the locale
Angular looks for the locale in this order:
- A locale argument you pass to the pipe.
- The
LOCALE_IDtoken, which you can provide in your app configuration. - The default,
en-US.
Only en-US data is built into Angular. For any other locale you must register its data first. If you skip this step, Angular throws an error such as NG0701: Missing locale data for the locale "de".
Setting the app-wide locale
Register the locale data, then 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 loads the formatting rules. The LOCALE_ID provider tells the pipes to use them by default. Both steps are required.
With this configuration, a template like the one below produces German output.
<p>{{ 1234.5 | number:'1.2-2' }}</p>
<p>{{ 0.256 | percent }}</p>
<p>{{ 1234.5 | currency:'EUR' }}</p>The results are 1.234,50, 26 % and 1.234,50 €. Note that the decimal separator changed, the percent sign is separated by a space, and the euro symbol moved after the number.
Overriding the locale for one pipe
Each of these pipes accepts a locale. For date it is the fourth argument (format, timezone, locale). For number and percent it is the third argument (digits info, locale). For currency it is the fifth argument (code, display, digits info, locale).
<p>{{ createdAt | date:'fullDate':undefined:'fr' }}</p>
<p>{{ 1234.5 | number:'1.2-2':'de' }}</p>
<p>{{ price | currency:'USD':'symbol':'1.2-2':'de' }}</p>Each locale you use still has to be registered with registerLocaleData. This is useful when one page must show a value in a locale that differs from the rest of the app, such as a price list in several markets.
import { registerLocaleData } from '@angular/common';
import localeFr from '@angular/common/locales/fr';
import localeDe from '@angular/common/locales/de';
registerLocaleData(localeFr);
registerLocaleData(localeDe);Currency code is not the locale
A common mistake is to treat the currency and the locale as one setting. They are independent. The currency code (USD, EUR) says which money is shown. The locale says how the amount is written.
The last example above shows US dollars written with German conventions: 1.234,50 $. If prices are in euros but your user reads French, pass 'EUR' as the code and let the locale handle the layout.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `NG0701` missing locale data error | The locale was not registered | Call `registerLocaleData` before the app renders |
| Output still looks like US format | `LOCALE_ID` is missing or `registerLocaleData` ran for a different locale | Check both the provider and the registered data |
| Wrong currency symbol | The code passed to `currency` does not match the money | Pass the correct ISO code, such as `'EUR'` |
| Locale argument is ignored in the date pipe | The arguments are in the wrong position | Pass `undefined` for the timezone when you only need the locale |
How the pieces connect
Exercise
Your app provides LOCALE_ID as 'de' and registers German data. A template contains {{ 1234.5 | number:'1.2-2':'fr' }}. Which locale formats the number, and what must you do for it to work?
Show answer
The French locale formats it, because a locale argument on the pipe takes priority over LOCALE_ID. For it to work, you must also call registerLocaleData with the French data (imported from '@angular/common/locales/fr'). Otherwise Angular throws a missing locale data error.
Exercise
A date pipe should show a full date in Spanish on one page only, while the rest of the app stays in en-US. Write the pipe expression for a variable called createdAt.
Show answer
{{ createdAt | date:'fullDate':undefined:'es' }}. The Spanish data must be registered first with registerLocaleData, using the data from '@angular/common/locales/es'. The undefined keeps the default timezone.