Making Your Angular App Speak Multiple Languages: A Friendly Guide to i18n šŸŒ

Paul Chesa
5 min readDec 30, 2024

--

Imagine youā€™re hosting a dinner party šŸ½ļø, and youā€™ve got friends coming over from different countries. You want everyone to feel at home and understand whatā€™s happening. Thatā€™s exactly what internationalization (i18n) does for your Angular application ā€” it makes your app feel like home for users from any part of the world!

What is Angular i18n? šŸ¤”

Think of i18n as your appā€™s personal translator. Just like how a translator helps people understand different languages, i18n helps your Angular app ā€œspeakā€ multiple languages. The term ā€œi18nā€ might look weird, but thereā€™s a fun story behind it ā€” itā€™s just ā€œinternationalizationā€ shortened by keeping the first ā€˜iā€™, the last ā€™nā€™, and counting the 18 letters in between!

Why Should You Care? šŸ’”

Letā€™s say youā€™ve built an amazing recipe-sharing app. Your Italian grandmother loves using it, but she struggles with English. Wouldnā€™t it be wonderful if she could use the app in Italian? Thatā€™s where i18n comes in! It helps you create an app that can switch between languages as easily as changing TV channels.

How Does it Work? šŸ› ļø

1. Marking Text for Translation

First, you need to tell Angular which text needs to be translated. Itā€™s like putting sticky notes šŸ“ on items in your house to label them. In Angular, we use a special marker called i18n:

<h1 i18n>Welcome to my app!</h1>

2. Creating Translation Files

Next, you create separate files for each language, like having different instruction manuals for the same product. These files are like dictionaries šŸ“š that tell Angular how to say each phrase in different languages:

{
"welcome": {
"en": "Welcome to my app!",
"es": "Ā”Bienvenido a mi aplicaciĆ³n!",
"fr": "Bienvenue dans mon application !"
}
}

3. Setting Up Your App to Use Translations

Finally, you configure your app to use these translations. Itā€™s like setting up your phone to display everything in your preferred language. Angular will automatically swap in the right translations based on the userā€™s language settings.

Best Practices šŸŒŸ

  1. Plan Ahead: Just like youā€™d plan seating arrangements for a dinner party, plan which parts of your app need translation from the start.
  2. Context is Key: Give your translators context about where and how the text is used. ā€œSubmitā€ could be translated differently depending on whether itā€™s for a form or a game!
  3. Handle Plurals: Different languages handle plurals differently. For example, English has two forms (1 apple, 2 apples), but Arabic has six different forms! Angular i18n helps you handle these cases gracefully.
  4. Remember Cultural Differences: Itā€™s not just about translating words. Dates, numbers, and currencies should also be formatted according to local conventions. December 1st might be 1/12 in some countries and 12/1 in others!

Common pitfalls and How to Avoid Them šŸšØ

  1. Hard-coded Text: Donā€™t forget about things like error messages and tooltips. They need translation too!
  2. Dynamic Content: When working with variables, remember different languages might order words differently. Use Angularā€™s translation interpolation to handle this:
<p i18n>Hello, {{name}}!</p>
  1. Image Alt Text: Donā€™t forget to translate alternative text for images. Accessibility is important in every language! šŸŒ

Getting Started šŸš€

Ready to make your app multilingual? Hereā€™s a simple checklist:

  1. Install the necessary Angular i18n packages
  2. Mark your text for translation
  3. Generate your translation files
  4. Configure your build process
  5. Test with different languages

Letā€™s walk through making your Angular app multilingual step by step! Think of this as preparing your app for its international debut. šŸŽ¬

1. Install the Required Packages šŸ“¦

First, youā€™ll need to install the necessary packages. Open your terminal and run:

# For Angular 15 and above
ng add @angular/localize

# If you're using ngx-translate (an alternative approach)
npm install @ngx-translate/core @ngx-translate/http-loader

2. Mark Your Text for Translation šŸ“

Now comes the fun part ā€” marking text for translation! There are several ways to do this:

In Templates:

<!-- Basic translation -->
<h1 i18n>Welcome to our app!</h1>

