Created
March 22, 2023 15:08
-
-
Save Avi-E-Koenig/863ee4cf53b5f5787eabf41011cccb98 to your computer and use it in GitHub Desktop.
useTimeAgo composable for Vue3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import AppLogger from '@/services/AppLogger' | |
import type { UseTimeAgoMessages, UseTimeAgoUnitNamesDefault } from '@vueuse/core' | |
import { useI18n } from 'vue-i18n' | |
import { useTimeAgo } from '@vueuse/core' | |
const messages = { | |
en: { | |
common: { | |
timeAgo: { | |
'just-now': 'just now', | |
ago: '{0} ago', | |
in: 'in {0}', | |
'last-month': 'last month', | |
'next-month': 'next month', | |
month: 'month | months', | |
'last-year': 'last year', | |
'next-year': 'next year', | |
year: 'year | years', | |
yesterday: 'yesterday', | |
tomorrow: 'tomorrow', | |
day: 'day | days', | |
'last-week': 'last week', | |
'next-week': 'next week', | |
week: 'week | weeks', | |
hour: 'hour | hours', | |
minute: 'minute | minutes', | |
second: 'second | seconds' | |
} | |
} | |
}, | |
fr: { | |
common: { | |
timeAgo: { | |
'just-now': 'juste maintenant', | |
ago: "il y'a {0}", | |
in: 'dans {0}', | |
'last-month': 'le mois dernier', | |
'next-month': 'le mois prochain', | |
month: 'mois | mois', | |
'last-year': 'l’année dernière', | |
'next-year': 'l’année prochaine', | |
year: 'an | ans', | |
yesterday: 'hier', | |
tomorrow: 'demain', | |
day: 'jour | jours', | |
'last-week': 'la semaine dernière', | |
'next-week': 'la semaine prochaine', | |
week: 'semaine | semaines', | |
hour: 'heure | heures', | |
minute: 'minute | minutes', | |
second: 'seconde | secondes' | |
} | |
} | |
}, | |
he: { | |
common: { | |
timeAgo: { | |
'just-now': 'ממש עכשיו', | |
ago: 'לפני {0}', | |
in: 'בעוד {0}', | |
'last-month': 'החודש האחרון', | |
'next-month': 'החודש הבא', | |
month: 'חודש | חודשים', | |
'last-year': 'השנה שעברה', | |
'next-year': 'השנה הבאה', | |
year: 'שנה | שנים', | |
yesterday: 'אתמול', | |
tomorrow: 'מחר', | |
day: 'יום | ימים', | |
'last-week': 'השבוע האחרון', | |
'next-week': 'השבוע הבא', | |
week: 'שבוע | שבועות', | |
hour: 'שעה | שעות', | |
minute: 'דקה | דקות', | |
second: 'שנייה | שניות' | |
} | |
} | |
}, | |
ar: { | |
common: { | |
timeAgo: { | |
'just-now': 'الآن فقط', | |
ago: 'منذ {0}', | |
in: 'في {0}', | |
'last-month': 'الشهر الماضي', | |
'next-month': 'الشهر القادم', | |
month: 'شهر | أشهر', | |
'last-year': 'العام الماضي', | |
'next-year': 'العام القادم', | |
year: 'سنة | سنوات', | |
yesterday: 'أمس', | |
tomorrow: 'غداً', | |
day: 'يوم | أيام', | |
'last-week': 'الأسبوع الماضي', | |
'next-week': 'الأسبوع القادم', | |
week: 'أسبوع | أسابيع', | |
hour: 'ساعة | ساعات', | |
minute: 'دقيقة | دقائق', | |
second: 'ثانية | ثواني' | |
} | |
} | |
} | |
} | |
export default function useLocaleTimeAgo() { | |
const { t } = useI18n({ messages }) | |
const I18N_MESSAGES: UseTimeAgoMessages<UseTimeAgoUnitNamesDefault> = { | |
justNow: t('common.timeAgo.just-now'), | |
past: (n) => (n.match(/\d/) ? t('common.timeAgo.ago', [n]) : n), | |
future: (n) => (n.match(/\d/) ? t('common.timeAgo.in', [n]) : n), | |
month: (n, past) => | |
n === 1 ? (past ? t('common.timeAgo.last-month') : t('common.timeAgo.next-month')) : `${n} ${t(`common.timeAgo.month`, n)}`, | |
year: (n, past) => | |
n === 1 ? (past ? t('common.timeAgo.last-year') : t('common.timeAgo.next-year')) : `${n} ${t(`common.timeAgo.year`, n)}`, | |
day: (n, past) => | |
n === 1 ? (past ? t('common.timeAgo.yesterday') : t('common.timeAgo.tomorrow')) : `${n} ${t(`common.timeAgo.day`, n)}`, | |
week: (n, past) => | |
n === 1 ? (past ? t('common.timeAgo.last-week') : t('common.timeAgo.next-week')) : `${n} ${t(`common.timeAgo.week`, n)}`, | |
hour: (n) => `${n} ${t('common.timeAgo.hour', n)}`, | |
minute: (n) => `${n} ${t('common.timeAgo.minute', n)}`, | |
second: (n) => `${n} ${t(`common.timeAgo.second`, n)}`, | |
invalid: '' | |
} | |
return function (date: Date | string) { | |
try { | |
if (typeof date === 'string') { | |
date = new Date(date) | |
} | |
return useTimeAgo(date, { | |
fullDateFormatter: (date: Date) => date.toLocaleDateString(), | |
messages: I18N_MESSAGES | |
}) | |
} catch (error: any) { | |
AppLogger.error(error) | |
return date | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment