秋天,大雁南飞,草木枯荣,掉落下的依依不舍的树叶随着瑟瑟的秋风飞动。尽管田园一片荒凉,但金灿灿的干草已覆盖了整个大地,几颗松树笔直的立在大地上,天空是多么广阔,多么蔚蓝!地是多么浩瀚,多么无边!天和地连在了一起,已分不清地平线在哪里,秋天的原野拥抱着蓝天,广阔的蓝天拥抱着田野!
一. 将ngx-translate添加到Angular应用程序中
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中设置初始值
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文件下创建让我们为英文翻译创建相关语言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>
到此这篇关于如何在Angular8.0下使用ngx-translate进行国际化配置就介绍到这了。人生没有一帆风顺,失败并不可悲,可悲的是从此一蹶不振,从此放弃了整个人生成功。更多相关如何在Angular8.0下使用ngx-translate进行国际化配置内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!



