Skip to content

Commit

Permalink
feat(locale): support default currency config (#423)
Browse files Browse the repository at this point in the history
* feat(locale): support default currency config
  • Loading branch information
seravifer authored Apr 13, 2021
1 parent ac7fb13 commit 463b170
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions projects/ngneat/transloco-locale/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ Let's go over each one of the `config` options:

- `localeConfig?`: Declare the default configuration of the locale's formatting. A general configuration could be set using the `global` property, for a configuration by locale use `localeBased` property (default value determine by the native [Javascript's API](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl)).
- `defaultLocale?`: The default locale formatted in [BCP 47](https://tools.ietf.org/html/bcp47) (default value: `en-US`),
- `defaultCurrency?`: The default currency formatted in [ISO 4217](https://www.iso.org/iso-4217-currency-codes.html) (default value: `USD`),
- `langToLocaleMapping?`: A key value `object` that maps Transloco language to it's Locale (default value: `{}`).
- `localeToCurrencyMapping?`: A key value `object` that maps the Locale to it's currency (formatted in [ISO 4217](https://www.iso.org/iso-4217-currency-codes.html)) (the library provide a default value with all of the existing mapping).

Expand Down
3 changes: 3 additions & 0 deletions projects/ngneat/transloco-locale/src/lib/tests/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export function createFakeCDR(locale: string = 'en-US') {
export const LOCALE_CURRENCY_MOCK = LOCALE_CURRENCY;
export const LANG_LOCALE_MOCK = { en: 'en-US', es: 'es-ES' };
export const DEFAULT_LOCALE_MOCK = 'en-US';
export const DEFAULT_CURRENCY_MOCK = 'USD';
export const LOCALE_CONFIG_MOCK: LocaleConfig = {
global: {
decimal: {
Expand Down Expand Up @@ -68,6 +69,7 @@ export const mockTranslocoService = (locale?: Locale): TranslocoService =>
export const mockService = (
translocoService = mockTranslocoService(),
locale = DEFAULT_LOCALE_MOCK,
currency = DEFAULT_CURRENCY_MOCK,
langLocale = LANG_LOCALE_MOCK,
config = LOCALE_CONFIG_MOCK,
localeCurrencyMapping = LOCALE_CURRENCY_MOCK,
Expand All @@ -78,6 +80,7 @@ export const mockService = (
translocoService,
langLocale,
locale,
currency,
config,
localeCurrencyMapping,
numberTransformer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface LocaleConfig {

export interface TranslocoLocaleConfig {
defaultLocale?: Locale;
defaultCurrency?: Currency;
localeConfig?: LocaleConfig;
langToLocaleMapping?: HashMap<Locale>;
localeToCurrencyMapping?: HashMap<Currency>;
Expand All @@ -28,11 +29,13 @@ export const defaultConfig: TranslocoLocaleConfig = {
localeBased: {}
},
defaultLocale: 'en-US',
defaultCurrency: 'USD',
localeToCurrencyMapping: LOCALE_CURRENCY,
langToLocaleMapping: {}
};

export const LOCALE_DEFAULT_LOCALE = new InjectionToken<NumberFormatOptions>('DEFAULT_LOCALE');
export const LOCALE_DEFAULT_CURRENCY = new InjectionToken<NumberFormatOptions>('DEFAULT_LOCALE_CURRENCY');
export const LOCALE_LANG_MAPPING = new InjectionToken<HashMap<Locale>>('LOCALE_LANG_MAPPING');
export const LOCALE_CONFIG = new InjectionToken<HashMap<LocaleFormatOptions>>('LOCALE_CONFIG');
export const LOCALE_CURRENCY_MAPPING = new InjectionToken<HashMap<Currency>>('LOCALE_CURRENCY_MAPPING');
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
LOCALE_LANG_MAPPING,
defaultConfig,
LOCALE_DEFAULT_LOCALE,
LOCALE_CONFIG
LOCALE_CONFIG,
LOCALE_DEFAULT_CURRENCY
} from './transloco-locale.config';
import {
TRANSLOCO_DATE_TRANSFORMER,
Expand Down Expand Up @@ -45,6 +46,10 @@ export class TranslocoLocaleModule {
provide: LOCALE_DEFAULT_LOCALE,
useValue: config.defaultLocale || defaultConfig.defaultLocale
},
{
provide: LOCALE_DEFAULT_CURRENCY,
useValue: config.defaultCurrency || defaultConfig.defaultCurrency
},
{
provide: TRANSLOCO_DATE_TRANSFORMER,
useClass: DefaultDateTransformer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
LOCALE_DEFAULT_LOCALE,
LOCALE_CONFIG,
LocaleConfig,
LOCALE_CURRENCY_MAPPING
LOCALE_CURRENCY_MAPPING,
LOCALE_DEFAULT_CURRENCY
} from './transloco-locale.config';
import {
TRANSLOCO_DATE_TRANSFORMER,
Expand All @@ -32,6 +33,7 @@ export class TranslocoLocaleService implements OnDestroy {
private translocoService: TranslocoService,
@Inject(LOCALE_LANG_MAPPING) private langLocaleMapping: HashMap<Locale>,
@Inject(LOCALE_DEFAULT_LOCALE) private defaultLocale: Locale,
@Inject(LOCALE_DEFAULT_CURRENCY) private defaultCurrency: Currency,
@Inject(LOCALE_CONFIG) private localeConfig: LocaleConfig,
@Inject(LOCALE_CURRENCY_MAPPING) private localeCurrencyMapping: HashMap<Currency>,
@Inject(TRANSLOCO_NUMBER_TRANSFORMER) private numberTransformer: TranslocoNumberTransformer,
Expand Down Expand Up @@ -127,7 +129,7 @@ export class TranslocoLocaleService implements OnDestroy {
* @internal
*/
_resolveCurrencyCode(locale: Locale = this.getLocale()) {
return this.localeCurrencyMapping[locale] || 'USD';
return this.localeCurrencyMapping[locale] || this.defaultCurrency;
}

private toLocale(val: string | Locale): Locale | null {
Expand Down

0 comments on commit 463b170

Please sign in to comment.