localize filesize symbols

This commit is contained in:
Gauthier Roebroeck 2026-03-25 16:17:26 +08:00
parent e57a77f716
commit ef8c4072b4

View file

@ -1,8 +1,40 @@
import { partial } from 'filesize'
import { currentLocale } from '@/utils/i18n/locale-helper'
export const delay = (ms: number) => new Promise((res) => setTimeout(res, ms))
const filesizePartial = partial({ round: 1 })
// Query Intl for the unit symbol of a given unit (byte, kilobyte, etc.)
function getUnitSymbol(
locale: string,
unit: Intl.NumberFormatOptions['unit'],
unitDisplay: Intl.NumberFormatOptions['unitDisplay'] = 'narrow',
): string {
const formatter = new Intl.NumberFormat(locale, {
style: 'unit',
unit: unit,
unitDisplay: unitDisplay,
})
return formatter.formatToParts(1).find((it) => it.type === 'unit')!.value
}
// Map the units we care about to symbols for a given locale
function getByteSymbolsForLocale(locale: string): Record<string, string> {
return {
B: getUnitSymbol(locale, 'byte', 'narrow'),
kB: getUnitSymbol(locale, 'kilobyte', 'narrow'),
MB: getUnitSymbol(locale, 'megabyte', 'narrow'),
GB: getUnitSymbol(locale, 'gigabyte', 'narrow'),
TB: getUnitSymbol(locale, 'terabyte', 'narrow'),
PB: getUnitSymbol(locale, 'petabyte', 'narrow'),
}
}
const filesizePartial = partial({
round: 1,
locale: currentLocale,
symbols: getByteSymbolsForLocale(currentLocale),
})
export function getFileSize(n?: number): string | undefined {
if (!n) return undefined
return filesizePartial(n)