DEV Community

Cover image for Simplify Language Translation in Angular with ngx-translate
Mo'men Ali
Mo'men Ali

Posted on

Simplify Language Translation in Angular with ngx-translate

Introduction

Language translation is a crucial aspect of creating multilingual Angular applications. With the help of ngx-translate, a powerful translation library, we can easily implement language localization and provide a seamless experience to users in different locales. In this article, we will explore the key features and usage of ngx-translate to simplify language translation in your Angular app.

What is ngx-translate?

ngx-translate is a widely-used library in the Angular ecosystem that provides translation support for Angular applications. It allows you to define and manage translations for different languages, switch between languages dynamically, and seamlessly update the UI based on the selected language.

Installation and Setup:

To get started with ngx-translate, follow these steps:

  • Install the necessary dependencies:
npm install @ngx-translate/core @ngx-translate/http-loader
Enter fullscreen mode Exit fullscreen mode
  • Configure the translation module in your app:
import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
import { HttpClient, HttpClientModule } from '@angular/common/http';

// Function to load translations from a JSON file
export function HttpLoaderFactory(http: HttpClient) {
  return new TranslateHttpLoader(http, './assets/i18n/', '.json');
}

@NgModule({
  imports: [
    // ... other imports
    HttpClientModule,
    TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: HttpLoaderFactory,
        deps: [HttpClient]
      }
    })
  ],
  // ... other module configurations
})
export class AppModule { }

Enter fullscreen mode Exit fullscreen mode

Usage:

Once ngx-translate is set up in your Angular app, you can start using it to translate your application's content. Here's a step-by-step guide:

  • Create translation files:

Create translation files:
In the assets/i18n directory, create translation files for each language you want to support. For example:
English (en.json):

{
    "HOME": {
      "EDIT": "Start editing to see some magic happen.",
      "SELECT": "Change language"
    },
    "HELLO": "Hello"
}

Enter fullscreen mode Exit fullscreen mode

French (fr.json):

{
    "HOME": {
        "EDIT": "Commencez l'édition pour voir une magie se produire.",
        "SELECT": "Changer la langue"
    },
    "HELLO": "Bonjour"
}

Enter fullscreen mode Exit fullscreen mode
  • Inject and use the TranslateService:

In your component or service, import the TranslateService and inject it via the constructor:

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

constructor(private translate: TranslateService) { }

Enter fullscreen mode Exit fullscreen mode
  • Load translations and set the default language: In the appropriate place (e.g., in your app initialization code), load the translations and set the default language:
this.translate.setDefaultLang('en'); // Set the default language
this.translate.use('en'); // Set the initial language

Enter fullscreen mode Exit fullscreen mode
  • Translate your content: To translate a string, use the translate pipe or the translate method of the TranslateService:

<!-- Using the translate pipe -->
<p>{{ 'HOME' | translate }}</p>

<!-- Using the translate method in component code -->
<p>{{ translate.instant('HOME.EDIT') }}</p>

Enter fullscreen mode Exit fullscreen mode

GitHub Example:

For a hands-on example of ngx-translate implementation, you can check out the following GitHub repository: ngx-translate-demo. This repository contains a sample Angular application where ngx-translate is used to demonstrate language translation and localization. You can explore the code, review the implementation, and run the application locally to see ngx-translate in action.

Conclusion:

ngx-translate provides a simple yet powerful solution for language translation in Angular applications. By leveraging its features, you can easily support multiple languages, switch between them dynamically, and provide a localized experience to your users. With the steps outlined in this article, you are now equipped to implement ngx-translate in your Angular app and take your internationalization efforts to the next level.

Remember to explore the ngx-translate documentation for advanced features like parameterized translations, pluralization, and more. Happy translating!

Top comments (0)