DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • 7 Best IDEs for AngularJS App Development
  • Leveraging Salesforce Using a Client Written In Angular
  • Ionic App Development Over Other Frameworks: Is It Hyped?
  • AngularJS Vs. ReactJS Vs. VueJS: A Detailed Comparison

Trending

  • Building Resilient Networks: Limiting the Risk and Scope of Cyber Attacks
  • Unlocking Data with Language: Real-World Applications of Text-to-SQL Interfaces
  • The Human Side of Logs: What Unstructured Data Is Trying to Tell You
  • Build Your First AI Model in Python: A Beginner's Guide (1 of 3)
  1. DZone
  2. Coding
  3. Frameworks
  4. How To Translate Angular 8 Application Using ngx-translate

How To Translate Angular 8 Application Using ngx-translate

Translate an Angular 8 application using ngx-translate.

By 
Imtiyaz Ansari user avatar
Imtiyaz Ansari
·
Oct. 11, 19 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
34.5K Views

Join the DZone community and get the full member experience.

Join For Free

reading-glasses-looking-at-book


In this article, we will discuss Internationalization in Angular 8 using ngx-translate library.

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

Prerequisites

  • Basic knowledge of Angular.
  • Node and NPM installed.
  • Visual Studio Code.

If you're new to Angular, then you can follow this tutorial on getting started.

Angular is a TypeScript-based, open source web application framework developed by Google. Angular is a platform for building mobile, desktop, and web applications.

You may also like: Angular: Everything You Need to Know [Tutorials].

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.js is an open source, cross-platform JavaScript runtime environment that executes JavaScript code outside of a browser. See Node.js' website for download and installation. 
  2. Angular CLI is a command line interface tool that we use to initialize, develop, and maintain Angular applications. Download this with the following command: npm install -g @angular/cli 
  3. Visual Studio Code — optional download link here. 

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 

Creating a new Angular project

Creating a new Angular project

Now, open this project in Visual Studio Code. To open this 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 commands. We can install bootstrap and jquery for design.

npm install @ngx-translate/core --save

npm install @ngx-translate/http-loader --save

npm install bootstrap@4 jquery –save


Example of how to run the previous commands:
Output of previous commands

Output of previous commands

After installing the bootstrap package, we have to add reference of the CSS file on the styles.css file. To do this, fun the following command:

 @import "~bootstrap/dist/css/bootstrap.css"; 

Step 3

Import the necessary modules into app.module.ts.

Installing necessary modules

Installing necessary modules

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 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.

Examples:

Example of JSON file

Example of JSON file

Example of key-value pairs

Example of key-value pairs

Step 5

Open the en.json file and paste the following code

{  
    "Addemployee": "Add-employee",  
    "Name": "Name",  
    "Email": "Email",  
    "PhoneNo": "Phone No",  
    "Submit": "Submit",  
    "Cancel": "Cancel",  
    "Home": "Home",  
    "Employee": "Employee",  
    "EmployeeList": "Employee List"  
}  


Step 6

Open fr.json file and paste the following code.

{  
    "Addemployee": "Ajouter employé",  
    "Name": "prénom",  
    "Email": "Email",  
    "PhoneNo": "Pas de téléphone",  
    "Submit": "Soumettre",  
    "Cancel": "Annuler",  
    "Home": "Accueil",  
    "Employee": "Employée",  
    "EmployeeList": "Liste des employés"  
}  


Step 7

Open the app.component.html file and paste the following code.

<!--The content below is only a placeholder and can be replaced.-->  

<div class="bg-dark" style="text-align:center;color: #fff">  

  <h2 class="navbar-brand ">  
    Welcome to {{ title }}!  
  </h2>  
  <select #langSelect (change)="changeLang(langSelect.value)">  
      <option *ngFor="let lang of translate.getLangs()" [value]="lang" [selected]="lang === translate.currentLang">{{ lang }}</option>  
  </select>  
</div>  
<div class="row">  
  <div class="col-md-2">  
    <ul class="list-group">  
      <li class="list-group-item"><a [routerLink]="['/']">{{ 'Home' | translate }}</a></li>  
      <li class="list-group-item"><a [routerLink]="['/employee']">{{ 'Employee' | translate }}</a></li>  
      <li class="list-group-item"><a [routerLink]="['/employeelist']">{{ 'EmployeeList' | translate }}</a></li>  
      <!-- <li class="list-group-item"><a href="employeelist">EmployeeList(Reload)</a></li> -->  
    </ul>  
  </div>  
  <div class="col-md-8">  
    <router-outlet></router-outlet>  
  </div>  
</div>  


Ex.

Declaring key and translating text

Declaring key and translating text

Step 8

Open the app.component.ts file and paste the following code.

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

@Component({  
  selector: 'app-root',  
  templateUrl: './app.component.html',  
  styleUrls: ['./app.component.css']  
})  
export class AppComponent {  
  title = 'MultilanguageApp';  

  constructor(  
    public translate: TranslateService) {  
    translate.addLangs(['en', 'fr']);  
    if (localStorage.getItem('locale')) {  
      const browserLang = localStorage.getItem('locale');  
      translate.use(browserLang.match(/en|fr/) ? browserLang : 'en');  
    } else {  
      localStorage.setItem('locale', 'en');  
      translate.setDefaultLang('en');  
    }  
  }  
  changeLang(language: string) {  
    localStorage.setItem('locale', language);  
    this.translate.use(language);  
  }  
} 


Ex.
Image title

Import service on constructor

Step 9

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

Running the command on port 4200

Output Window

Final output

Final output

Summary

In this article, 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!


Further Reading

  • Angular Tutorial: Angular 7 and the RESTEasy Framework.
  • Angular Essentials.
  • Angular 7 + Spring Boot Application: Hello World Example.
AngularJS mobile app

Opinions expressed by DZone contributors are their own.

Related

  • 7 Best IDEs for AngularJS App Development
  • Leveraging Salesforce Using a Client Written In Angular
  • Ionic App Development Over Other Frameworks: Is It Hyped?
  • AngularJS Vs. ReactJS Vs. VueJS: A Detailed Comparison

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!