如何使用ngx-translate本地化日期管道



我想把这个日期管道翻译成挪威语。是否有一种不同于普通文本或变量翻译的特定方法来翻译日期管道。

{{activity.feed.timestamp | timeAgo}}

您可以检查支持i18n的ngx timeago。

I18n默认情况下,没有可用的intl服务格式化程序不提供语言支持。如果你最终得到了一个需要它的格式化程序(或者由库或您自己提供的TimeagoCustomFormatter(。这个intl服务的目的是包含所有必要的i18n格式化程序使用的字符串。

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { Timeago, TimeagoIntl, TimeagoFormatter, TimeagoCustomFormatter } from 'ngx-timeago';
import { AppComponent } from './app';
export class MyIntl extends TimeagoIntl {
// do extra stuff here...
}
@NgModule({
imports: [
BrowserModule,
TimeagoModule.forRoot({
intl: { provide: TimeagoIntl, useClass: MyIntl },
formatter: { provide: TimeagoFormatter, useClass: TimeagoCustomFormatter },
})
],
bootstrap: [AppComponent]
})
export class AppModule { }

支持大量现成的语言。这支持基于从jquery timeago中获取的字符串对象。

若要使用提供的任何语言,您必须导入语言字符串,并将它们提供给intl服务。

import { Component } from '@angular/core';
import { TimeagoIntl } from 'ngx-timeago';
import {strings as englishStrings} from 'ngx-timeago/language-strings/en';
@Component({
selector: 'app',
template: `
<div timeago [date]="1553683912689"></div>
`
})
export class AppComponent {
constructor(intl: TimeagoIntl) {
intl.strings = englishStrings;
intl.changes.next();
}
}

您也可以自定义语言字符串或提供自己的语言字符串。

最新更新