How To Translate Angular 8 Application Using ngx-translate

Introduction

 
NGX-Translate is an internationalization library for Angular. Internationalization is the process of translating our application into multiple languages. Using this library, we can translate our application language into multiple languages. Not only static data as well as dynamic data.
 

Prerequisites

  • Basic knowledge of Angular
  • Node and NPM installed
  • Visual Studio Code 
If new to Angular then you can follow my blog for initial setup of the Angular app.Click.
 
Angular is a TypeScript-based open source web application framework developed by Google. Angular is a platform for building mobile, desktop and web applications.
 

TypeScript

 
TypeScript is an open-source programming language developed and maintained by Microsoft. TypeScript is a superset of JavaScript that compiles to plain JavaScript.
 
Development Environment Required Software,
  1. Node
    Node.js is an open-source, cross-platform JavaScript run-time environment that executes JavaScript code outside of a browser.
    Download Link - Click

  2. NPM
    Install when we install node js

  3. Angular CLI
    Angular CLI is a command-line interface tool that we use to initialize, develop and maintain Angular applications.
    Download by Command - “npm install -g @angular/cli”

  4. Visual studio code - Optional
    Download Link:- Click
Step 1

Create a new Angular project by using the following command.first we need to set a path for creating a new angular app.
 
ng new MultilanguageApp
 

How To Translate Angular 8 Application Using ngx-translate

 
Now open this project in visual studio code. To open in visual studio code:
 
Choose the project path and run below command.
 
code .
 
Step 2
 
Now install the ngx-translate library by using the following command.and also we can install bootstrap and jquery for design.
  1. npm install @ngx-translate/core --save  
  2. npm install @ngx-translate/http-loader --save  
  3. npm install bootstrap@4 jquery –save  
Example - How to run the command.

How To Translate Angular 8 Application Using ngx-translate

After installing the bootstrap package, we have to add reference of CSS file on styles.css file.

So open styles.css and add the following line.
  1. @import "~bootstrap/dist/css/bootstrap.css";   
Step 3

Import the necessary modules into app.module.ts.
 

How To Translate Angular 8 Application Using ngx-translate

 
Step 4
 
Now, expand the src folder and right-click on the Assets folder. Add a new folder under it and rename that to 'i18' and add JSON files to this folder. Based on the requirement of how many languages you want to translate. I'm showing an example of two language English and French so I'm creating two files.
  1. en.json
  2. fr.json
The JSON file is a combination of a key-value pair.

Ex 1,

How To Translate Angular 8 Application Using ngx-translate
 
Ex 2,
 

How To Translate Angular 8 Application Using ngx-translate

 
Step 5

Open the en.json file and paste the following code.
  1. {  
  2.     "Addemployee""Add-employee",  
  3.     "Name""Name",  
  4.     "Email""Email",  
  5.     "PhoneNo""Phone No",  
  6.     "Submit""Submit",  
  7.     "Cancel""Cancel",  
  8.     "Home""Home",  
  9.     "Employee""Employee",  
  10.     "EmployeeList""Employee List"  
  11. }  
Step 6

Open fr.json file and paste the following code.
  1. {  
  2.     "Addemployee""Ajouter employé",  
  3.     "Name""prénom",  
  4.     "Email""Email",  
  5.     "PhoneNo""Pas de téléphone",  
  6.     "Submit""Soumettre",  
  7.     "Cancel""Annuler",  
  8.     "Home""Accueil",  
  9.     "Employee""Employée",  
  10.     "EmployeeList""Liste des employés"  
  11. }  
Step 7

Open app.component.html file and paste the following code. 
  1. <!--The content below is only a placeholder and can be replaced.-->  
  2.   
  3. <div class="bg-dark" style="text-align:center;color: #fff">  
  4.    
  5.   <h2 class="navbar-brand ">  
  6.     Welcome to {{ title }}!  
  7.   </h2>  
  8.   <select #langSelect (change)="changeLang(langSelect.value)">  
  9.       <option *ngFor="let lang of translate.getLangs()" [value]="lang" [selected]="lang === translate.currentLang">{{ lang }}</option>  
  10.   </select>  
  11. </div>  
  12. <div class="row">  
  13.   <div class="col-md-2">  
  14.     <ul class="list-group">  
  15.       <li class="list-group-item"><a [routerLink]="['/']">{{ 'Home' | translate }}</a></li>  
  16.       <li class="list-group-item"><a [routerLink]="['/employee']">{{ 'Employee' | translate }}</a></li>  
  17.       <li class="list-group-item"><a [routerLink]="['/employeelist']">{{ 'EmployeeList' | translate }}</a></li>  
  18.       <!-- <li class="list-group-item"><a href="employeelist">EmployeeList(Reload)</a></li> -->  
  19.     </ul>  
  20.   </div>  
  21.   <div class="col-md-8">  
  22.     <router-outlet></router-outlet>  
  23.   </div>  
  24. </div>  
Ex.

How To Translate Angular 8 Application Using ngx-translate
 
Step 8

Open the app.component.ts file and paste the following code.
  1. import { Component } from '@angular/core';  
  2. import { TranslateService } from '@ngx-translate/core';  
  3.   
  4. @Component({  
  5.   selector: 'app-root',  
  6.   templateUrl: './app.component.html',  
  7.   styleUrls: ['./app.component.css']  
  8. })  
  9. export class AppComponent {  
  10.   title = 'MultilanguageApp';  
  11.   
  12.   constructor(  
  13.     public translate: TranslateService) {  
  14.     translate.addLangs(['en''fr']);  
  15.     if (localStorage.getItem('locale')) {  
  16.       const browserLang = localStorage.getItem('locale');  
  17.       translate.use(browserLang.match(/en|fr/) ? browserLang : 'en');  
  18.     } else {  
  19.       localStorage.setItem('locale''en');  
  20.       translate.setDefaultLang('en');  
  21.     }  
  22.   }  
  23.   changeLang(language: string) {  
  24.     localStorage.setItem('locale', language);  
  25.     this.translate.use(language);  
  26.   }  
  27. }  
Ex.

How To Translate Angular 8 Application Using ngx-translate
 
Step 9

Run this command for hosting the application with default port 4200.

How To Translate Angular 8 Application Using ngx-translate
 
Output Window

How To Translate Angular 8 Application Using ngx-translate
 

Summary

 
In this write-up, we discussed ngx-translate with a demo example.
 
NGX-Translate is an internationalization library for Angular. Internationalization is the process of translating our application into multiple languages. In my next article, I will discuss an Angular i18n language translator and Paypal subscription payment integration using angular 8 Any feedback related to this article is most welcome.