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:
@ -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},
|
||||
];
|
||||
|
||||
/**
|
||||
|
@ -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';
|
||||
|
@ -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';
|
||||
|
@ -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.
|
||||
|
@ -6,7 +6,7 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {LOCALE_ID} from '@angular/core';
|
||||
import {DEFAULT_CURRENCY_CODE, LOCALE_ID} from '@angular/core';
|
||||
import {ivyEnabled} from '@angular/private/testing';
|
||||
|
||||
import {getLocaleId} from '../src/render3';
|
||||
@ -19,6 +19,11 @@ import {describe, expect, inject, it} from '../testing/src/testing_internal';
|
||||
it('should set the default locale to "en-US"',
|
||||
inject([LOCALE_ID], (defaultLocale: string) => { expect(defaultLocale).toEqual('en-US'); }));
|
||||
|
||||
it('should set the default currency code to "USD"',
|
||||
inject([DEFAULT_CURRENCY_CODE], (defaultCurrencyCode: string) => {
|
||||
expect(defaultCurrencyCode).toEqual('USD');
|
||||
}));
|
||||
|
||||
if (ivyEnabled) {
|
||||
it('should set the ivy locale with the configured LOCALE_ID', () => {
|
||||
TestBed.configureTestingModule({providers: [{provide: LOCALE_ID, useValue: 'fr'}]});
|
||||
|
Reference in New Issue
Block a user