From 7bb1b6baac98f7b22f5ea159643e4b4951060a63 Mon Sep 17 00:00:00 2001 From: Csillag Kristof Date: Tue, 2 May 2023 13:39:00 +0200 Subject: [PATCH] Restore transactions count i18n --- src/app/components/Account/index.tsx | 2 +- src/app/hooks/useNumberFormatter.ts | 20 ++++++++++++++++---- src/app/pages/BlockDetailPage/index.tsx | 4 +++- src/locales/en/translation.json | 2 ++ 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/app/components/Account/index.tsx b/src/app/components/Account/index.tsx index 4c36b3380a..42266ef97d 100644 --- a/src/app/components/Account/index.tsx +++ b/src/app/components/Account/index.tsx @@ -110,7 +110,7 @@ export const Account: FC = ({ account, isLoading, roseFiatValue, s
{t('common.transactions')}
- {formatNumber(account.stats.num_txns)} + {formatNumber(account.stats.num_txns, { countKey: 'common.transactionsNumber' })}
diff --git a/src/app/hooks/useNumberFormatter.ts b/src/app/hooks/useNumberFormatter.ts index a5e7e165ac..f5ff74427c 100644 --- a/src/app/hooks/useNumberFormatter.ts +++ b/src/app/hooks/useNumberFormatter.ts @@ -1,4 +1,5 @@ import { BigNumber } from 'bignumber.js' +import { useTranslation } from 'react-i18next' export type NumberFormattingParameters = Partial & { // Additional features which are not natively supported by BigNumber @@ -7,16 +8,20 @@ export type NumberFormattingParameters = Partial & { maximumFractionDigits?: number roundingMode?: BigNumber.RoundingMode unit?: string + countKey?: string } -export const useFormatNumber = - () => - ( +export const useFormatNumber = () => { + const { t } = useTranslation() + return ( inputNumber: number | string | BigNumber.Instance | undefined, format: NumberFormattingParameters = {}, ): string | undefined => { if (inputNumber === undefined) return - const { decimalPlaces, maximumFractionDigits, roundingMode, unit, ...formatting } = format + const { decimalPlaces, maximumFractionDigits, roundingMode, unit, countKey, ...formatting } = format + if (!!unit && !!countKey) { + throw new Error("Please don't try to use unit and countKey together! They are incompatible.") + } let number = typeof inputNumber === 'number' ? BigNumber(inputNumber.toString(2), 2) // This is required to keep all precision @@ -40,7 +45,14 @@ export const useFormatNumber = .formatToParts(number.toNumber()) .find(p => p.type === 'unit')!.value return `${numberString} ${formattedUnit}` + } else if (countKey) { + if (number.toNumber() === 1) { + return t(countKey as any, { count: 1 }) + } else { + return t(countKey as any, { count: 2 }).replace('2', numberString) + } } else { return numberString } } +} diff --git a/src/app/pages/BlockDetailPage/index.tsx b/src/app/pages/BlockDetailPage/index.tsx index 87e8ae5752..ee9f0ced6b 100644 --- a/src/app/pages/BlockDetailPage/index.tsx +++ b/src/app/pages/BlockDetailPage/index.tsx @@ -109,7 +109,9 @@ export const BlockDetailView: FC<{
{t('common.transactions')}
- {formatNumber(block.num_transactions)} + + {formatNumber(block.num_transactions, { countKey: 'common.transactionsNumber' })} +
{t('common.gasUsed')}
diff --git a/src/locales/en/translation.json b/src/locales/en/translation.json index eaabc3f72e..b0eca78229 100644 --- a/src/locales/en/translation.json +++ b/src/locales/en/translation.json @@ -68,6 +68,8 @@ "to": "To", "totalSent": "Total Sent", "transactions": "Transactions", + "transactionsNumber_one": "1 transaction", + "transactionsNumber_other": "{{ count }} transactions", "txnFee": "Txn Fee", "type": "Type", "value": "Value",