Internationalization (i18n) is the process of designing and preparing your application to be adaptable to various languages and regions without requiring engineering changes to the source code. Angular provides robust support for internationalization, making it easier to create applications that can be translated into multiple languages.
Key Concepts
- Localization (l10n): The process of translating your internationalized application into specific languages and adapting it to specific regions.
- Translation Files: Files that contain the translated text for different languages.
- Angular i18n Tools: Angular provides built-in tools and libraries to facilitate internationalization and localization.
Steps to Implement Internationalization in Angular
- Preparing the Application
First, ensure your application is ready for internationalization by marking the text that needs to be translated.
Example: Marking Text for Translation
In this example, the i18n
attribute is used to mark the text "Welcome to our application!" for translation. The @@welcomeMessage
is an optional custom ID for the translation unit.
- Extracting Translation Messages
Use Angular CLI to extract the marked text into a translation file.
This command generates a messages.xlf
file in the specified output path. This file contains all the marked text in your application.
- Translating Messages
Translate the extracted messages into the desired languages. For example, create a messages.fr.xlf
file for French translations.
Example: French Translation File
<!-- src/locale/messages.fr.xlf --> <xliff version="1.2"> <file source-language="en" target-language="fr" datatype="plaintext"> <body> <trans-unit id="welcomeMessage" datatype="html"> <source>Welcome to our application!</source> <target>Bienvenue dans notre application!</target> </trans-unit> </body> </file> </xliff>
- Configuring the Angular Application
Configure Angular to use the translation files. This involves modifying the angular.json
file and setting up the build configurations.
Example: angular.json
Configuration
{ "projects": { "your-app-name": { "i18n": { "sourceLocale": "en", "locales": { "fr": "src/locale/messages.fr.xlf" } }, "architect": { "build": { "configurations": { "fr": { "localize": ["fr"] } } }, "serve": { "configurations": { "fr": { "browserTarget": "your-app-name:build:fr" } } } } } } }
- Building and Serving the Application
Build and serve the application for the specific locale.
This will build and serve the application with the French translations.
Practical Exercise
Exercise: Add Spanish (es) Localization
- Mark Text for Translation: Add the
i18n
attribute to some text in your application. - Extract Messages: Use the Angular CLI to extract the messages.
- Translate Messages: Create a
messages.es.xlf
file and add Spanish translations. - Configure Angular: Update the
angular.json
file to include the Spanish locale. - Build and Serve: Build and serve the application with the Spanish locale.
Solution
- Mark Text for Translation:
- Extract Messages:
- Translate Messages:
<!-- src/locale/messages.es.xlf --> <xliff version="1.2"> <file source-language="en" target-language="es" datatype="plaintext"> <body> <trans-unit id="greetingMessage" datatype="html"> <source>Hello, how are you?</source> <target>Hola, ¿cómo estás?</target> </trans-unit> </body> </file> </xliff>
- Configure Angular:
{ "projects": { "your-app-name": { "i18n": { "sourceLocale": "en", "locales": { "es": "src/locale/messages.es.xlf" } }, "architect": { "build": { "configurations": { "es": { "localize": ["es"] } } }, "serve": { "configurations": { "es": { "browserTarget": "your-app-name:build:es" } } } } } } }
- Build and Serve:
Conclusion
Internationalization is a crucial aspect of modern web applications, allowing them to reach a global audience. By following the steps outlined in this section, you can prepare your Angular application for multiple languages and regions, ensuring a broader reach and better user experience. In the next module, we will explore performance optimization techniques to make your Angular applications faster and more efficient.
Angular 2+ Course
Module 1: Introduction to Angular
Module 2: TypeScript Basics
- Introduction to TypeScript
- TypeScript Variables and Data Types
- Functions and Arrow Functions
- Classes and Interfaces