Skip to content

Commit

Permalink
format large numbers per locale (github#34579)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbe authored and vgrl committed Feb 8, 2023
1 parent f90f604 commit cd058f2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
10 changes: 10 additions & 0 deletions components/hooks/useNumberFormatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { useRouter } from 'next/router'

export function useNumberFormatter() {
const { locale } = useRouter()
return {
formatInteger: (num: number) => {
return new Intl.NumberFormat(locale).format(num)
},
}
}
4 changes: 3 additions & 1 deletion components/search/SearchResults.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import cx from 'classnames'

import type { SearchResultsT, SearchResultHitT } from './types'
import { useTranslation } from 'components/hooks/useTranslation'
import { useNumberFormatter } from 'components/hooks/useNumberFormatter'
import { Link } from 'components/Link'
import { useQuery } from 'components/hooks/useQuery'
import { sendEvent, EventType } from 'components/lib/events'
Expand All @@ -17,6 +18,7 @@ type Props = {
}
export function SearchResults({ results, query }: Props) {
const { t } = useTranslation('search')
const { formatInteger } = useNumberFormatter()

const pages = Math.ceil(results.meta.found.value / results.meta.size)
const { page } = results.meta
Expand All @@ -26,7 +28,7 @@ export function SearchResults({ results, query }: Props) {
<Text>
{results.meta.found.value === 1
? t('one_result')
: t('n_results').replace('{n}', results.meta.found.value.toLocaleString())}
: t('n_results').replace('{n}', formatInteger(results.meta.found.value))}
</Text>
<br />
<SearchResultHits hits={results.hits} query={query} />
Expand Down
4 changes: 3 additions & 1 deletion components/search/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Heading } from '@primer/react'
import { sendEvent, EventType } from 'components/lib/events'
import { useTranslation } from 'components/hooks/useTranslation'
import { DEFAULT_VERSION, useVersion } from 'components/hooks/useVersion'
import { useNumberFormatter } from 'components/hooks/useNumberFormatter'
import type { SearchResultsT } from 'components/search/types'
import { SearchResults } from 'components/search/SearchResults'
import { SearchError } from 'components/search/SearchError'
Expand All @@ -17,6 +18,7 @@ import { useMainContext } from 'components/context/MainContext'

export function Search() {
const { locale } = useRouter()
const { formatInteger } = useNumberFormatter()
const { t } = useTranslation('search')
const { currentVersion } = useVersion()
const { query, debug } = useQuery()
Expand Down Expand Up @@ -74,7 +76,7 @@ export function Search() {
pageTitle += ` (${searchVersion})`
}
if (results) {
pageTitle = `${results.meta.found.value.toLocaleString()} ${pageTitle}`
pageTitle = `${formatInteger(results.meta.found.value)} ${pageTitle}`
}
}

Expand Down

0 comments on commit cd058f2

Please sign in to comment.