elk/composables/i18n.ts

92 lines
2.7 KiB
TypeScript
Raw Normal View History

2023-01-02 21:19:36 +01:00
import type { MaybeComputedRef, MaybeRef, UseTimeAgoOptions } from '@vueuse/core'
2023-01-09 21:20:26 +01:00
import { locale } from '~~/config/i18n'
2022-12-02 09:52:00 +01:00
const formatter = Intl.NumberFormat()
export function formattedNumber(num: number, useFormatter: Intl.NumberFormat = formatter) {
2022-12-02 09:52:00 +01:00
return useFormatter.format(num)
}
2023-01-09 21:20:26 +01:00
const numberFormats = {
percentage: {
style: 'percent',
maximumFractionDigits: 1,
},
smallCounting: {
style: 'decimal',
maximumFractionDigits: 0,
},
bigCounting: {
notation: 'compact',
compactDisplay: 'short',
maximumFractionDigits: 0,
},
} as const
2023-01-09 21:20:26 +01:00
function format(number: MaybeRef<number>, format: keyof typeof numberFormats) {
return new Intl.NumberFormat(locale.value, numberFormats[format]).format(unref(number))
}
2023-01-09 21:20:26 +01:00
export function useHumanReadableNumber() {
2022-12-02 09:52:00 +01:00
return {
2023-01-09 21:20:26 +01:00
formatHumanReadableNumber: (num: MaybeRef<number>) => format(num, unref(num) < 10000 ? 'smallCounting' : 'bigCounting'),
formatNumber: (num: MaybeRef<number>) => format(num, 'smallCounting'),
formatPercentage: (num: MaybeRef<number>) => format(num, 'percentage'),
forSR: (num: MaybeRef<number>) => unref(num) > 10000,
2022-12-02 09:52:00 +01:00
}
}
export function useFormattedDateTime(value: MaybeComputedRef<string | number | Date | undefined | null>,
options: Intl.DateTimeFormatOptions = { dateStyle: 'long', timeStyle: 'medium' }) {
const formatter = $computed(() => Intl.DateTimeFormat(locale.value, options))
return computed(() => {
const v = resolveUnref(value)
return v ? formatter.format(new Date(v)) : ''
})
}
2022-11-26 06:05:44 +01:00
export function useTimeAgoOptions(short = false): UseTimeAgoOptions<false> {
2023-01-09 21:20:26 +01:00
const { $t } = useFluent()
2022-12-02 09:16:06 +01:00
const prefix = short ? 'short_' : ''
2022-12-02 03:18:36 +01:00
const fn = (n: number, past: boolean, key: string) => {
2023-01-09 21:20:26 +01:00
return $t(`time_ago_options_${prefix}${key}_${past ? 'past' : 'future'}`, { n })
}
2022-12-02 03:18:36 +01:00
return {
rounding: 'floor',
2022-12-02 09:16:06 +01:00
showSecond: !short,
updateInterval: short ? 60000 : 1000,
2022-12-02 03:18:36 +01:00
messages: {
2023-01-09 21:20:26 +01:00
justNow: $t('time_ago_options_just_now'),
2022-12-02 03:18:36 +01:00
// just return the value
past: n => n,
// just return the value
future: n => n,
second: (n, p) => fn(n, p, 'second'),
minute: (n, p) => fn(n, p, 'minute'),
hour: (n, p) => fn(n, p, 'hour'),
day: (n, p) => fn(n, p, 'day'),
week: (n, p) => fn(n, p, 'week'),
month: (n, p) => fn(n, p, 'month'),
year: (n, p) => fn(n, p, 'year'),
2022-12-20 14:27:53 +01:00
invalid: '',
2022-12-02 03:18:36 +01:00
},
fullDateFormatter(date) {
2023-01-09 21:20:26 +01:00
const options: Intl.DateTimeFormatOptions = short
? {
dateStyle: 'short',
timeStyle: 'short',
}
: {
dateStyle: 'long',
timeStyle: 'medium',
}
const intl = new Intl.DateTimeFormat(locale.value, options)
return intl.format(date)
2022-12-02 03:18:36 +01:00
},
}
2022-11-26 06:05:44 +01:00
}