Skip to content

Commit

Permalink
fix(Currency): Add fallback for Safari (#7194)
Browse files Browse the repository at this point in the history
  • Loading branch information
Betree authored Nov 26, 2021
1 parent bc16685 commit c5800f0
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions lib/currency-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,25 @@ export function formatCurrency(amount, currency = 'USD', options = {}) {
minimumFractionDigits = options.precision;
maximumFractionDigits = options.precision;
}
return amount.toLocaleString(options.locale, {
style: 'currency',
currency,
minimumFractionDigits: minimumFractionDigits,
maximumFractionDigits: maximumFractionDigits,
currencyDisplay: 'narrowSymbol', // We manually add the exact currency (e.g. "$10 USD") in many places. This is to prevent showing the currency twice is some locales (10$US USD)
});

const formatAmount = currencyDisplay => {
return amount.toLocaleString(options.locale, {
style: 'currency',
currency,
minimumFractionDigits: minimumFractionDigits,
maximumFractionDigits: maximumFractionDigits,
currencyDisplay,
});
};

try {
// We manually add the exact currency (e.g. "$10 USD") in many places. This is to prevent
// showing the currency twice is some locales ($US10 USD)
return formatAmount('narrowSymbol');
} catch (e) {
// ... unfortunately, some old versions of Safari doesn't support it, so we need a fallback
return formatAmount('symbol');
}
}

export const formatValueAsCurrency = (value, options) =>
Expand Down

2 comments on commit c5800f0

@vercel
Copy link

@vercel vercel bot commented on c5800f0 Nov 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on c5800f0 Nov 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.