feat(common): allow default currency code to be configurable (#34771)

Default currency code in CurrencyPipe is currently hardcoded to USD
and is not configurable. This commit allows the default currency code
to be configurable by adding a DEFAULT_CURRENCY_CODE injection token.

Example:
```
providers: [{ provide: DEFAULT_CURRENCY_CODE, useValue: "GBP" }]
...
{{ 123.45 | currency }} // outputs £123.45 as opposed to always $123.45 before
```

Closes: #25461

PR Close #34771
This commit is contained in:
Hayouung
2019-12-14 10:39:41 +00:00
committed by atscott
parent d318249125
commit 965f5575c7
9 changed files with 52 additions and 10 deletions

View File

@ -14,8 +14,8 @@ import {Console} from './console';
import {Injector, StaticProvider} from './di';
import {Inject, Optional, SkipSelf} from './di/metadata';
import {ErrorHandler} from './error_handler';
import {DEFAULT_LOCALE_ID} from './i18n/localization';
import {LOCALE_ID} from './i18n/tokens';
import {DEFAULT_LOCALE_ID, USD_CURRENCY_CODE} from './i18n/localization';
import {DEFAULT_CURRENCY_CODE, LOCALE_ID} from './i18n/tokens';
import {ivyEnabled} from './ivy_switch';
import {ComponentFactoryResolver} from './linker';
import {Compiler} from './linker/compiler';
@ -96,6 +96,7 @@ export const APPLICATION_MODULE_PROVIDERS: StaticProvider[] = [
useFactory: _localeFactory,
deps: [[new Inject(LOCALE_ID), new Optional(), new SkipSelf()]]
},
{provide: DEFAULT_CURRENCY_CODE, useValue: USD_CURRENCY_CODE},
];
/**

View File

@ -26,7 +26,7 @@ export {DebugElement, DebugEventListener, DebugNode, asNativeElements, getDebugN
export {GetTestability, Testability, TestabilityRegistry, setTestabilityGetter} from './testability/testability';
export * from './change_detection';
export * from './platform_core_providers';
export {TRANSLATIONS, TRANSLATIONS_FORMAT, LOCALE_ID, MissingTranslationStrategy} from './i18n/tokens';
export {TRANSLATIONS, TRANSLATIONS_FORMAT, LOCALE_ID, DEFAULT_CURRENCY_CODE, MissingTranslationStrategy} from './i18n/tokens';
export {ApplicationModule} from './application_module';
export {AbstractType, Type} from './interface/type';
export {EventEmitter} from './event_emitter';

View File

@ -34,3 +34,9 @@ export function getPluralCase(value: any, locale: string): string {
* The locale id that the application is using by default (for translations and ICU expressions).
*/
export const DEFAULT_LOCALE_ID = 'en-US';
/**
* USD currency code that the application uses by default for CurrencyPipe when no
* DEFAULT_CURRENCY_CODE is provided.
*/
export const USD_CURRENCY_CODE = 'USD';

View File

@ -32,6 +32,29 @@ import {InjectionToken} from '../di/injection_token';
*/
export const LOCALE_ID = new InjectionToken<string>('LocaleId');
/**
* Provide this token to set the default currency code your application uses for
* CurrencyPipe when there is no currency code passed into it. This is only used by
* CurrencyPipe and has no relation to locale currency. Defaults to USD if not configured.
*
* See the [i18n guide](guide/i18n#setting-up-locale) for more information.
*
* @usageNotes
* ### Example
*
* ```typescript
* import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
* import { AppModule } from './app/app.module';
*
* platformBrowserDynamic().bootstrapModule(AppModule, {
* providers: [{provide: DEFAULT_CURRENCY_CODE, useValue: 'EUR' }]
* });
* ```
*
* @publicApi
*/
export const DEFAULT_CURRENCY_CODE = new InjectionToken<string>('DefaultCurrencyCode');
/**
* Use this token at bootstrap to provide the content of your translation file (`xtb`,
* `xlf` or `xlf2`) when you want to translate your application in another language.