Internationalization (i18n) and localization (l10n) in Angular.

Venu Vudugula
3 min readSep 30, 2023

--

In Angular, internationalization (i18n) and localization (l10n) are essential considerations for building applications that can be used in different languages and regions. Angular provides tools and mechanisms to facilitate internationalization and localization within your application.

1. Internationalization (i18n) in Angular:

Internationalization in Angular involves preparing your application for multiple languages. Angular uses a tool called ngI18n to mark strings for translation and generate translation files.

Here are the steps to implement i18n in Angular:

a. Marking Strings for Translation:

To mark strings in your Angular application for translation, you can use the i18n attribute in your HTML templates. For example:

<p i18n="@@welcomeMessage">Welcome to our app!</p>

The @@welcomeMessage is a unique identifier for this string.

b. Extracting Messages:

After marking your strings, you need to extract these messages into a translation file using the Angular CLI:

ng xi18n --output-path src/locale

This command generates a messages.xlf file in the specified output directory.

c. Translating Messages:

Translators can then translate the messages in the messages.xlf file into different languages. They can use translation tools like POEdit or specialized translation services.

d. Using Translations:

In your Angular application, you can use Angular TranslateService to load and use translations. Here's a simplified example of how you might use it:

import { TranslateService } from '@ngx-translate/core';

// ...

constructor(private translateService: TranslateService) {
// Load translations from a JSON file or another source.
this.translateService.use('en'); // Set the default language.
}

// Use the translation in your component or template.
getMessage() {
return this.translateService.instant('welcomeMessage');
}

2. Localization (l10n) in Angular:

Localization in Angular involves adapting your application for specific regions or locales, including formatting dates, numbers, and other region-specific content. Angular provides mechanisms for this as well.

a. Date and Number Formatting:

Angular’s DatePipe and DecimalPipe allow you to format dates and numbers based on the user's locale. For example:

<p>{{ firstName| date }}</p>
<p>{{ accountNumber| number }}</p>

Angular will automatically use the appropriate format for the user’s locale.

b. Currency Formatting:

Angular’s CurrencyPipe can format currency values based on the user's locale:

<p>{{ priceValue | currency: 'USD' }}</p>

c. Locale Settings:

You can set the default locale for your application using the LOCALE_ID provider:

import { LOCALE_ID } from '@angular/core';

@NgModule({
providers: [{ provide: LOCALE_ID, useValue: 'en-US' }],
// ...
})

his sets the default locale to ‘en-US’, but you can change it based on user preferences.

d. Custom Locale Data:

You can also use the registerLocaleData function to load custom locale data:

import { registerLocaleData } from '@angular/common';
import localeFr from '@angular/common/locales/fr';

registerLocaleData(localeFr);

e. Angular’s Pipes:

Angular’s built-in pipes, like DatePipe, automatically adapt to the user's locale settings when formatting dates, numbers, and currencies.

3. Language Switching:

To allow users to switch languages or locales in your Angular application, you can create a language selector component or menu that triggers the change in the active locale and then reloads translations and locale-specific settings accordingly.

Thanks for reading, do clap and follow for more interesting topics in Angular.

--

--

Venu Vudugula
Venu Vudugula

Written by Venu Vudugula

I'm Venu, a seasoned tech professional with over 9 years of experience in full-stack development, eager to take on exciting challenges 😊.

No responses yet