angular的国际化方案,采用ngx-translate来实现。
安装模块:
npm install @ngx-translate/core --save
在根模块中导入:
// other module
import {TranslateModule} from '@ngx-translate/core';
@NgModule({
declarations: [
AppComponent,
],
imports: [
// other module
TranslateModule.forRoot(),
],
providers: [
],
bootstrap: [AppComponent]
})
export class AppModule {
}
我们希望可以在一个固定的文件里面配置对应的翻译文件,然后在每个用到的组件里面使用它,随意我们需要借助TranslateHttpLoader来加载翻译文件。首先安装TranslateHttpLoader:
npm install @ngx-translate/http-loader --save
翻译文件可以放在/assets/i18n/[lang].json中,[lang]代表使用的语言文件名称。然后我们可以在跟组件中添加配置对应的加载项:
// other module
import {TranslateModule} from '@ngx-translate/core';
// 自定义加载方法
export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http, './assets/i18n/', '.json"htmlcode">
// /asserts/il8n/en.json
{
"Hello": "hello, {{value}}",
"Introduce": {
"Name": "my name is {{name}}.",
"Today": "today is {{date}}, and now time is {{time}}"
}
}
应用的时候我们可以使用点语法,例如:Introduce.Name。
好了,定义好之后再来看如何使用。我们可以使用服务或管道或指令的方式来达到显示语言的效果。在使用之前,我们需要在应用中初始化TranslateService:
import { Component } from '@angular/core';
import {TranslateService} from '@ngx-translate/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.less']
})
export class AppComponent {
constructor(
public translate: TranslateService,
) {
this.translate.setDefaultLang('en');
this.translate.use('en');
}
}
我倾向于在跟组件的construct里面初始化TranslateService,因为一个系统的翻译是统一的,在开始应用的时候就需要设置好默认语言。这里使用translate.setDefaultLang('en')来设置默认语言为英文。然后使用translate.user('en')手动选择使用英文。在切换语言的时候,我们使用translate.user([lang])来设置启用哪个语言。
最后来使用翻译,有多种使用的方式。来看看。
使用方式
使用Service的方式
在运行的时候,会先发起个请求通过Http获取翻译文件,通过Observable的方式应用参数上去,然后获得翻译的内容。
// app.compent.ts
this.translate.get(
'Introduce.Name',
{name: 'Jarvis'}
).subscribe((res: string) => {
console.log('res', res); // res my name is Jarvis.
});
this.translate.get(
'Introduce.Today',
{
date: new Date().getDate(),
time: new Date().getTime()
},
).subscribe((res: string) => {
console.log('res', res); // res today is 22, and now time is 1534937370461
});
使用pipe的方式
<div>{{'Hello' | translate: param</div>
在js里定义参数param:
const param = {
value: 'world',
};
使用指令
管道的方式虽然方便,但参数还是需要在先定义好,这样子变量多的话也比较繁琐。使用指令的方式可以在程序中直接传参:
<span [translate]="'Introduce.Name'" [translateParams]="{name: 'Jarvis'}"></span>
或者直接将元素的内容作为key:
<span translate [translateParams]="{date: '10.11', time: '20:33'}">Introduce.Today</span>
应用html标签
可以在翻译文件中中定义简单的行级html标签
{
"Hello": "hello, {{value}}",
}
要渲染它们,在任何元素上只需要将innerHTML属性和管道一同使用即可。
<p [innerHTML]="'Introduce.Name'| translate: param"></p>
常用方法
instant() 即时翻译
有些情况下,我们要在js里面动态的获取值和赋值,这时候没法使用模板语法,使用subscribe的方式又不利于代码的组织,这时候我们需要即时翻译来搞定了。方法定义:
instant(key: string|Array<string>), insterpolateParams"htmlcode">
this.translate.instant('HELLO', {value: 'Jarvis'});
但是需要注意的是,这个方法是同步的,默认加载器是异步的。使用这个方法需要确保翻译文件已经加载完成了,如果不确定,就应该用get的方式。
参考:
https://www.npmjs.com/package/@ngx-translate/core
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
angular多语言配置
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新动态
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]