如何在Angular8.0下使用ngx-translate進(jìn)行國際化配置
一. 將ngx-translate添加到Angular應(yīng)用程序中
npm install @ngx-translate/core @ngx-translate/http-loader rxjs --save
二.在app.module.ts中初始化翻譯TranslateModule
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
// import ngx-translate and the http loader
import {TranslateLoader, TranslateModule} from '@ngx-translate/core';
import {TranslateHttpLoader} from '@ngx-translate/http-loader';
import {HttpClient, HttpClientModule} from '@angular/common/http';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
// ngx-translate and the loader module
HttpClientModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient]
}
})
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
// required for AOT compilation
export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http);
}
三.在app.component.ts中設(shè)置初始值
import {Component} from '@angular/core';
import {TranslateService} from '@ngx-translate/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
constructor(private translate: TranslateService) {
translate.setDefaultLang('en');
}
}
四.在assets/i18n文件下創(chuàng)建讓我們?yōu)橛⑽姆g創(chuàng)建相關(guān)語言JSON文件,如en.json文件
{
"demo.title": "Translation demo",
"demo.text": "This is a simple demonstration app for ngx-translate"
}
五.在app.component.html中使用
<div>
<!-- translation: translation pipe -->
<h1>{{ 'demo.title' | translate }}</h1>
<!-- translation: directive (key as attribute)-->
<p [translate]="'demo.text'"></p>
<!-- translation: directive (key as content of element) -->
<p translate>demo.text</p>
</div>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
@angular前端項(xiàng)目代碼優(yōu)化之構(gòu)建Api Tree的方法
這篇文章主要介紹了@angular前端項(xiàng)目代碼優(yōu)化之構(gòu)建Api Tree的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12
angular2+nodejs實(shí)現(xiàn)圖片上傳功能
這篇文章主要介紹了angular2+nodejs實(shí)現(xiàn)圖片上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Angular2 自定義表單驗(yàn)證器的實(shí)現(xiàn)方法
這篇文章主要介紹了Angular2 自定義表單驗(yàn)證器的實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12
angular 用Observable實(shí)現(xiàn)異步調(diào)用的方法
這篇文章主要介紹了angular 用Observable實(shí)現(xiàn)異步調(diào)用的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12
Angular中的結(jié)構(gòu)指令模式及使用詳解
這篇文章主要為大家介紹了Angular中的結(jié)構(gòu)指令模式及使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
AngularJS基礎(chǔ) ng-selected 指令簡單示例
本文主要介紹AngularJS ng-selected 指令,這里對(duì)ng-selected 指令的基礎(chǔ)資料做了詳細(xì)介紹,并附有示例代碼,有需要的小伙伴可以參考下2016-08-08
Angular懶加載機(jī)制刷新后無法回退的快速解決方法
使用oclazyload懶加載angular的模塊,刷新頁面后,單擊回退按鈕無法返回上一個(gè)頁面.怎么回事呢?下面小編給大家?guī)砹薬ngular懶加載機(jī)制刷新后無法回退的快速解決方法,非常不錯(cuò),感興趣的朋友參考下2016-08-08

