Angular4項(xiàng)目中添加i18n國際化插件ngx-translate的步驟詳解
前言
本文將介紹在 Angular4 項(xiàng)目中配置 ngx-translate i18n 國際化組件的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面來一起看看詳細(xì)的介紹:
npm 安裝 ngx-translate 模塊
npm install @ngx-translate/core --save npm install @ngx-translate/http-loader --save
在 Angular 項(xiàng)目配置
app.module.ts
添加
import { TranslateLoader, TranslateModule} from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
imports: [
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: (createTranslateHttpLoader),
deps: [Http]
}
})
]
結(jié)果如下:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpModule, Http } from '@angular/http';
import { TranslateLoader, TranslateModule} from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
import { AppComponent } from './app.component';
export function createTranslateHttpLoader(http: Http) {
return new TranslateHttpLoader(http, './assets/i18n/', '.json');
}
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: (createTranslateHttpLoader),
deps: [Http]
}
})
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
app.component.ts
添加
import { TranslateService } from "@ngx-translate/core";
constructor(public translateService: TranslateService) {
}
ngOnInit() {
// --- set i18n begin ---
this.translateService.addLangs(["zh", "en"]);
this.translateService.setDefaultLang("zh");
const browserLang = this.translateService.getBrowserLang();
this.translateService.use(browserLang.match(/zh|en/) ? browserLang : 'zh');
// --- set i18n end ---
}
結(jié)果如下:
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 = 'app';
constructor(public translateService: TranslateService) {
}
ngOnInit() {
// --- set i18n begin ---
this.translateService.addLangs(["zh", "en"]);
this.translateService.setDefaultLang("zh");
const browserLang = this.translateService.getBrowserLang();
this.translateService.use(browserLang.match(/zh|en/) ? browserLang : 'zh');
// --- set i18n end ---
}
}
添加多語言文件
在 src/app/assets/ 下創(chuàng)建 i18n 文件夾,并在文件夾內(nèi)創(chuàng)建 en.json 和 zh.json 文件

測試
app.component.html
添加代碼:
<div>
<span> test the i18n module: ngx-translate</span>
<h1>{{ 'hello' | translate }}</h1>
</div>
在 en.json 和 zh.json 文件中添加配置
en.json
{
"hello": "the word is hello"
}
zh.json
{
"hello": "你好"
}
測試結(jié)果
在中文下

在英文下

示例代碼
Github地址:angular + ngx-translate
本地下載地址:http://xiazai.jb51.net/201707/yuanma/james-blog-ui(jb51.net).rar
參考文章
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
- Angular實(shí)現(xiàn)的進(jìn)度條功能示例
- AngularJS實(shí)現(xiàn)進(jìn)度條功能示例
- Spring Boot+AngularJS+BootStrap實(shí)現(xiàn)進(jìn)度條示例代碼
- 如何在Angular2中使用jQuery及其插件的方法
- AngularJS中如何使用echart插件示例詳解
- 如何在AngularJs中調(diào)用第三方插件庫
- Angular5中調(diào)用第三方j(luò)s插件的方法
- 詳解在Angular項(xiàng)目中添加插件ng-bootstrap
- Angular2整合其他插件的方法
- angular4+百分比進(jìn)度顯示插件用法示例
相關(guān)文章
Angularjs的$http異步刪除數(shù)據(jù)詳解及實(shí)例
這篇文章主要介紹了Angularjs的$http異步刪除數(shù)據(jù)詳解及實(shí)例的相關(guān)資料,這里提供實(shí)現(xiàn)思路及實(shí)現(xiàn)具體的方法,需要的朋友可以參考下2017-07-07
AngularJS入門教程二:在路由中傳遞參數(shù)的方法分析
這篇文章主要介紹了AngularJS在路由中傳遞參數(shù)的方法,結(jié)合實(shí)例形式分析了AngularJS實(shí)現(xiàn)路由中傳遞參數(shù)的相關(guān)技巧,并總結(jié)了相關(guān)操作步驟與注意事項(xiàng),需要的朋友可以參考下2017-05-05
Angular使用 ng-img-max 調(diào)整瀏覽器中的圖片的示例代碼
本篇文章主要介紹了Angular使用 ng-img-max 調(diào)整瀏覽器中的圖片的示例代碼,具有一定的參考價值,有興趣的可以了解一下2017-08-08
Angular2使用SVG自定義圖表(條形圖、折線圖)組件示例
這篇文章主要介紹了Angular2使用SVG自定義圖表(條形圖、折線圖)組件,涉及Angular結(jié)合svg進(jìn)行圖表繪制的相關(guān)操作技巧,需要的朋友可以參考下2019-05-05

