NashTech Insights

Angular Localization Made Easy with Transloco

Alka Vats
Alka Vats
Table of Contents

Introduction:

Localization is a critical aspect of building globalized applications that cater to diverse audiences. In this blog post, we will explore how to implement localization in Angular using Transloco, a powerful localization library. We’ll cover the basics of Transloco, demonstrate its usage with practical examples, and discuss its key features for seamless internationalization in Angular applications.

If you want to learn about a new feature of angular, you can refer here.

Introduction to Transloco:

  1. What is Transloco?
    Transloco is a localization library for Angular that provides a comprehensive set of features to handle multi-language support. It offers a simple yet flexible API for managing translations, handling pluralization, and organizing language files.
  2. Key Features of Transloco:
  • Dynamic translation loading: Transloco enables lazy-loading translations for improved performance.
  • Pluralization and interpolation: It supports plural forms and variable interpolation in translations.
  • Locale fallbacks: Transloco supports fallback languages when a translation is missing.
  • Language switcher: It provides tools to dynamically switch between different languages at runtime.
  • Angular integration: Transloco seamlessly integrates with Angular’s change detection and localization pipeline.

Setting Up Transloco:

  1. Installation and Configuration:
    Start by installing the Transloco package using npm or yarn:
npm install @ngneat/transloco

Configure Transloco in the AppModule, specifying the available languages and default locale:

import { TranslocoModule } from '@ngneat/transloco';

@NgModule({
  imports: [
    TranslocoModule.forRoot({
      availableLangs: ['en', 'fr'],
      defaultLang: 'en',
      fallbackLang: 'en',
      prodMode: environment.production,
    }),
  ],
})
export class AppModule {}
  1. Creating Language Files:
    Create language files for each supported language, following the Transloco file structure. For example, create en.json and fr.json files in the assets folder:
// en.json
{
  "greeting": "Hello!",
  "message": "Welcome to our application."
}
// fr.json
{
  "greeting": "Bonjour!",
  "message": "Bienvenue dans notre application."
}

Using Transloco in Angular Components:

  1. Translation Pipe: Utilize the transloco pipe to translate text within HTML templates. For example:
<p>{{ 'greeting' | transloco }}</p>
  1. Translation Service: Inject the TranslocoService into Angular components to perform translations programmatically. For example:
import { TranslocoService } from '@ngneat/transloco';

@Component({...})
export class MyComponent {
  constructor(private translocoService: TranslocoService) {}

  translateMessage() {
    const translation = this.translocoService.translate('message');
    // Use the translation as needed
  }
}

Pluralization and Interpolation:

  1. Pluralization: Implement pluralization rules using Transloco’s translocoPlural directive. For example:
<p [translocoPlural]="count" translocoPluralMapping="{ '=0': 'No items', '=1': '1 item', 'other': '# items' }"></p>
  1. Interpolation: Interpolate dynamic values into translations using the transloco pipe. For example:
<p>{{ 'welcome' | transloco: { name: 'John' } }}</p>

Dynamic Language Switching:

  1. Language Selection UI: Create a language selector component to display the available languages. For example:
<button (click)="setLanguage('en')">English</button>
<button (click)="setLanguage('fr')">French</button>
  1. Handling Locale Changes: Listen for locale changes using the TranslocoService and update the translations accordingly. For example:
import { TranslocoService } from '@ngneat/transloco';

@Component({...})
export class MyComponent {
  constructor(private translocoService: TranslocoService) {}

  setLanguage(language: string) {
    this.translocoService.setActiveLang(language);
  }
}

Conclusion:

Transloco simplifies the process of implementing localization in Angular applications. By leveraging Transloco’s dynamic translation loading, pluralization, interpolation, and language-switching capabilities, you can provide a seamless multi-language experience for your users. With its integration with Angular’s change detection and localization pipeline, Transloco is a powerful tool for achieving internationalization in Angular applications. Explore the documentation and experiment with Transloco to unlock the full potential of localization in your Angular projects.

Finally, for more such posts, please follow our LinkedIn page- FrontEnd Competency.

Alka Vats

Alka Vats

Alka Vats is a Software Consultant at Nashtech. She is passionate about web development. She is recognized as a good team player, a dedicated and responsible professional, and a technology enthusiast. She is a quick learner & curious to learn new technologies. Her hobbies include reading books, watching movies, and traveling.

Leave a Comment

Your email address will not be published. Required fields are marked *

Suggested Article

%d bloggers like this: