ngx-localizer is a easy Angular internationalization tool which can help you make your app available in multiple languages.
- Custom type json translation files to translate between languaes.
- Simple html attribute to markdown html tags to translate.
- Quick rendering.
- Single route hosting.
- More customization options.
- Google translator support.
- Currencey, Date, number localization support.
ngx-localizer requires Angular v4.3+ to run.
Install the library using npm.
$ npm install ngx-localizer
You have to import NgxLocalizerModule in the root NgModule of your application in order to use the library.
import { NgModule } from "@angular/core";
import { NgxLocalizerModule } from "ngx-localizer";
@NgModule({
imports: [ NgxLocalizerModule]
})
Once you've imported the NgxLocalizerModule, You can mark html tags which will be used to translate using localizer attributte. string value passed to the localizer will be used to identify the translation.
<h1 class="header" [localizer]="'greeting1'">Good Morning</h1>
<h1 [localizer]="'greeting2'">Good Afternoon</h1>
You can put your translations in a json file located in a path as "/assets/localizer/localize.[lang].json" (default path). ([lang] is the lang that you're using, for english it could be en).
json strucure of the language file will be an array with a tag, value object model. Where tag will be used to identify the html tag. Value will be the translation text.
Example : localize.en.json file
[
{
"tag": "greeting1",
"value": "Good Morning"
},
{
"tag": "greeting2",
"value": "Good Afternoon"
}
]
Example : localize.fr.json file
[
{
"tag": "greeting1",
"value": "Bonjour"
},
{
"tag": "greeting2",
"value": "Bonsoir"
}
]
After configuring the translation you can use NgxLocalizerService to change the language. In following example fr is the [lang] prefix used to define the tranlate json file prefix.
import { Component } from '@angular/core';
import { NgxLocalizerService } from 'ngx-localizer';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor(private localizer:NgxLocalizerService){
}
change(): void{
this.localizer.setLocale('fr');
}
}
- setDefautLocale(lang: string): Used to set the default language as the fallback language.
- setLocale('fr'): Changes the displayed language.
- setPath(path: string) : Set a differnet path folder other than the default folder path.
- setPrefix(prefix: string): Set a diffrenet file prefix. (Default : localize)
0.0.3
- Fixed an issue with fallback language;
0.0.2
- Added service methods to change defult paths;
0.0.1
- Inital release;
- Sample plunker project will be added soon
MIT