Angular 4,如何用英语和阿拉伯语将数字转换为Word

时间:2017-12-06 11:43:10

标签: javascript angular angular-ui-router angular-directive

我正在尝试当用户输入金额数字显示单词英语和阿拉伯语中的数字

有英语和阿拉伯语的插件支持吗?

例如 : 1980年:一千九百八十。

1980:الفوتسعمائةوثمانون。

1 个答案:

答案 0 :(得分:0)

好的,你去吧

这应该是自我解释的。它获取当前日期,格式化它。您可以在英语和阿拉伯语之间切换。

请注意,我不会说阿拉伯语,但ar语言环境用于提供格式。因此,因为我知道一点,所以我也把中文包括在内。

<强> app.component.html

<h1>The current date and time using locale: {{locale}}</h1>
<p>Now: {{now | date : 'full' : '' : locale}}</p>
<button (click)="toggleLocale()">Toggle Locale</button>

<强> app.component.ts

import {Component, Inject} from '@angular/core';

@Component({
  selector: 'app',
  templateUrl: './app.component.html'
})
export default class {
  constructor(@Inject('registered-locales') readonly locales: string[]) {}

  get now() {return new Date();}

  val = 19.8;

  locale = this.locales[0];

  toggleLocale() {
    // this is just a cycler. There are better ways to right this code
    const predicate = this.locale && this.locale.match(/zh/)
      ? l => l.match('ar')
      : this.locale && this.locale.match(/en/)
      ? l => l.match('zh')
      : l => l.match('en');

    this.locale = this.locales.find(predicate);
  }
}

<强> app.module.ts

import arabic from '@angular/common/locales/ar';
import chinese from '@angular/common/locales/zh';
import english from '@angular/common/locales/en';
import {registerLocaleData} from '@angular/common';
import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import AppComponent from './app.component';

registerLocaleData(arabic, 'ar');
registerLocaleData(chinese, 'zh');
registerLocaleData(english, 'en');

@NgModule({
  providers: [
    {provide: 'registered-locales', multi: true, useValue: 'ar'},
    {provide: 'registered-locales', multi: true, useValue: 'zh'},
    {provide: 'registered-locales', multi: true, useValue: 'en'}
  ],
  imports: [BrowserModule],
  declarations: [AppComponent],
  bootstrap: [AppComponent]
}) export default class { }

所以你不能抱怨类型错误或类似的东西,这里是

<强> main.ts

import 'core-js';
import 'zone.js';
import {NgModuleRef} from '@angular/core';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';

import AppModule from './app/app.module';

const platform = platformBrowserDynamic();

(async function () {
  try {
    const ref = await platform.bootstrapModule(AppModule);
    const {ngRef} = window;
    if (ngRef) {
      ngRef.destroy();
    }
    window.ngRef = ref;
  }
  catch (e) {
    console.error(e);
  }
}());

declare global {
  interface Window {
    ngRef: NgModuleRef<AppModule>;
  }
}
运行

的相同代码的

Here is a live, editable example