Internationalization (i18n) is the process of designing and preparing your Angular application to be adaptable to different languages and regions without requiring engineering changes to the source code. This is crucial for applications that aim to reach a global audience.
Key Concepts
- Localization (l10n): Adapting your application to a specific locale, including translating text, formatting dates and numbers, and handling other locale-specific data.
- Locale: A set of parameters that defines the user's language, country, and any special variant preferences.
- Translation Files: Files that contain the translated text for different locales.
Steps to Implement Internationalization in Angular
- Setting Up Angular i18n
Angular provides built-in support for internationalization through the @angular/localize
package. To set it up:
-
Install the package:
ng add @angular/localize
-
Mark Text for Translation: Use the
i18n
attribute to mark text for translation in your templates.<h1 i18n="@@welcomeMessage">Welcome to our application!</h1>
- Extracting Translation Files
-
Extract Messages: Use Angular CLI to extract the messages into an XLIFF file.
ng extract-i18n
This command generates a
messages.xlf
file in thesrc/locale
directory. -
Translate Messages: Create copies of the
messages.xlf
file for each locale and translate the text.<trans-unit id="welcomeMessage" datatype="html"> <source>Welcome to our application!</source> <target>Bienvenido a nuestra aplicación!</target> </trans-unit>
- Configuring the Application for Multiple Locales
-
Update
angular.json
: Add configurations for each locale in theangular.json
file."projects": { "your-app": { "i18n": { "sourceLocale": "en", "locales": { "es": "src/locale/messages.es.xlf" } } } }
-
Build for Different Locales: Build the application for each locale.
ng build --localize
- Serving the Application
- Serve the Application:
Use a web server to serve the localized versions of your application. Each locale will have its own directory under the
dist
folder.
- Dynamic Locale Switching
To dynamically switch locales in your application, you can use Angular's LOCALE_ID
and registerLocaleData
functions.
-
Import Locale Data:
import { registerLocaleData } from '@angular/common'; import localeEs from '@angular/common/locales/es'; registerLocaleData(localeEs);
-
Provide Locale ID:
import { LOCALE_ID } from '@angular/core'; @NgModule({ providers: [ { provide: LOCALE_ID, useValue: 'es' } ] }) export class AppModule { }
Practical Example
Example Component
<!-- app.component.html --> <h1 i18n="@@welcomeMessage">Welcome to our application!</h1> <button (click)="switchLocale('es')">Switch to Spanish</button> <button (click)="switchLocale('en')">Switch to English</button>
Example Component Logic
// app.component.ts import { Component } from '@angular/core'; import { LOCALE_ID, Inject } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { constructor(@Inject(LOCALE_ID) private localeId: string) {} switchLocale(locale: string) { // Logic to switch locale // This might involve reloading the application with the new locale window.location.href = `/${locale}/`; } }
Exercises
Exercise 1: Mark Text for Translation
- Mark the following text for translation in your Angular template:
<p>Thank you for using our service!</p>
Solution:
Exercise 2: Extract and Translate Messages
- Extract the messages and translate the
thankYouMessage
to Spanish.
Solution:
- Extract messages:
ng extract-i18n
- Translate in
messages.es.xlf
:<trans-unit id="thankYouMessage" datatype="html"> <source>Thank you for using our service!</source> <target>¡Gracias por usar nuestro servicio!</target> </trans-unit>
Exercise 3: Configure and Build for Locales
- Add the Spanish locale to
angular.json
and build the application.
Solution:
- Update
angular.json
:"projects": { "your-app": { "i18n": { "sourceLocale": "en", "locales": { "es": "src/locale/messages.es.xlf" } } } }
- Build the application:
ng build --localize
Conclusion
Internationalization is a powerful feature in Angular that allows you to create applications that can be easily adapted to different languages and regions. By following the steps outlined in this section, you can ensure that your application is ready to reach a global audience. In the next topic, we will explore Angular Animations and how to add dynamic effects to your application.
Angular Course
Module 1: Introduction to Angular
- What is Angular?
- Setting Up the Development Environment
- Angular Architecture
- First Angular Application
Module 2: Angular Components
- Understanding Components
- Creating Components
- Component Templates
- Component Styles
- Component Interaction
Module 3: Data Binding and Directives
- Interpolation and Property Binding
- Event Binding
- Two-Way Data Binding
- Built-in Directives
- Custom Directives
Module 4: Services and Dependency Injection
Module 5: Routing and Navigation
Module 6: Forms in Angular
Module 7: HTTP Client and Observables
- Introduction to HTTP Client
- Making HTTP Requests
- Handling HTTP Responses
- Using Observables
- Error Handling
Module 8: State Management
- Introduction to State Management
- Using Services for State Management
- NgRx Store
- NgRx Effects
- NgRx Entity
Module 9: Testing in Angular
Module 10: Advanced Angular Concepts
- Angular Universal
- Performance Optimization
- Internationalization (i18n)
- Custom Pipes
- Angular Animations