Two languages, no runtime cost: what Angular's i18n really does
To make an Angular site available in Italian and in English there are two ways. The first translates while the site
runs: a library loads a dictionary and replaces the texts in the browser. The second translates when you compile:
the compiler replaces the texts, and you get one application per language. Angular's built-in internationalization
(@angular/localize) does the second. The result, on version 22.2.6 which I used (the latest on npm on 13
November 2026), is one folder per language with already translated texts and no dictionary to download.
The price is rigidity: the language is fixed by the build, changing it means opening another address, and every extra language is a translation file to maintain. This article walks through the flow with a test project created with the CLI, showing the real files and the real errors, including two situations in which a translation is missing and the build does not notice the way you expect. In the final part it tells, briefly, why this site chose only two languages. The test project is written in Italian, so the texts inside the listings are Italian on purpose: it is the source language of the experiment, and I gloss them in the text.
Marking texts: template and code
In the template you add the i18n attribute to the element whose content must be translated, and
i18n-title (or i18n- followed by the name) for an attribute. With @@name you give the unit a
custom id, and it is the choice I recommend: without it, the id is a number computed from the text. For plurals
and choices there is the ICU syntax, plural and select, with the keywords in the form of the
source. Outside templates you use the $localize template literal, with the same format:
:meaning|description@@id:text, where meaning and description are optional and help whoever translates.
<h1 i18n="@@home.titolo">Catalogo dei corsi</h1>
<p i18n="@@home.conteggio">
{corsi(), plural, =0 {Nessun corso} =1 {Un corso} other {{{ corsi() }} corsi}}
</p>
<p i18n="@@home.livello">
Livello: {livello(), select, base {principiante} avanzato {esperto} other {non definito}}
</p>
<button type="button" i18n="@@azione.iscriviti" i18n-title="@@azione.iscriviti.titolo" title="Iscriviti subito">
Iscriviti
</button>
<p>{{ benvenuto }} - {{ etichetta('Angular') }}</p>
<p i18n>Scegli un corso</p>
The title and the button are the simple cases. The count paragraph uses plural with three cases (=0,
=1, other) and the interpolation {{ corsi() }} inside the other case; the Italian texts mean "No course", "One course" and "N courses". The level paragraph uses
select, which chooses among words based on the value of an expression ("Livello" is "Level"; the words are "beginner", "expert" and "undefined"). The last paragraph, <p i18n>
without an id ("Scegli un corso" is "Choose a course"), is used further on to show what happens when the id is computed. The TypeScript part follows.
import { Component, signal } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.html',
})
export class App {
protected readonly corsi = signal(3);
protected readonly livello = signal<'base' | 'avanzato'>('base');
// Outside the template you use the $localize template literal, with the same custom id
protected readonly benvenuto = $localize`:Titolo della pagina|Saluto in testa al catalogo@@home.benvenuto:Benvenuto nel catalogo`;
protected etichetta(nome: string): string {
return $localize`:@@corso.etichetta:Corso di ${nome}:nome:`;
}
}
$localize is used like a tagged template literal, and Angular replaces it at build time. In the text
:Titolo della pagina|Saluto in testa al catalogo@@home.benvenuto: the meaning is "Titolo della pagina" ("Page title"), the
description "Saluto in testa al catalogo" ("Greeting at the top of the catalogue"), and the id home.benvenuto. In the second call,
${nome}:nome: assigns the name nome to the placeholder: in the translation file it will appear as
<x id="nome"/> instead of an anonymous number, and whoever translates understands what it stands for.
Configure, extract, translate
The first command adds the package and, as the output shows, modifies four files: the types import in
main.ts, the two tsconfig files and angular.json, where it adds the
@angular/localize/init polyfill to the build.
npx ng add @angular/localize --skip-confirmation
# Output:
# UPDATE src/main.ts (267 bytes)
# UPDATE tsconfig.app.json (429 bytes)
# UPDATE tsconfig.spec.json (436 bytes)
# UPDATE angular.json (1987 bytes)
Then the languages are declared in angular.json. The sourceLocale property is the language in which you wrote the
texts: if you do not declare it, it is en-US, and I verified it, because the first extraction I did before
setting it reported source-language="en-US" on Italian texts. The locales object associates each
language with its file; localize: true asks for one build per declared language; and
i18nMissingTranslation, which I discuss below, decides what happens when a translation is missing.
{
"projects": {
"portale": {
"i18n": {
"sourceLocale": "it",
"locales": {
"en": "src/locale/messages.en.xlf"
}
},
"architect": {
"build": {
"options": {
"polyfills": [
"@angular/localize/init"
],
"localize": true,
"i18nMissingTranslation": "error"
}
}
}
}
}
}
With the configuration in place, ng extract-i18n writes the source file, by default in XLIFF 1.2 format. The number
of extracted messages is nine in the first version and ten with the paragraph added at the end: the seven markers of the template and of the
code, plus two more messages, as you see below.
npx ng extract-i18n --output-path src/locale
# Output:
# Extraction Complete. (Messages: 10)
The messages.xlf file contains one trans-unit per message, with the source line. A detail the
first extraction reveals: ICU messages generate two units. The outer unit, with the custom id, contains only
the <x id="ICU"/> placeholder; the inner unit contains the plural text, with a computed numeric id.
You must translate both, and the numeric id changes if the plural text changes.
<?xml version="1.0" encoding="UTF-8" ?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="it" datatype="plaintext" original="ng2.template">
<body>
<trans-unit id="home.titolo" datatype="html">
<source>Catalogo dei corsi</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/app.html</context>
<context context-type="linenumber">1,3</context>
</context-group>
</trans-unit>
<!-- ... -->
<trans-unit id="home.conteggio" datatype="html">
<source> <x id="ICU" equiv-text="{corsi(), plural, =0 {Nessun corso} =1 {Un corso} other {{{ corsi() }} corsi}}" xid="3885689419089918158"/>
</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/app.html</context>
<context context-type="linenumber">4,7</context>
</context-group>
</trans-unit>
<trans-unit id="3802928872103069837" datatype="html">
<source>{VAR_PLURAL, plural, =0 {Nessun corso} =1 {Un corso} other {<x id="INTERPOLATION"/> corsi}}</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/app.html</context>
<context context-type="linenumber">4,7</context>
</context-group>
</trans-unit>
<!-- ... -->
</body>
</file>
</xliff>
For English you copy the file to messages.en.xlf, add target-language="en" and write a
<target> after each <source>. In the plural unit the target keeps the placeholders and the
keywords (=0, other, <x id="INTERPOLATION"/>) and translates only the text between the braces.
<?xml version="1.0" encoding="UTF-8" ?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="it" target-language="en" datatype="plaintext" original="ng2.template">
<body>
<trans-unit id="home.titolo" datatype="html">
<source>Catalogo dei corsi</source>
<target>Course catalogue</target>
<context-group purpose="location">
<context context-type="sourcefile">src/app/app.html</context>
<context context-type="linenumber">1,3</context>
</context-group>
</trans-unit>
<!-- ... -->
<trans-unit id="3802928872103069837" datatype="html">
<source>{VAR_PLURAL, plural, =0 {Nessun corso} =1 {Un corso} other {<x id="INTERPOLATION"/> corsi}}</source>
<target>{VAR_PLURAL, plural, =0 {No courses} =1 {One course} other {<x id="INTERPOLATION"/> courses}}</target>
<context-group purpose="location">
<context context-type="sourcefile">src/app/app.html</context>
<context context-type="linenumber">4,7</context>
</context-group>
</trans-unit>
<!-- ... -->
</body>
</file>
</xliff>
Building: one folder per language
ng build produces one application per language. The official guide describes it like this: the CLI registers the locale data,
puts each version in a dedicated folder under outputPath and adjusts the document's base href. You
see it in the file tree and in two lines of each index.html.
npx ng build
# Output:
# $ find dist/portale/browser -maxdepth 2 -type f | sort
# dist/portale/browser/en/favicon.ico
# dist/portale/browser/en/index.html
# dist/portale/browser/en/main-RRRQUTAV.js
# dist/portale/browser/en/polyfills-IQFQBAKS.js
# dist/portale/browser/en/styles-5INURTSO.css
# dist/portale/browser/it/favicon.ico
# dist/portale/browser/it/index.html
# dist/portale/browser/it/main-RRRQUTAV.js
# dist/portale/browser/it/polyfills-IQFQBAKS.js
# dist/portale/browser/it/styles-5INURTSO.css
# $ grep -o -E "<html[^>]*>|<base[^>]*>" dist/portale/browser/*/index.html
# dist/portale/browser/en/index.html:<html lang="en" dir="ltr" data-beasties-container>
# dist/portale/browser/en/index.html:<base href="/en/">
# dist/portale/browser/it/index.html:<html lang="it" dir="ltr" data-beasties-container>
# dist/portale/browser/it/index.html:<base href="/it/">
# $ grep -c "Catalogo dei corsi" dist/portale/browser/*/main-*.js
# dist/portale/browser/en/main-RRRQUTAV.js:0
# dist/portale/browser/it/main-RRRQUTAV.js:1
# $ grep -c "Course catalogue" dist/portale/browser/*/main-*.js
# dist/portale/browser/it/main-RRRQUTAV.js:0
# dist/portale/browser/en/main-RRRQUTAV.js:1
The two folders have the same files, but the lang attribute and the base href are different
(/it/ and /en/). The grep count shows the build-time substitution: the Italian title
appears only in the Italian bundle, the English one only in the English. The two main files have the same size
within a few bytes (in my first build, 215,784 and 215,774 bytes), and the Italian polyfills file is larger than the English
one (2,205 against 68 bytes), probably because of the locale data that the CLI registers. A practical consequence that the guide points out: ng serve localizes a
single language at a time, so in development you work on the source language or on a single translation.
When a translation is missing: three cases, and one silent
The question to ask before going to production is what happens if something is missing in messages.en.xlf. I tried
three situations on the same application, one at a time, clearing the build cache (NG_BUILD_CACHE=0).
- the
home.titolounit is deleted from the English file, with the default configuration; - the same unit is deleted, but with
"i18nMissingTranslation": "error"; - the unit stays in the file, but without the
<target>element.
In the first case the build ends successfully and prints
No translation found for "home.titolo" ("Catalogo dei corsi") ("Course catalogue"): in the English version the Italian text stays, and if
you do not look at the log you publish it like that. In the second case the same message becomes an error and the command exits with code 1:
it is the setting I recommend for production. The third case is the most insidious: the build ends with no
warning, even with error, and in the English version the Italian text appears. A unit that is present but empty
does not count as missing.
An id that changes by itself
If you do not assign a custom id, the id is computed from the text, and changing a single word is enough for the existing translation to stop being valid. I changed "Scegli un corso" ("Choose a course") into "Scegli un corso di studi" ("Choose a degree course") without re-extracting the file, and this is what happened:
npx ng build # after changing the text of a message without a custom id
# Output:
# ▲ [WARNING] No translation found for "1234862714337785510" ("Scegli un corso di studi").
The warning names a new numeric id, 1234862714337785510, which does not exist in the translation file: the old translation
was left orphaned. With a custom id, like home.titolo, the same change would not have had this effect. Another
effect of the id is described by the documentation: if you assign the same custom id to two different texts, the tool
extracts only the first, and its translation is used for both. Ids must therefore be unique.
A parity check that sees the three problems
A very common check compares the number of units between the source file and the translated one. It is a good start, but it sees neither a renamed id nor a missing target: in the two cases I prepared the count is identical. The check that follows compares the ids, looks for orphan units and verifies that every unit has a non-empty target (or a placeholder). I ran it on three versions of the English file: complete, with a renamed id, and with a target removed.
import { readFileSync } from 'node:fs';
// Reads an XLIFF 1.2 file and returns a map id -> "has a non-empty target"
function leggi(percorso) {
const xml = readFileSync(percorso, 'utf8');
const unita = [...xml.matchAll(/<trans-unit id="([^"]+)"[^>]*>([\s\S]*?)<\/trans-unit>/g)];
return new Map(unita.map(([, id, corpo]) => [id, /<target>[^<]*\S[^<]*<\/target>|<target>[\s\S]*?<x /.test(corpo)]));
}
const [sorgente, traduzione] = process.argv.slice(2).map(leggi);
const problemi = [];
if (sorgente.size !== traduzione.size) {
problemi.push(`different count: ${sorgente.size} versus ${traduzione.size}`);
}
for (const id of sorgente.keys()) {
if (!traduzione.has(id)) problemi.push(`unit ${id} is missing`);
}
for (const id of traduzione.keys()) {
if (!sorgente.has(id)) problemi.push(`orphan unit ${id}`);
}
for (const [id, haTarget] of traduzione) {
if (!haTarget) problemi.push(`unit without target ${id}`);
}
console.log(`units: source ${sorgente.size}, translation ${traduzione.size}`);
for (const p of problemi) console.log(`PROBLEM: ${p}`);
if (problemi.length === 0) console.log('full parity');
process.exitCode = problemi.length === 0 ? 0 : 1;
// Output:
// $ node scripts/parita.mjs src/locale/messages.xlf scenari/a-completo.xlf
// units: source 10, translation 10
// full parity
// exit=0
// $ node scripts/parita.mjs src/locale/messages.xlf scenari/b-id-diverso.xlf
// units: source 10, translation 10
// PROBLEM: unit home.titolo is missing
// PROBLEM: orphan unit home.titolo.vecchio
// exit=1
// $ node scripts/parita.mjs src/locale/messages.xlf scenari/c-senza-target.xlf
// units: source 10, translation 10
// PROBLEM: unit without target home.titolo
// exit=1
In the first run the exit code is 0. In the second the units are ten and ten, so a check on the count alone
would have approved, but the script reports that home.titolo is missing and that home.titolo.vecchio is orphan. In the
third it reports the unit without a target, the case that the build lets through. It is worth running such a check before the
build, and it is worth having it fail with a non-zero exit code as here, so that it can be put in a script
of the build chain.
Why this site stopped at two languages
This site followed the path described here, and it was broader than this. Before May 2026 two models coexisted:
Italian and English compiled with the i18n markers, and sixteen other languages served at runtime. With a decision
of 31 May 2026 the site consolidated everything on a single model, Italian plus compiled English, removed Transloco and
retired the sixteen runtime languages, which now answer with a 410 and a geographic redirect to Italian or
English. The parity check between the two translation files is part of the build. The reason was maintenance: every new
text had to be aligned across several different mechanisms, and two hand-curated languages were a sustainable commitment while eighteen
were not.
The flow with which a text is added on this site, as the previous version of this page already described it and without figures, is linear:
- you write the Italian text in the template, with the
i18n="@@id"marker; - you extract the keys with
npm run extract-i18n:no-translate, which updatesmessages.xlfwith no other steps; - you translate the new units in
messages.en.xlf; - you run
npm run i18n:validateto check parity between the two files; - you build, and the deploy remains manual: update the code on the server and restart the service.
The order matters: parity is checked after translation and before the build, because it is the check that prevents producing an incomplete English version, and it is the reason why the parity check seen above should see the ids and the targets too and not only the count.
The trade-off that comes out of it holds for any project. Build-time translation gives zero runtime cost and precise control over every text, but fixes the languages at build and multiplies them across all output folders. Runtime translation allows adding languages without recompiling, but moves the work to the browser and requires a library. In my test project the build time with one, two and three languages was respectively 5.7-6.1 s, 5.5-5.9 s and 5.3-7.4 s (three runs each, cache disabled, tiny application): I saw no differences, but I cannot tell you how a large application behaves, and for this reason I give no figure.
hreflang and deploy: two things outside Angular
For search engines every version must indicate all the versions: Google's guide says that "each language version must
list itself as well as all other language versions", and recommends x-default for unforeseen languages. Redirecting
based on the browser's language instead depends on the server: the Angular guide explains that with server-side rendering
in server mode the redirect via Accept-Language is handled by the framework, while with
outputMode: static there is no server to choose the language, and the hosting configuration does it.
What you can do now
You can mark texts and plurals, extract the source file, write the translation and build one folder per language. You know that
a unit without a target goes unnoticed even with i18nMissingTranslation: error and how to write a check that
sees it. You know why it is worth giving an id to every text. To see how these folders are served and hydrated by the server,
read Angular SSR and Incremental Hydration; for page load,
Core Web Vitals in Angular; for the migration of a
project, Migrating from Angular 17 to Angular 21.
Frequently asked questions
What happens if a translation is missing in Angular?
If the unit is missing from the file, the build warns and uses the source text; with i18nMissingTranslation set to error the build fails. If the unit is there but without a target element, in the test no warning appeared, not even with error.
Why is it worth using a custom id with @@?
Because without an id Angular computes it from the text: changing a word changes the id and the existing translation is left orphaned. With a custom id the translation follows the message, provided the id is unique.
How do I handle plurals and choices in templates?
With the ICU syntax plural and select inside an element marked with i18n. In the translation file each ICU produces two units: the outer one with the custom id and the inner one, with a numeric id, which contains the text to translate.
How do I add a third language?
You add an entry in locales inside angular.json, a translation file extracted from the source and translated, and the build produces one more folder. The texts must then be maintained in all languages.
Sources
- Angular: Merge translations into the application - one folder per language, base href, sourceLocale, ng serve with a single language, warning levels for missing translations, consulted on 2026-11-13
- Angular: Manage marked text with custom IDs - custom ids, description and meaning, uniqueness of ids, consulted on 2026-11-13
- Angular: Deploy multiple locales - redirect by Accept-Language and outputMode, consulted on 2026-11-13
- Google Search Central: Localized versions of your pages - hreflang, versions that list themselves and the others, x-default, consulted on 2026-11-13
The flow was run with CLI 22.2.6 on a project created with ng new (ng add, extraction, build with two languages, three missing-translation tests, check of the bundle contents). The build timings are those of a tiny application. Behavior in the browser, server-side rendering and the per-language redirect were not tried.

Commenti
Caricamento commenti...
Sign in to comment