mirror of
https://github.com/gotson/komga.git
synced 2026-05-07 12:01:40 +02:00
localize filesize symbols
This commit is contained in:
parent
e57a77f716
commit
ef8c4072b4
1 changed files with 33 additions and 1 deletions
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue