diff --git a/packages/input-amount/src/LionInputAmount.js b/packages/input-amount/src/LionInputAmount.js index 24139559d..6cb1aae68 100644 --- a/packages/input-amount/src/LionInputAmount.js +++ b/packages/input-amount/src/LionInputAmount.js @@ -1,9 +1,9 @@ import { css } from '@lion/core'; -import { LocalizeMixin, getCurrencyName } from '@lion/localize'; +import { LocalizeMixin, getCurrencyName, localize } from '@lion/localize'; import { LionInput } from '@lion/input'; import { IsNumber } from '@lion/validate'; import { parseAmount } from './parsers.js'; -import { formatAmount } from './formatters.js'; +import { formatAmount, formatCurrencyLabel } from './formatters.js'; /** * `LionInputAmount` is a class for an amount custom form element (``). @@ -40,7 +40,8 @@ export class LionInputAmount extends LocalizeMixin(LionInput) { // The data-label attribute will make sure that FormControl adds this to // input[aria-labelledby] el.setAttribute('data-label', ''); - el.textContent = this.currency; + + el.textContent = this.__getCurrencyLabel(); return el; } return null; @@ -114,7 +115,7 @@ export class LionInputAmount extends LocalizeMixin(LionInput) { _onCurrencyChanged({ currency }) { if (this._isPrivateSlot('after')) { - this._currencyDisplayNode.textContent = currency; + this._currencyDisplayNode.textContent = this.__getCurrencyLabel(); } this.formatOptions.currency = currency; this._calculateValues(); @@ -127,4 +128,12 @@ export class LionInputAmount extends LocalizeMixin(LionInput) { // sufficient, it should also contain the abbreviation. this._currencyDisplayNode.setAttribute('aria-label', getCurrencyName(this.currency)); } + + __getCurrencyLabel() { + return formatCurrencyLabel(this.currency, this.__getLocale()); + } + + __getLocale() { + return this.locale || localize.locale; + } } diff --git a/packages/input-amount/src/formatters.js b/packages/input-amount/src/formatters.js index aac5fdda8..c1bcd500d 100644 --- a/packages/input-amount/src/formatters.js +++ b/packages/input-amount/src/formatters.js @@ -1,4 +1,4 @@ -import { formatNumber, getFractionDigits } from '@lion/localize'; +import { formatNumber, getFractionDigits, normalizeCurrencyLabel } from '@lion/localize'; /** * Formats a number considering the default fraction digits provided by Intl @@ -23,3 +23,10 @@ export function formatAmount(modelValue, givenOptions) { return formatNumber(modelValue, options); } + +export function formatCurrencyLabel(currency, locale) { + if (currency === '') { + return ''; + } + return normalizeCurrencyLabel(currency, locale); +} diff --git a/packages/input-amount/test/lion-input-amount.test.js b/packages/input-amount/test/lion-input-amount.test.js index ca7484c3c..fd881a39e 100644 --- a/packages/input-amount/test/lion-input-amount.test.js +++ b/packages/input-amount/test/lion-input-amount.test.js @@ -79,6 +79,12 @@ describe('', () => { expect(Array.from(el.children).find(child => child.slot === 'after').innerText).to.equal('EUR'); }); + it('displays correct currency for TRY if locale is tr-TR', async () => { + localize.locale = 'tr-TR'; + const el = await fixture(``); + expect(Array.from(el.children).find(child => child.slot === 'after').innerText).to.equal('TL'); + }); + it('can update currency', async () => { const el = await fixture(``); el.currency = 'USD'; diff --git a/packages/localize/index.js b/packages/localize/index.js index 86412a7c8..f4790f07b 100644 --- a/packages/localize/index.js +++ b/packages/localize/index.js @@ -13,3 +13,4 @@ export { getGroupSeparator } from './src/number/getGroupSeparator.js'; export { localize, setLocalize } from './src/localize.js'; export { LocalizeManager } from './src/LocalizeManager.js'; export { LocalizeMixin } from './src/LocalizeMixin.js'; +export { normalizeCurrencyLabel } from './src/number/normalizeCurrencyLabel.js'; diff --git a/packages/localize/src/number/normalizeCurrencyLabel.js b/packages/localize/src/number/normalizeCurrencyLabel.js new file mode 100644 index 000000000..5054830fc --- /dev/null +++ b/packages/localize/src/number/normalizeCurrencyLabel.js @@ -0,0 +1,13 @@ +/** + * Function that fixes currency label with locale options + * + * @param {String} currency + * @param {string} locale + * @returns {string} currency + */ +export function normalizeCurrencyLabel(currency, locale) { + if (currency === 'TRY' && locale === 'tr-TR') { + return 'TL'; + } + return currency; +}