国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

Angular4項目中添加i18n國際化插件ngx-translate的步驟詳解

2019-11-19 16:12:30
字體:
來源:轉載
供稿:網友

前言

本文將介紹在 Angular4 項目中配置 ngx-translate i18n 國際化組件的相關內容,分享出來供大家參考學習,下面來一起看看詳細的介紹:

npm 安裝 ngx-translate 模塊

npm install @ngx-translate/core --savenpm install @ngx-translate/http-loader --save

在 Angular 項目配置

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] } }) ]

結果如下:

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 --- }

結果如下:

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/ 下創建 i18n 文件夾,并在文件夾內創建 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": "你好"}

測試結果

在中文下

在英文下

示例代碼

Github地址:angular + ngx-translate

本地下載地址:http://xiazai.VeVB.COm/201707/yuanma/james-blog-ui(VeVB.COm).rar

參考文章

ngx-translate core

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如有疑問大家可以留言交流,謝謝大家對武林網的支持。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 安宁市| 福海县| 湘潭县| 天长市| 聂拉木县| 措勤县| 娱乐| 伊川县| 本溪市| 乌苏市| 微博| 紫云| 惠来县| 彭水| 乐平市| 开远市| 葫芦岛市| 乌鲁木齐县| 无为县| 河北区| 达拉特旗| 秭归县| 汕头市| 桐梓县| 拉萨市| 唐山市| 富顺县| 永修县| 都兰县| 涪陵区| 乌海市| 上高县| 体育| 屏东市| 洞口县| 双桥区| 信阳市| 云安县| 泉州市| 沁源县| 武清区|