如何在Angular 8.0中配置ngx-translate实现国际化?

2026-04-08 17:351阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计345个文字,预计阅读时间需要2分钟。

如何在Angular 8.0中配置ngx-translate实现国际化?

一. 将ngx-translate添加到Angular应用程序中+npm install @ngx-translate/core @ngx-translate/http-loader rxjs --save

二. 在app.module.ts中初始化翻译模块import { BrowserModule } from '@angular/platform-browser';import { TranslateModule } from '@ngx-translate/core';

import { NgModule } from '@angular/core';

@NgModule({ declarations: [], imports: [ BrowserModule, TranslateModule ], providers: [], bootstrap: [AppComponent]})export class AppModule { }

一. 将ngx-translate添加到Angular应用程序中

如何在Angular 8.0中配置ngx-translate实现国际化?

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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计345个文字,预计阅读时间需要2分钟。

如何在Angular 8.0中配置ngx-translate实现国际化?

一. 将ngx-translate添加到Angular应用程序中+npm install @ngx-translate/core @ngx-translate/http-loader rxjs --save

二. 在app.module.ts中初始化翻译模块import { BrowserModule } from '@angular/platform-browser';import { TranslateModule } from '@ngx-translate/core';

import { NgModule } from '@angular/core';

@NgModule({ declarations: [], imports: [ BrowserModule, TranslateModule ], providers: [], bootstrap: [AppComponent]})export class AppModule { }

一. 将ngx-translate添加到Angular应用程序中

如何在Angular 8.0中配置ngx-translate实现国际化?

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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。