Angular Internationalization (i18n)
What is Internationalization (i18n) in Angular?
internationalization (i18n) support to Angular applications involves making your app available in multiple languages.
Angular provides built-in features for i18n and localization.
Adding Internationalization Support to Angular Applications
- Enable i18n: In your Angular project's configuration file (angular.json), enable i18n support by setting the "i18n" property to true.
"i18n": true
- Extract Translatable Text: Use Angular's built-in i18n attribute in HTML templates to mark translatable text. For example:
<p i18n>Welcome to my app!</p>
- Generate Translation Files: Use the Angular CLI to extract translatable text and generate translation files for different languages.
ng xi18n --output-path src/locale
- Translate Text: Convert this translatable text (example: messages.xlf) to intended language (e.g., English; Spanish; Chinese etc.) in each translation file.
- Configure Localization: Updating angular.json file, which is a configuration file of the project using angular, to set translations files for different languages.
"localize": ["en", "fr", "de"]
- Build and Serve: Build and serve your Angular application, and it will automatically display the appropriate language based on the user's browser settings.
ng serve --configuration=de
Using Angular's Built-in i18n Features for Localization
- Translation Pipes: Angular supplies out-of-the-box pipes for you illustrate the translated text within your tags. Utilize the | translate }{{value}} pipe to render translated text.
<p>{{ 'Welcome to my app!' | translate }}</p>
- Localizing Date, Number, and Currency Formats: The on-board pipe layout allows for Angular localization with date, number, and currency formats. {{ value | date }], {{value | currency}} and {{ value | number}} pipelines should be used with appropriate locale settings
<p>{{ today | date }}</p>
<p>{{ price | currency }}</p>
- Language Switching: In your Angular app you can add multi-lanuage switching feature to make it possible to switch languages instantly. This can be done by implementing language translation mechanism and changing the application's locale settings to the specified language.