WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Angular · Intermediate · question 29 of 100

How do you implement internationalization (i18n) in Angular applications?

📕 Buy this interview preparation book: 100 Angular questions & answers — PDF + EPUB for $5

Internationalization (i18n) is the process of designing and preparing your application to be usable in different languages. Angular provides built-in support for internationalization, which allows you to effectively design applications that can adapt to different languages and regions.

To implement i18n in Angular applications, you can follow these steps:

1. Install Angular’s localization package:

ng add @angular/localize

2. Mark text that needs to be translated:

Angular uses a custom attribute called ‘i18n‘ to mark text that needs to be translated. For example, in your template, you can add the ‘i18n‘ attribute to a text element:

<h1 i18n>Hello, World!</h1>

You can also add optional description and meaning to help the translators understand the context:

<h1 i18n="site header|An introduction header for the website">Hello, World!</h1>

3. Extract translation messages:

To extract the marked text and generate a translation source file in XLIFF or JSON format, you can use Angular CLI command ‘xi18n‘. The default format is XLIFF:

ng xi18n

To generate a file in JSON format, use the following:

ng xi18n --format=json

4. Translate the messages:

You will receive a messages file, either in XLIFF or JSON format, based on your chosen format. You need to create a copy of this file for each language you want to support and translate the messages accordingly. For example, if you have a ‘messages.xlf‘ file, create a ‘messages.fr.xlf‘ for French translations and update the content accordingly.

5. Configure the application to use translations:

To use the translated messages in your application, you need to create a new configuration for each language in the ‘angular.json‘ file. Add a new configuration under the ‘projects > your-project > architect > build > configurations‘ and ‘projects > your-project > architect > serve > configurations‘ sections.

For example, to add French support, you can add the following:

"configurations": {
  //...
  "fr": {
    "aot": true,
    "i18nFile": "src/locale/messages.fr.xlf",
    "i18nFormat": "xlf",
    "i18nLocale": "fr",
    "i18nMissingTranslation": "error",
    "outputPath": "dist/my-app-fr/"
  }
}

Update the ‘serve‘ section as well:

"configurations": {
  //...
  "fr": {
    "browserTarget": "your-project:build:fr"
  }
}

6. Serve or build the application for specific language:

You can now serve or build your application for a specific language using the appropriate configuration. For example, to serve the French version of your application, use:

ng serve --configuration=fr

To build the production version of your application in French, you can use:

ng build --configuration=production,fr

After completing these steps, your Angular application will support internationalization and be able to adapt to different languages based on the translations you provided.

**Note:** Keep in mind that you should also handle date, time, and currency formats according to the locale. Angular provides built-in pipes like ‘DatePipe‘, ‘CurrencyPipe‘, and ‘PercentPipe‘ that handle these formats based on the locale. Make sure to use them in your templates when dealing with dates, numbers or currency values.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Angular interview — then scores it.
📞 Practice Angular — free 15 min
📕 Buy this interview preparation book: 100 Angular questions & answers — PDF + EPUB for $5

All 100 Angular questions · All topics