Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions packages/ui/src/composables/format-number.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { LRUCache } from 'lru-cache'

import { injectI18n } from '../providers/i18n'
import { LOCALES } from './i18n.ts'

const formatterCache = new LRUCache<string, Intl.NumberFormat>({ max: 15 })

Expand Down Expand Up @@ -35,16 +36,13 @@ export function useCompactNumber() {
return twoDigitsCompactFormatter.format(value)
}

function formatCompactNumberPlural(value: number | bigint): string {
function formatCompactNumberPlural(value: number | bigint): number | bigint {
Copy link
Member

Choose a reason for hiding this comment

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

This return type is wrong, it needs to be string.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The formatCompactNumber function returns a compact number as a string

The formatCompactNumberPlural function is used in ICU plural messages ({(here!), plural, ...}). This plural variable is internally converted to a number for category matching (one, few, other, etc.), so it doesn't matter whether this function returns a string or a number

if (value < 10_000) {
return value.toString()
return value
}
if (value < 1_000_000) {
const oneDigitCompactFormatter = getCompactFormatter(locale.value, 1)
return oneDigitCompactFormatter.format(value)
}
const twoDigitsCompactFormatter = getCompactFormatter(locale.value, 2)
return twoDigitsCompactFormatter.format(value)
const currentLocale = locale.value
const localeDefinition = LOCALES.find((l) => l.code === currentLocale)
return localeDefinition?.compactNumberPlural ?? NaN
}

return { formatCompactNumber, formatCompactNumberPlural }
Expand Down
2 changes: 2 additions & 0 deletions packages/ui/src/composables/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface LocaleDefinition {
name: string
translatedName: MessageDescriptor
numeric?: Intl.RelativeTimeFormatNumeric
compactNumberPlural?: number
dir?: 'ltr' | 'rtl'
serverLanguageCode?: string
}
Expand Down Expand Up @@ -92,6 +93,7 @@ export const LOCALES: LocaleDefinition[] = [
code: 'fil-PH',
name: 'Filipino',
translatedName: defineMessage({ id: 'locale.fil-PH', defaultMessage: 'Filipino' }),
compactNumberPlural: 1,
serverLanguageCode: 'tl',
},
{
Expand Down