Marking text for translation helps your app show content in different languages. It tells Angular which parts to translate.
Marking text for translation in Angular
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Angular
<p i18n>Text to translate</p>
Use the
i18n attribute on HTML elements to mark text for translation.You can add a description like
i18n="meaning|description" to help translators.Examples
Angular
<h1 i18n>Welcome to our site</h1>
Angular
<button i18n="button label|Submit button">Submit</button>Angular
<p i18n>Click <strong>here</strong> to continue.</p>Sample Program
This simple Angular template marks text and buttons for translation so they can appear in different languages.
Angular
<!-- app.component.html --> <h2 i18n>Choose your language</h2> <button i18n="button label|English language button">English</button> <button i18n="button label|Spanish language button">Español</button>
Important Notes
Mark only the text users see, not variable names or code.
Use clear descriptions in i18n attributes to help translators.
Angular extracts these marked texts into files for translators to work on.
Summary
Use the i18n attribute to mark text for translation in Angular templates.
Adding descriptions helps translators understand context.
This prepares your app to support multiple languages easily.
Practice
1. What is the primary purpose of the
i18n attribute in Angular templates?easy
Solution
Step 1: Understand the role of
Thei18nin Angulari18nattribute is used to mark text that should be translated for different languages.Step 2: Differentiate from other attributes
It is not related to styling, animations, or data binding, which are handled by other Angular features.Final Answer:
To mark text for translation in the app -> Option AQuick Check:
i18n attribute = mark text for translation [OK]
Hint: Remember: i18n means internationalization for translation [OK]
Common Mistakes:
- Confusing i18n with styling or animations
- Thinking i18n binds data instead of marking text
- Assuming i18n is a directive instead of an attribute
2. Which of the following is the correct syntax to mark a paragraph for translation with a description in Angular?
easy
Solution
Step 1: Recognize the correct i18n attribute format
Thei18nattribute can include a description or meaning using the syntaxi18n="@@desc"to help translators.Step 2: Check each option's syntax
Welcome to our site!
correctly usesi18n="@@desc"inside the paragraph tag. Options B, C, and D misuse the attribute or place description incorrectly.Final Answer:
<p i18n="@@desc">Welcome to our site!</p> -> Option DQuick Check:
Correct i18n syntax with description =Welcome to our site!
[OK]
Hint: Use i18n="@@description" inside the tag for descriptions [OK]
Common Mistakes:
- Placing description outside the tag
- Using i18n-desc instead of i18n
- Confusing description with translation key
3. Given the template code:
<h1 i18n="@@titleDesc">Hello, user!</h1>What will Angular do with this text during the build process?
medium
Solution
Step 1: Understand Angular's i18n extraction
Angular extracts text marked withi18nfor translation and uses the description (after @@) to help translators identify the text.Step 2: Analyze the given code
The text 'Hello, user!' will be extracted with the description 'titleDesc' for translation files. No syntax error occurs because @@ is valid for description keys.Final Answer:
Extract 'Hello, user!' for translation with the description 'titleDesc' -> Option CQuick Check:
i18n extracts text with description = Extract 'Hello, user!' for translation with the description 'titleDesc' [OK]
Hint: @@ means description key for translators in i18n [OK]
Common Mistakes:
- Thinking @@ causes syntax errors
- Assuming text is not extracted for translation
- Believing i18n attribute is ignored at build
4. Identify the error in this Angular template snippet for translation:
<button i18n="Submit button">Submit</button>
medium
Solution
Step 1: Check the i18n attribute format
The description or meaning for translation keys must be prefixed with @@ inside the i18n attribute, e.g.,i18n="@@Submit button".Step 2: Validate other options
i18n can be used on any element, includingbutton. Text does not require curly braces for static content. The attributei18n-descis not valid.Final Answer:
The description should be inside @@, like i18n="@@Submit button" -> Option BQuick Check:
Description needs @@ prefix in i18n attribute [OK]
Hint: Descriptions in i18n need @@ prefix inside quotes [OK]
Common Mistakes:
- Omitting @@ prefix for descriptions
- Thinking i18n only works on <span> elements
- Using i18n-desc instead of i18n
5. You want to mark multiple pieces of text in an Angular template for translation, each with a unique description. Which approach correctly applies this?
hard
Solution
Step 1: Use unique descriptions with @@ in i18n attribute
Angular uses the syntaxi18n="@@description"to add unique descriptions for each translatable text.Step 2: Evaluate each option
Welcome
This is the intro paragraph.
correctly usesi18n="@@headerDesc"andi18n="@@introDesc".Welcome
This is the intro paragraph.
uses invalidi18n-desc.Welcome
This is the intro paragraph.
misses @@ prefix.Welcome
This is the intro paragraph.
uses plain text instead of description keys.Final Answer:
<h2 i18n="@@headerDesc">Welcome</h2> <p i18n="@@introDesc">This is the intro paragraph.</p> -> Option AQuick Check:
Use i18n="@@uniqueDescription" for each text [OK]
Hint: Use @@ prefix in i18n for unique translation descriptions [OK]
Common Mistakes:
- Using i18n-desc instead of i18n
- Omitting @@ prefix for descriptions
- Putting descriptions as plain text in i18n attribute
