Update ago to respect user's language

This commit is contained in:
venashial
2022-05-31 21:03:22 -07:00
parent d841d69bd4
commit 87c7d1a5f4

View File

@@ -1,15 +1,18 @@
/**
* Human readable elapsed or remaining time (example: 3 minutes ago)
* @author https://github.com/victornpb
* @see https://stackoverflow.com/a/67338038/938822
* Human readable elapsed or remaining time (example: 3 minutes)
* modified from https://stackoverflow.com/a/67338038/938822
*/
const rft = new Intl.RelativeTimeFormat(
typeof navigator !== 'undefined' ? [...navigator.languages] : ['en'],
{ numeric: 'auto' }
)
export function ago(
/** A Date object, timestamp or string parsable with Date.parse() */
date: string | number | Date,
/** A Date object, timestamp or string parsable with Date.parse() */
nowDate: string | number | Date = Date.now(),
/** A Intl formater */
rft: Intl.RelativeTimeFormat = new Intl.RelativeTimeFormat(undefined, { numeric: 'auto' })
nowDate: string | number | Date = Date.now()
): string {
const SECOND = 1000
const MINUTE = 60 * SECOND
@@ -25,8 +28,7 @@ export function ago(
{ ge: DAY, divisor: DAY, unit: 'day' },
{ ge: HOUR, divisor: HOUR, unit: 'hour' },
{ ge: MINUTE, divisor: MINUTE, unit: 'minute' },
{ ge: 30 * SECOND, divisor: SECOND, unit: 'seconds' },
{ ge: 0, divisor: 1, text: 'just now' },
{ ge: 0, divisor: SECOND, unit: 'seconds' },
]
const now = typeof nowDate === 'object' ? nowDate.getTime() : new Date(nowDate).getTime()
const diff = now - (typeof date === 'object' ? date : new Date(date)).getTime()
@@ -35,80 +37,7 @@ export function ago(
if (diffAbs >= interval.ge) {
const x = Math.round(Math.abs(diff) / interval.divisor)
const isFuture = diff < 0
if (
typeof navigator !== 'undefined' &&
(navigator.language === 'zh-CN' || navigator.language === 'zh')
) {
return chs_format(x, isFuture, interval.unit as Unit)
}
return interval.unit ? rft.format(isFuture ? x : -x, interval.unit as Unit) : interval.text
return rft.format(isFuture ? x : -x, interval.unit as Intl.RelativeTimeFormatUnit)
}
}
}
type Unit =
| 'second'
| 'seconds'
| 'minute'
| 'minutes'
| 'hour'
| 'hours'
| 'day'
| 'days'
| 'week'
| 'weeks'
| 'month'
| 'months'
| 'year'
| 'years'
type ChsUnit = '秒' | '分' | '小时' | '天' | '周' | '月' | '年'
/**
* Convert unit to chinese unit
* @param unit
* @returns {ChsUnit}
*/
function convertUnitToChsUnit(unit: Unit): ChsUnit {
switch (unit) {
case 'second':
case 'seconds':
return '秒'
case 'minute':
case 'minutes':
return '分'
case 'hour':
case 'hours':
return '小时'
case 'day':
case 'days':
return '天'
case 'week':
case 'weeks':
return '周'
case 'month':
case 'months':
return '月'
case 'year':
case 'years':
return '年'
}
}
/**
* The default converter provided by js does not conform to Chinese typography.
* @param value date value
* @param isFuture
* @param unit
* @returns {string}
*/
function chs_format(value: number, isFuture: boolean, unit: Unit): string {
const chsUnit = convertUnitToChsUnit(unit)
let quantifier = ''
switch (chsUnit) {
case '月':
case '小时':
quantifier = '个'
}
return `${value} ${quantifier}${chsUnit}${isFuture ? '后' : '前'}`
}