Internationalization
The components have no text of their own: your templates hold it, so they translate with the rest of your app, whichever way you translate it.
Angular's i18n works in two ways. Its usual one builds the app once per language (ng build --localize: an /en/, an /fr/…). The other builds it once, and the browser loads the language's translations when the app starts. This page sets up the second, with @semantic-components/ui/i18n: it picks the language, loads its translations before the app starts, and sets the page's language and direction (right to left for Arabic). One build to deploy; changing language reloads the page. The dashboard app works this way, in English, French and Arabic.
1. Install Angular's localize
Then add @angular/localize/init to your build's polyfills and @angular/localize to your app's tsconfig types. Leave out the localize build option: it makes one build per locale.
2. List your locales
The source locale (your templates' language) and the others, each named in its own language. A locale's dir wins over the platform's guess.
import type { ScI18nConfig } from '@semantic-components/ui/i18n';
export const I18N: ScI18nConfig = { sourceLocale: 'en', locales: [ { id: 'en', label: 'English' }, { id: 'fr', label: 'Français' }, { id: 'ar', label: 'العربية', dir: 'rtl' }, ], // For Angular's pipes (date, number…): each its own chunk. localeData: { fr: () => import('@angular/common/locales/fr'), ar: () => import('@angular/common/locales/ar'), },};3. Load the locale first
In main.ts, before the app: its templates read their translations when they load, so the app's code is imported after.
import { bootstrapApplication } from '@angular/platform-browser';import { loadLocale } from '@semantic-components/ui/i18n';import { I18N } from './app/i18n';
await loadLocale(I18N);const [{ App }, { appConfig }] = await Promise.all([ import('./app/app'), import('./app/app.config'),]);bootstrapApplication(App, appConfig);4. Give the locales to the app
A feature of the library, withI18n(), for ScI18n — your switcher reads it.
import { provideSemanticComponents } from '@semantic-components/ui';import { withI18n } from '@semantic-components/ui/i18n';import { I18N } from './i18n';
export const appConfig: ApplicationConfig = { providers: [provideSemanticComponents({}, withI18n(I18N))],};5. Mark and translate your text
Mark text with i18n attributes (custom ids keep the files readable) and $localize in code, extract it as JSON, then write one file per locale beside it, in public/i18n/<id>.json — the format of the extracted file. Copy placeholders and plural forms from it.
ng extract-i18n --format json --output-path public/i18n --out-file messages.json6. Write your switcher
From ScI18n, with any of the components — here a select. Each name in its own language (lang); choosing one reloads the page in it, and the user’s other tabs with it.
<div scSelect [value]="i18n.locale.id" (valueChange)="i18n.setLocale($event)"> <button scSelectTrigger aria-label="Language" i18n-aria-label> <span scSelectValue [attr.lang]="i18n.locale.id">{{ i18n.locale.label }}</span> <svg scSelectIcon siChevronDownIcon></svg> </button> <ng-template scSelectPortal> <div scSelectPopup> <div scSelectList> @for (locale of i18n.locales; track locale.id) { <div scSelectItem [value]="locale.id" [label]="locale.label" [attr.lang]="locale.id"> <span scSelectItemText>{{ locale.label }}</span> <span scSelectItemIndicator><svg siCheckIcon></svg></span> </div> } </div> </div> </ng-template></div>How the locale is chosen
- The one chosen before (kept in a cookie), if it is still offered.
- Else the first of the browser's languages that the app offers — or its base:
fr-CAgivesfr. - Else the source locale.
If its translations fail to load, the app starts in the source locale and says why in the console. A message missing from a file shows its source text. <html dir> is the locale's dir when you give one, else the platform's (Intl.Locale#getTextInfo()). Angular's LOCALE_ID follows the locale, and with it the components' dates, numbers and calendars. They have no locale input: a section in another locale provides LOCALE_ID on its component — providers: [{ provide: LOCALE_ID, useValue: 'fr' }] — and every part inside follows it.
The week's first day
Each locale has its own, from Intl: Saturday for ar; where the browser can't tell (Firefox), Sunday, as en-US — Angular's default locale, and the library's. Where yours differs, say it in the config: firstDayOfWeek on the locale, by the day's name — 'mon' to 'sun', as Unicode writes them.
Intl follows the week as it is lived: in Morocco it starts on Monday, Sunday being a day off. Yet in Arabic Sunday is the first day by its name — الأحد, from أحد, one; the days to Thursday are numbered after it — and many people count from it. Set Sunday where your users do.
export const I18N: ScI18nConfig = { sourceLocale: 'en', locales: [ { id: 'en', label: 'English' }, { id: 'fr', label: 'Français' }, // Sunday: Intl says Saturday for ar. { id: 'ar', label: 'العربية', dir: 'rtl', firstDayOfWeek: 'sun' }, // Morocco: Intl says Monday (Sunday is off); Sunday here, the first by its name. { id: 'ar-MA', label: 'العربية (المغرب)', dir: 'rtl', firstDayOfWeek: 'sun' }, ], localeData: { fr: () => import('@angular/common/locales/fr'), ar: () => import('@angular/common/locales/ar'), 'ar-MA': () => import('@angular/common/locales/ar-MA'), },}; The locale Angular is given then carries it, as Unicode writes it — ar-u-fw-sun — and every calendar and picker starts its weeks there. Its translations and <html lang> keep the locale's id. One calendar can still say its own: its firstDayOfWeek input.
The user's choice comes first, when your app offers one: injectFirstDayOfWeek() — always a day: theirs once they pick (kept as the cookie sc-first-day-of-week, read on the server too; their other tabs follow at once), else the locale's. Then a calendar's own firstDayOfWeek, then the locale's — your setting on it included — then Sunday, as en-US, where the browser can't tell.
A locale with its country works alike: ar-MA (Morocco) is given as ar-MA-u-fw-sun, its country kept. Its id is the key of its locale data ('ar-MA', from @angular/common/locales/ar-MA) and the name of its translations (i18n/ar-MA.json), unless it reads another locale's (below).
Digits
Each locale writes its own digits, from Intl. Where yours differs, set numberingSystem on the locale — 'latn' 123, 'arab' ١٢٣, 'arabext' ۱۲۳ — and it travels in the locale tag as ar-u-nu-latn.
The user's choice comes first, when your app offers one: injectNumberingSystem(), kept as the cookie sc-numbering-system — and their clock, injectHourCycle() (12 or 24 hours, kept as sc-hour-cycle). Every part shows its numbers, dates and times in them at once, and the user's other tabs follow; values and what is typed stay Latin. Your own numbers match with injectFormatNumber(), your dates with injectDisplayLocale(). Angular's pipes read the locale once, at start: an app that keeps them reloads the page on a change.
One translation, several locales
Egyptian and Moroccan readers each get their own locale — months, digits, separators and clock from Intl — from one translation: translationLocale on a locale names the locale whose translations it reads. Its locale data, its <html lang> and the locale Angular is given stay its own. Naming the source locale loads nothing: en-GB reads en's templates. Left out, a locale reads its own.
export const I18N: ScI18nConfig = { sourceLocale: 'en', locales: [ { id: 'en', label: 'English' }, // Both read i18n/ar.json; each is still its own locale. { id: 'ar-EG', label: 'العربية (مصر)', dir: 'rtl', translationLocale: 'ar' }, { id: 'ar-MA', label: 'العربية (المغرب)', dir: 'rtl', translationLocale: 'ar' }, ], localeData: { 'ar-EG': () => import('@angular/common/locales/ar-EG'), 'ar-MA': () => import('@angular/common/locales/ar-MA'), },};ScI18n
| Member | Description |
|---|---|
locales | The locales offered. |
locale | The locale in use: { id, label, dir? }. |
dir | Its writing direction: ltr or rtl. |
setLocale(id) | Keeps the choice and reloads the page in it; nothing for the locale in use or one not offered. |
Limits
- Switching reloads the page: Angular translates a template once, when it is created.
- The locale is not in the URL: no per-locale links, and search engines see the source locale.
- Angular doesn't check translations when it loads them: check them in CI against the extracted file (keys and placeholders).
- It runs in the browser: a server-rendered page is in the source locale — Angular evaluates a component's messages once per server process, so server-rendered locales need its build-time i18n, a build per locale.