<!-- With description and meaning for translators -->
<h1 i18n="site header|The main welcome message">Welcome to our app!</h1>

<!-- With variables -->
<p i18n>Hello, {{username}}! You have {{messageCount}} new messages.</p>

<!-- With pluralization -->
<p i18n>Updated {minutes, plural,
=0 {just now}
=1 {one minute ago}
other {{{minutes}} minutes ago}
}</p>

In TypeScript (using ngx-translate):

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

export class AppComponent {
constructor(private translate: TranslateService) {
// Set default language
translate.setDefaultLang('en');
// Use a specific language
translate.use('es');
}
}

3. Generate Translation Files šŸ“š

Run the extraction command to create your translation files:

ng extract-i18n --output-path src/locale

This will generate a messages.xlf file. You'll need to create copies of this file for each language. For example:

<!-- messages.es.xlf -->
<?xml version="1.0" encoding="UTF-8" ?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" target-language="es" datatype="plaintext">
<body>
<trans-unit id="welcomeMessage">
<source>Welcome to our app!</source>
<target>Ā”Bienvenido a nuestra aplicaciĆ³n!</target>
</trans-unit>
</body>
</file>
</xliff>

4. Configure Your Build Process āš™ļø

Update your angular.json to include configuration for each language:

{
"projects": {
"your-app": {
"architect": {
"build": {
"configurations": {
"production": { ... },
"es": {
"localize": ["es"],
"outputPath": "dist/your-app-es/",
"i18nFile": "src/locale/messages.es.xlf",
"i18nFormat": "xlf",
"i18nLocale": "es"
},
"fr": {
"localize": ["fr"],
"outputPath": "dist/your-app-fr/",
"i18nFile": "src/locale/messages.fr.xlf",
"i18nFormat": "xlf",
"i18nLocale": "fr"
}
}
}
}
}
}
}

5. Test Different Languages šŸ”

Now you can serve your app in different languages:

# Serve in Spanish
ng serve --configuration=es

# Serve in French
ng serve --configuration=fr

# Build for production in all languages
ng build --localize

6. Add Language Switcher (Optional) šŸ”„

Add a language switcher to your app to let users change languages on the fly:

// app.component.ts
export class AppComponent {
languages = ['en', 'es', 'fr'];
currentLang = 'en';

constructor(private translate: TranslateService) {
translate.setDefaultLang('en');
}

switchLanguage(lang: string) {
this.currentLang = lang;
this.translate.use(lang);
}
}
<!-- app.component.html -->
<div class="language-switcher">
<select [(ngModel)]="currentLang" (change)="switchLanguage(currentLang)">
<option *ngFor="let lang of languages" [value]="lang">
{{lang | uppercase}}
</option>
</select>
</div>

7. Testing Tips šŸ§Ŗ

  • Test your app with different language settings in your browser
  • Check if date formats, numbers, and currencies are correctly localized
  • Verify that dynamic content (like form validation messages) is translated
  • Test text expansion (some languages might need more space)
  • Check RTL (Right-to-Left) support for languages like Arabic

Pro Tips šŸ’”

  • Keep your translations organized in smaller files by feature/module
  • Use translation management systems (TMS) for larger projects
  • Consider automated translation services for initial drafts
  • Document special instructions for translators
  • Use translation memory tools to maintain consistency

Conclusion šŸŽ‰

Making your Angular app speak multiple languages might seem daunting at first, but itā€™s really just like being a good host ā€” making sure everyone feels welcome and understood. With Angularā€™s i18n tools, you can create an app that feels native to users around the world.

Remember, the web is for everyone, and by internationalizing your app, youā€™re making it more inclusive and accessible to people from different cultures and backgrounds. Thatā€™s something worth celebrating! šŸŽˆ

Happy coding, and may your app speak many languages fluently! šŸŒšŸŒšŸŒŽ

--

--

Paul Chesa
Paul Chesa

Written by Paul Chesa

Angular developer | Frontend Developer | Proficient in Typescript and JavaScript | Parva res est

No responses yet