我们在应用程序中使用Angular 6。在该应用程序中,我们希望提供多语言支持。
我们如何在angular 6中实现本地化和国际化?这是一个angular 6版本。
发布于 2018-06-30 10:53:49
参考:https://dgwebidea.blogspot.com/2020/01/angular-internationalization-and.html
使用ngx翻译Angular 6应用程序-翻译我们将做的事情:
创建最小的Angular6工程安装和加载ngx-translate初始化TranslateService创建.json翻译文件翻译简单标题和简介集成语言切换器翻译带变量的句子
使用嵌套的.json对象
创建最小Angular6项目
我们使用@angular/cli在终端中创建一个名为“traduction”的新项目:
ng new traduction --prefix tr
cd traduction
ng serve -o安装和加载ngx-translate
在终端中的项目文件夹“traduction”中使用npm:
npm install @ngx-translate/core --save
npm install @ngx-translate/http-loader注意:以下版本适用于angular 6
"@ngx-translate/core": "^9.1.1"
"@ngx-translate/http-loader": "^3.0.1"和angular 5使用最新版本10及更高版本
将必要的模块导入到app.module.ts中:
import { HttpClientModule, HttpClient } from '@angular/common/http';
import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';添加一个函数,该函数返回一个“TranslateHttpLoader”并导出它(AoT需要)。在本例中,我们创建的HttpLoaderFactory函数返回一个可以使用Http和.json加载Translations的对象,但您可以编写自己的类,例如使用全局JavaScript变量而不是加载文件,或者使用Google Translate:
export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http);
}或
export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http, 'assets/i18n/', '.json');
}然后我们需要将我们的模块导入到@NgModule,这是告诉Angular将这个模块加载到你的AppModule中的导入:
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient]
}
})
],
providers: [],
bootstrap: [AppComponent]
})注入TranslateService
在“app.component.ts”中,我们现在初始化“TranslateService”,我们导入TranslateService:
import { TranslateService } from '@ngx-translate/core';然后在AppComponent类中注入服务并定义我们的默认语言。为了为下一步做好准备,我们添加了一个切换语言的函数。
constructor(private translate: TranslateService) {
translate.setDefaultLang('en');
}
switchLanguage(language: string) {
this.translate.use(language);
}创建.json转换文件
现在,我们在assets/i18n文件夹中创建转换文件:
src/assets/i18n/en.json
{
"Title": "Translation example",
"Intro": "Hello I am Arthur, I am 42 years old."
}
src/assets/i18n/fr.json
{
"Title": "Exemple de traduction",
"Intro": "Bonjour je m'appelle Arthur, j'ai 42 ans."
}这些是简单的.json文件,将由我们在“app.module.ts”中创建的“TranslateHttpLoader”加载。
翻译简单标题和简介
在app.component.html中,我们在“h1”标签中添加一个带有translate“directive”的标头。此指令将获取标记内的文本,并将其替换为匹配的翻译。如果您使用该指令,则必须确保标记中除了文本之外没有其他内容。
作为第二个例子,我们使用“TranslationPipe”来翻译一个定义为内联字符串的标签。因为我们有时需要替换转换中的值,所以可以将数据对象传递到“translate”管道中。
<h1 translate>Title</h1>
<div>
{{ 'Intro' | translate:user }}
</div>集成语言切换器
我们现在可以将上面在app.component.ts中看到的语言切换器函数附加到一个按钮上。在本例中,我们为每种语言创建一个按钮,并使用匹配的语言键调用switchLanguage()函数。
<button (click)="switchLanguage('en')">en</button>
<button (click)="switchLanguage('fr')">fr</button>使用变量进行翻译句子
正如前面提到的,有时你会遇到包含变量的句子。在这个小示例中,我们有一个user对象,它的年龄和名称在“app.component.ts”中,我们想要翻译一个包含下列值的句子:
...
export class AppComponent {
user = {
name: 'Arthur',
age: 42
};
...
}因为我们将这个对象传递给了“translate”管道,所以我们现在可以通过{{ placeholder }}表示法在翻译中使用它的属性。
src/assets/i18n/en.json
{
"Title": "Translation example",
"Intro": "Hello I am {{name}}, I am {{age}} years old."
}
src/assets/i18n/fr.json
{
"Title": "Exemple de traduction",
"Intro": "Bonjour je m'appelle {{name}}, j'ai {{age}} ans."
}使用嵌套.json对象的
如果您希望能够对翻译有更多的控制,例如翻译页面块(从最终用户的角度)或组件(从开发人员的角度),一种解决方案可能是:使用git代码库中描述的嵌套.json对象。-json文件中的示例可能如下所示:
{
"Title": "Translation example",
"Intro": "Hello I am {{name}}, I am {{age}} years old.",
"Startpage": {
"TranslationSections": "Hello World"
},
"Aboutpage": {
"TranslationSections": "We are letsboot"
}
}
{
"Title": "Exemple de traduction",
"Intro": "Bonjour je m'appelle {{name}}, j'ai {{age}} ans.",
"Startpage": {
"TranslationSections": "Bonjour Monde"
},
"Aboutpage": {
"TranslationSections": "Nous sommes letsboot"
}
}并在相应的模板中:
<h1 translate>Title</h1>
<div>
{{ 'Intro' | translate:user }}
</div>
<div>
{{ 'Startpage.TranslationSections' | translate }}
</div>
<div>
{{ 'Aboutpage.TranslationSections' | translate }}
</div>
<br/>
<button (click)="switchLanguage('en')">en</button>
<button (click)="switchLanguage('fr')">fr</button>发布于 2019-06-08 14:08:55
component.module.ts
export function translateHttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http);
}
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: translateHttpLoaderFactory,
deps: [HttpClient]
}
})类LanguagService.ts
import { Injectable } from '@angular/core';
import { TranslateService, LangChangeEvent } from '@ngx-translate/core';
import { ReplaySubject } from 'rxjs';
import { take } from 'rxjs/operators';
@Injectable({ providedIn: 'root' })
export class LanguageService {
language$ = new ReplaySubject<LangChangeEvent>(1);
translate = this.translateService;
// 國旗對照;
constructor(private translateService: TranslateService) {}
setInitState() {
this.translateService.addLangs(['en', 'cn','vi']);
console.log( 'Browser Lang', this.translate.getBrowserLang());
const browserLang = (this.translate.getBrowserLang().includes('vi')) ? 'vi' : 'cn' ;
console.log("anhtt "," anguage = " +browserLang);
this.setLang(browserLang);
}
setLang(lang: string) {
this.translateService.onLangChange.pipe(take(1)).subscribe(result => {
this.language$.next(result);
});
this.translateService.use(lang);
}
}app.component.html
<h1>How to multi language in angular 7</h1>
<p >{{'content' |translate}}</p>
<h4 translate>
{{'message' |translate}}
</h4>
<button (click)="selectLanguageEN()">English</button>
<button (click)="selectLanguageCN()">中國</button>
<button (click)="selectLanguageVI()">Viet Nam</button>代码演示:
https://tienanhvn.blogspot.com/2019/06/angular-7-how-to-multi-language.html
https://stackoverflow.com/questions/51111329
复制相似问题