Angular 2/4 Content Translation using ngx-translate
NGX-Translate
NGX-Translate is the i18n library for Angular 2 or 4 and used for template translation in various languages and switching between the languages.It provides a service, a directive and a pipe to handle any dynamic/static template.
NGX-Translate is based on modular approach and customizable.
Implementation with ngx-translate
The main module of the library is named core. A good practice to add a loader to load translations into application.NGX-Translate Implementaion is available in the following modules:
- core: The internationalization (i18n) library for Angular 2/4.
- http-loader: A loader for ngx-translate that loads translations with http calls.
- ngx-translate-website: NGX-Translate, the internationalization (i18n) library for Angular 2/4
NGX-Translate Loaders
- Http Loader: Loads JSON translation files with http.
- Angular Universal Loader: Loads JSON translation files with fs instead of http.
- Po Loader: Loads .po files with http.
NGX-Translate Implementation: Core
- Import the TranslateModule
- Init the TranslationService for application
- Define the translations
- Use the service, the pipe or the directive
- Use the HTML tags
Step by step guide to apply localization on Angular-CLI
- Step-1. Create i18n folder in project root and add lang JSON eg. en.json fr.json or ge.json.
- Step-2. Open app.module.ts file and add the following lines of code:
imports:
import {TranslateModule, TranslateLoader} from "@ngx-translate/core"; import {TranslateHttpLoader} from "@ngx-translate/http-loader"; import {Http} from "@angular/http";
export function HttpLoaderFactory(http: Http) { return new TranslateHttpLoader(http, "i18n/", ".json"); }
TranslateModule.forRoot({ loader: { provide: TranslateLoader, useFactory: HttpLoaderFactory, deps: [Http] } })
- Step-3. Resolve dependencies, if you run the application you can see the following error:
app/app.module.ts(17,48): error TS2307: Cannot find module '@ngx-translate/core'. app/app.module.ts(18,35): error TS2307: Cannot find module '@ngx-translate/http-loader'.
npm install @ngx-translate/core --save npm install @ngx-translate/http-loader --save
- Step-4. Open your component.ts and import translate core:
import {TranslateService} from '@ngx-translate/core';
- Step-5. Open your component.ts and add/modify the constructor with:
constructor(private translate: TranslateService) { translate.addLangs(["en", "ge"]); translate.setDefaultLang('en'); let browserLang = translate.getBrowserLang(); translate.use(browserLang.match(/en|ge/) ? browserLang : 'en'); }
-
Step-6. Open component.html file and add the drop down logic to change the lang:
<div style="padding-top:15px;padding-right:5px;"> <label><span style="color:white">{{ 'header.select' | translate }}</span> <select #langSelect (change)="translate.use(langSelect.value)"> <option *ngFor="let lang of translate.getLangs()" [value]="lang" [selected]="lang === translate.currentLang">{{ lang }} </option> </select> </label> </div>
We have developed and implemented and example using these services which is available https://github.com/vinodkumartiwari/angular2-ngx-translate-demo-angular-cli and you can download/clone the code using the following url:
https://github.com/vinodkumartiwari/angular2-ngx-translate-demo-angular-cli.git
Comments
Post a Comment