0
0
Angularframework~10 mins

Angular i18n built-in support - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Angular i18n built-in support
Write template with i18n tags
Run Angular compiler (ng extract-i18n)
Generate translation files (XLIFF)
Translate messages in files
Build app with locale and translations
App displays translated text based on locale
Angular i18n flow starts with marking text in templates, extracting messages, translating, then building the app with chosen locale to show translated content.
Execution Sample
Angular
<h1 i18n>Welcome to our app!</h1>
<p i18n="@@intro">This app helps you learn.</p>
This template marks text for translation using the i18n attribute.
Execution Table
StepActionInput/StateOutput/Result
1Template parsing<h1 i18n>Welcome to our app!</h1>Detects text 'Welcome to our app!' for translation
2Run ng extract-i18nTemplate filesGenerates messages.xlf with extracted texts
3Translate messages.xlfmessages.xlfTranslated messages file (e.g. messages.fr.xlf)
4Build app with locale 'fr'ng build --localizeApp bundles include French translations
5Run app in browserfr locale build servedDisplays 'Bienvenue dans notre application!' instead of English text
💡 App shows translated text based on the build locale
Variable Tracker
VariableStartAfter ExtractionAfter TranslationAfter BuildFinal
Template TextEnglish text in templateMarked for translationReplaced by translation keysBundles include translationsDisplayed translated text at runtime
Translation FilesNonemessages.xlf createdmessages.fr.xlf createdIncluded in buildUsed by app to show translations
Key Moments - 3 Insights
Why do we need to run 'ng extract-i18n' before translating?
Because 'ng extract-i18n' scans templates and creates a file with all texts to translate, as shown in execution_table step 2.
How does Angular know which language to show at runtime?
Angular uses the locale set during build to include the correct translations, explained in execution_table step 5.
What happens if a translation is missing for a locale?
Angular falls back to the original text in the template, so untranslated text appears in the default language.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is generated after running 'ng extract-i18n'?
Amessages.xlf file with extracted texts
BTranslated app bundles
CRuntime locale detection code
DTemplate with i18n attributes
💡 Hint
Check execution_table row 2 for the output of 'ng extract-i18n'
At which step does the app include translated messages in its bundles?
AStep 1
BStep 3
CStep 4
DStep 5
💡 Hint
Look at execution_table row 4 describing the build process
According to the execution_table, when the fr locale build is run in browser, what text will the app display?
AEnglish text from template
BFrench translated text
CNo text shown
DError message
💡 Hint
See execution_table row 5 for runtime display behavior
Concept Snapshot
Angular i18n built-in support:
- Mark text in templates with i18n attribute
- Run 'ng extract-i18n' to create translation files
- Translate messages in generated files
- Build app with --localize for target locales
- App shows translated text based on the target locale
Full Transcript
Angular i18n built-in support helps you translate your app's text. You start by marking text in your templates with the i18n attribute. Then you run the Angular command 'ng extract-i18n' which scans your templates and creates a file with all the texts to translate. You send this file to translators who create translated versions. When you build your app with the --localize option, Angular includes these translations in the app bundles. At runtime, the app shows the translated text for its target locale if available. If a translation is missing, it shows the original text. This process ensures your app can speak many languages smoothly.