-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(admin-ui): Currencies respect UI language setting
Relates to #568
- Loading branch information
1 parent
dd0e73a
commit 5530782
Showing
34 changed files
with
307 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 4 additions & 1 deletion
5
.../admin-ui/src/lib/core/src/shared/components/currency-input/currency-input.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 0 additions & 40 deletions
40
packages/admin-ui/src/lib/core/src/shared/pipes/currency-name.pipe.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
packages/admin-ui/src/lib/core/src/shared/pipes/locale-currency-name.pipe.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { ChangeDetectorRef, OnDestroy, Optional, Pipe, PipeTransform } from '@angular/core'; | ||
import { Subscription } from 'rxjs'; | ||
|
||
import { DataService } from '../../data/providers/data.service'; | ||
|
||
/** | ||
* Displays a human-readable name for a given ISO 4217 currency code. | ||
*/ | ||
@Pipe({ | ||
name: 'localeCurrencyName', | ||
pure: false, | ||
}) | ||
export class LocaleCurrencyNamePipe implements PipeTransform, OnDestroy { | ||
private locale: string; | ||
private readonly subscription: Subscription; | ||
|
||
constructor( | ||
@Optional() private dataService?: DataService, | ||
@Optional() changeDetectorRef?: ChangeDetectorRef, | ||
) { | ||
if (this.dataService && changeDetectorRef) { | ||
this.subscription = this.dataService.client | ||
.uiState() | ||
.mapStream(data => data.uiState.language) | ||
.subscribe(languageCode => { | ||
this.locale = languageCode.replace(/_/g, '-'); | ||
changeDetectorRef.markForCheck(); | ||
}); | ||
} | ||
} | ||
|
||
ngOnDestroy() { | ||
if (this.subscription) { | ||
this.subscription.unsubscribe(); | ||
} | ||
} | ||
|
||
transform(value: any, display: 'full' | 'symbol' | 'name' = 'full', locale?: unknown): any { | ||
if (value == null || value === '') { | ||
return ''; | ||
} | ||
if (typeof value !== 'string') { | ||
return `Invalid currencyCode "${value as any}"`; | ||
} | ||
let name = ''; | ||
let symbol = ''; | ||
const activeLocale = typeof locale === 'string' ? locale : this.locale ?? 'en'; | ||
|
||
if (display === 'full' || display === 'name') { | ||
name = new Intl.NumberFormat(activeLocale, { | ||
style: 'currency', | ||
currency: value, | ||
currencyDisplay: 'name', | ||
}) | ||
.format(undefined as any) | ||
.replace(/\s*NaN\s*/, ''); | ||
} | ||
if (display === 'full' || display === 'symbol') { | ||
symbol = new Intl.NumberFormat(activeLocale, { | ||
style: 'currency', | ||
currency: value, | ||
currencyDisplay: 'symbol', | ||
}) | ||
.format(undefined as any) | ||
.replace(/\s*NaN\s*/, ''); | ||
} | ||
return display === 'full' ? `${name} (${symbol})` : display === 'name' ? name : symbol; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
packages/admin-ui/src/lib/core/src/shared/pipes/locale-currency.pipe.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { CurrencyCode, LanguageCode } from '../../common/generated-types'; | ||
|
||
import { LocaleCurrencyPipe } from './locale-currency.pipe'; | ||
|
||
describe('LocaleCurrencyPipe', () => { | ||
it('GBP in English', () => { | ||
const pipe = new LocaleCurrencyPipe(); | ||
expect(pipe.transform(1, CurrencyCode.GBP, LanguageCode.en)).toBe('£0.01'); | ||
expect(pipe.transform(123, CurrencyCode.GBP, LanguageCode.en)).toBe('£1.23'); | ||
expect(pipe.transform(4200000, CurrencyCode.GBP, LanguageCode.en)).toBe('£42,000.00'); | ||
}); | ||
|
||
it('EUR in German', () => { | ||
const pipe = new LocaleCurrencyPipe(); | ||
expect(pipe.transform(1, CurrencyCode.EUR, LanguageCode.de)).toBe('0,01 €'); | ||
expect(pipe.transform(123, CurrencyCode.EUR, LanguageCode.de)).toBe('1,23 €'); | ||
expect(pipe.transform(4200000, CurrencyCode.EUR, LanguageCode.de)).toBe('42.000,00 €'); | ||
}); | ||
}); |
Oops, something went wrong.