github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/static_source/admin/src/hooks/web/useTimeAgo.ts (about) 1 import { useTimeAgo as useTimeAgoCore, UseTimeAgoMessages } from '@vueuse/core' 2 import { computed, unref } from 'vue' 3 import { useLocaleStoreWithOut } from '@/store/modules/locale' 4 5 const TIME_AGO_MESSAGE_MAP: { 6 'zh-CN': UseTimeAgoMessages 7 en: UseTimeAgoMessages 8 } = { 9 'zh-CN': { 10 justNow: '刚刚', 11 invalid: '无效时间', 12 past: (n) => (n.match(/\d/) ? `${n}前` : n), 13 future: (n) => (n.match(/\d/) ? `${n}后` : n), 14 month: (n, past) => (n === 1 ? (past ? '上个月' : '下个月') : `${n} 个月`), 15 year: (n, past) => (n === 1 ? (past ? '去年' : '明年') : `${n} 年`), 16 day: (n, past) => (n === 1 ? (past ? '昨天' : '明天') : `${n} 天`), 17 week: (n, past) => (n === 1 ? (past ? '上周' : '下周') : `${n} 周`), 18 hour: (n) => `${n} 小时`, 19 minute: (n) => `${n} 分钟`, 20 second: (n) => `${n} 秒` 21 }, 22 en: { 23 justNow: '刚刚', 24 invalid: 'Invalid Date', 25 past: (n) => (n.match(/\d/) ? `${n} ago` : n), 26 future: (n) => (n.match(/\d/) ? `in ${n}` : n), 27 month: (n, past) => 28 n === 1 ? (past ? 'last month' : 'next month') : `${n} month${n > 1 ? 's' : ''}`, 29 year: (n, past) => 30 n === 1 ? (past ? 'last year' : 'next year') : `${n} year${n > 1 ? 's' : ''}`, 31 day: (n, past) => (n === 1 ? (past ? 'yesterday' : 'tomorrow') : `${n} day${n > 1 ? 's' : ''}`), 32 week: (n, past) => 33 n === 1 ? (past ? 'last week' : 'next week') : `${n} week${n > 1 ? 's' : ''}`, 34 hour: (n) => `${n} hour${n > 1 ? 's' : ''}`, 35 minute: (n) => `${n} minute${n > 1 ? 's' : ''}`, 36 second: (n) => `${n} second${n > 1 ? 's' : ''}` 37 } 38 } 39 40 export const useTimeAgo = (time: Date | number | string) => { 41 const localeStore = useLocaleStoreWithOut() 42 43 const currentLocale = computed(() => localeStore.getCurrentLocale) 44 45 const timeAgo = useTimeAgoCore(time, { 46 messages: TIME_AGO_MESSAGE_MAP[unref(currentLocale).lang] 47 }) 48 49 return timeAgo 50 }