-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(widgets): Localize CurrencyAmounts and Prices (#3247)
* add basic number formatting * test formatLocaleNumber * localize CurrencyAmounts and Prices * use lingui locale hook * pr review * cleaner type assertions * check if locale is supported when formatting * pr feedback
- Loading branch information
1 parent
0ec2dd4
commit f95275d
Showing
10 changed files
with
167 additions
and
50 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { SUPPORTED_LOCALES, SupportedLocale } from 'constants/locales' | ||
|
||
import formatLocaleNumber from './formatLocaleNumber' | ||
|
||
const INPUT = 4000000.123 // 4 million | ||
|
||
function expectedOutput(l: SupportedLocale): string { | ||
switch (l) { | ||
case 'en-US': | ||
case 'he-IL': | ||
case 'ja-JP': | ||
case 'ko-KR': | ||
case 'zh-CN': | ||
case 'sw-TZ': | ||
case 'zh-TW': | ||
return `4,000,000.123` | ||
case 'fr-FR': | ||
return `4 000 000,123` | ||
case 'ar-SA': | ||
return `٤٬٠٠٠٬٠٠٠٫١٢٣` | ||
case 'cs-CZ': | ||
case 'fi-FI': | ||
case 'af-ZA': | ||
case 'hu-HU': | ||
case 'no-NO': | ||
case 'pl-PL': | ||
case 'pt-PT': | ||
case 'ru-RU': | ||
case 'sv-SE': | ||
case 'uk-UA': | ||
return `4 000 000,123` | ||
case 'ca-ES': | ||
case 'da-DK': | ||
case 'de-DE': | ||
case 'el-GR': | ||
case 'es-ES': | ||
case 'id-ID': | ||
case 'it-IT': | ||
case 'nl-NL': | ||
case 'pt-BR': | ||
case 'ro-RO': | ||
case 'sr-SP': | ||
case 'tr-TR': | ||
case 'vi-VN': | ||
return `4.000.000,123` | ||
default: | ||
throw new Error('unreachable') | ||
} | ||
} | ||
|
||
const TEST_MATRIX = SUPPORTED_LOCALES.map((locale) => ({ | ||
locale, | ||
input: INPUT, | ||
expected: expectedOutput(locale), | ||
})) | ||
|
||
describe('formatLocaleNumber', () => { | ||
test.concurrent.each(TEST_MATRIX)('should format correctly for %p', async ({ locale, input, expected }) => { | ||
const result = formatLocaleNumber({ number: input, locale }) | ||
expect(result).toEqual(expected) | ||
}) | ||
}) |
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,23 @@ | ||
import { Currency, CurrencyAmount, Price } from '@uniswap/sdk-core' | ||
import { DEFAULT_LOCALE, SUPPORTED_LOCALES } from 'constants/locales' | ||
|
||
interface FormatLocaleNumberArgs { | ||
number: CurrencyAmount<Currency> | Price<Currency, Currency> | number | ||
locale: string | null | undefined | ||
options?: Intl.NumberFormatOptions | ||
sigFigs?: number | ||
} | ||
|
||
export default function formatLocaleNumber({ number, locale, sigFigs, options = {} }: FormatLocaleNumberArgs): string { | ||
let localeArg: string | string[] | ||
if (!locale || (locale && !SUPPORTED_LOCALES.includes(locale))) { | ||
localeArg = DEFAULT_LOCALE | ||
} else { | ||
localeArg = [locale, DEFAULT_LOCALE] | ||
} | ||
if (typeof number === 'number') { | ||
return number.toLocaleString(localeArg, options) | ||
} else { | ||
return parseFloat(number.toSignificant(sigFigs)).toLocaleString(localeArg, options) | ||
} | ||
} |
Oops, something went wrong.