我正在使用Angular 2本地化支持日期和货币.
本地化设置在主应用程序模块级别完成.
在我的应用程序模块设置中,如果我神奇地配置LOCALE_ID提供程序,我有本地化支持.
@NgModule({ imports: [ BrowserModule ],declarations: [ AppComponent ],providers: [ { provide: LOCALE_ID,useValue: 'nl' } ],bootstrap: [ AppComponent ] }) export class AppModule { }
现在,如果我在我的应用程序上使用货币管道,如下所示:
@Component({ selector: 'my-app',template: '<h1>{{title}}</h1>' + '<div>{{convertNumber | currency}}</div>',}) export class AppComponent { title = 'Currency Test'; convertNumber = '12.30'; }
您可以在此plnkr code中找到工作示例.
我的产量为12,30美元.
荷兰的汇率不是美元.
基于这个问题,我有两个问题:
>当我配置LOCALE_ID时到底发生了什么?
这个本地化文件在哪里?
>我不想看到美元或欧元,我希望看到货币符号本身. (例如€)在文档中,货币的默认配置是货币符号(€),而不是货币文本(EUR).
如何找到此文件并进行编辑?
谢谢.
解决方法
我搜索了源代码,这是我对你的问题的调查结果:
首先,如果你看一下货币管道线159 Source1
返回NumberFormatter.format.到目前为止,locale参数始终被携带(但从未实际使用过)
最后在NumberFormatter.format源代码Source3中,它返回一个Intl.NumberFormat
从mdn:https://developer.mozilla.org/tr/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat看一下这个方法
var number = 123456.789; // request a currency format console.log(new Intl.NumberFormat('de-DE',{ style: 'currency',currency: 'EUR' }).format(number)); // → 123.456,79 €
因此,让我们回到源代码,看看在第二个参数中设置此货币选项的位置.
https://github.com/angular/angular/blob/master/packages/common/src/pipes/intl.ts#L34
可以预期Angular会将locale参数转换为实际的本地货币(例如nl到EUR),而是将其转换为实际的本地货币
options.currency = typeof currency == 'string' ? currency : undefined;
因此,如果您没有传递这样的货币输入:
'<div>{{convertNumber | currency:"EUR":true}}</div>',