Implement i18n and l10n in AngularJS Apps
Q: How do you implement internationalization (i18n) and localization (l10n) in an AngularJS application?
- AngularJS
- Senior level question
Explore all the latest AngularJS interview questions and answers
ExploreMost Recent & up-to date
100% Actual interview focused
Create AngularJS interview for FREE!
AngularJS provides built-in support for internationalization
(i18n) and localization (l10n) through the @angular/localize package.
This package provides a set of services and directives that make it easy to
translate your application into different languages and regions.
Here are the steps to implement internationalization and
localization in an AngularJS application:
- Install
the @angular/localize package using npm or add it to your project
manually.
- Set up
your application for i18n by running the ng add @angular/localize
command. This command will update your application's configuration and
create the necessary files and folders for i18n.
- Extract
the translatable strings from your application by running the ng
extract-i18n command. This command will generate a messages.xlf
file that contains all the translatable strings in your application.
- Translate
the translatable strings in the messages.xlf file into your desired
languages.
- Build
your application with the --prod flag and the --localize
flag to generate a production-ready build with the translated strings
included.
- Use
the translate directive to translate the translatable strings in
your templates, like this:
<div>{{ 'Hello, world!' | translate }}</div>
In this example, we use the translate directive to
translate the string Hello, world! into the user's preferred language.
- Use
the translate service to translate the translatable strings in your
code, like this:
import { TranslateService } from '@ngx-translate/core'; constructor(private translate: TranslateService) {} ngOnInit() { this.translate.get('HELLO').subscribe((res: string) => { console.log(res); }); }
In this example, we inject the TranslateService into
our component and use the get method to translate the string with the
key HELLO into the user's preferred language.


