Angular i18n helps you make your app speak different languages easily. It changes text based on the user's language.
Angular i18n built-in support
Start learning this pattern below
Jump into concepts and practice - no test required
<div i18n>Welcome to our app!</div> <!-- In TypeScript --> import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html' }) export class AppComponent {}
The i18n attribute marks text for translation.
Angular extracts these texts to create translation files.
<h1 i18n>Home Page</h1>
<button i18n="@@saveBtn">Save</button><p i18n>Today is {{ today | date }}</p>This simple component shows how to mark text for translation using i18n. When you extract and add translations, Angular will replace the text based on the user's language.
/* app.component.html */ <div i18n>Hello, welcome to our app!</div> /* app.component.ts */ import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html' }) export class AppComponent { today = new Date(); } /* Usage: Run Angular i18n extraction and build with translations to see text change */
Use the Angular CLI command ng extract-i18n to get translation files.
Translations are stored in XLIFF or other supported formats.
You need to build your app separately for each language to serve translated versions.
Angular i18n uses the i18n attribute to mark text for translation.
Extract translations with Angular CLI and provide translated files.
Build your app for each language to serve users in their language.
Practice
i18n attribute in Angular templates?Solution
Step 1: Understand the role of
Thei18nin Angulari18nattribute is used to identify text in templates that should be translated for internationalization.Step 2: Compare with other options
The other options relate to styling, data binding, and events, which are unrelated to translation.Final Answer:
To mark text for translation into different languages -> Option AQuick Check:
i18n attribute = mark text for translation [OK]
- Confusing i18n with data binding syntax
- Thinking i18n applies styles or events
- Assuming i18n is a directive instead of an attribute
Solution
Step 1: Recall Angular i18n attribute usage
Thei18nattribute is added directly to the element without brackets or asterisk.Step 2: Analyze each option
Hello, welcome!
usesi18ncorrectly.Hello, welcome!
uses property binding syntax which is incorrect.Hello, welcome!
uses structural directive syntax which is invalid here.Hello, welcome!
uses a non-existent attributei18n-text.Final Answer:
<p i18n>Hello, welcome!</p> -> Option DQuick Check:
Use plain i18n attribute without brackets or asterisk [OK]
- Using property binding syntax [i18n]
- Using structural directive syntax *i18n
- Inventing attributes like i18n-text
<h1 i18n>Welcome, {{ userName }}!</h1>What will be the output if
userName is 'Alice' and the app is built with English locale?Solution
Step 1: Understand interpolation with i18n
Thei18nattribute marks the text for translation but Angular still processes interpolation expressions like{{ userName }}.Step 2: Consider the locale and variable value
With English locale anduserNameas 'Alice', the text renders with the variable replaced by 'Alice'.Final Answer:
Welcome, Alice! -> Option CQuick Check:
i18n with interpolation outputs translated text with variables replaced [OK]
- Thinking interpolation is ignored inside i18n
- Expecting raw {{ userName }} to show
- Assuming missing translation error for default locale
ng extract-i18n but the generated translation file is empty. What is the most likely cause?Solution
Step 1: Understand what
This command extracts marked text withng extract-i18ndoesi18nattributes from templates into translation files.Step 2: Analyze why the file might be empty
If noi18nattributes exist, there is nothing to extract, resulting in an empty file.Final Answer:
No i18n attributes found in templates -> Option BQuick Check:
Empty extraction file means no i18n markers found [OK]
- Assuming CLI version causes empty file
- Thinking app components absence causes empty file
- Confusing file path issues with extraction content
Solution
Step 1: Extract translations and provide translation files
Use Angular CLI to extract text marked withi18nand create translation files for English and French.Step 2: Build the app separately for each locale
Build the app twice, once with English locale and once with French locale, so each build contains the correct translations.Final Answer:
Extract translations, provide French translation file, build app twice with different locales -> Option AQuick Check:
Angular i18n requires extraction, translation files, and separate builds [OK]
- Thinking inline translations replace translation files
- Assuming runtime service handles built-in i18n translations
- Trying to create separate components per language manually
