Merge branch 'from/develop/tusooa/unit-pl' into 'develop'

Use vue-i18n to determine plural forms of time units

Closes #1164

See merge request pleroma/pleroma-fe!1535
This commit is contained in:
HJ 2022-06-19 20:28:29 +00:00
commit da022e722e
4 changed files with 30 additions and 44 deletions

View File

@ -3,7 +3,7 @@
:datetime="time" :datetime="time"
:title="localeDateString" :title="localeDateString"
> >
{{ $t(relativeTime.key, [relativeTime.num]) }} {{ $tc(relativeTime.key, relativeTime.num, [relativeTime.num]) }}
</time> </time>
</template> </template>

View File

@ -692,38 +692,26 @@
} }
}, },
"time": { "time": {
"day": "{0} day", "unit": {
"days": "{0} days", "days": "{0} day | {0} days",
"day_short": "{0}d", "days_short": "{0}d",
"days_short": "{0}d", "hours": "{0} hour | {0} hours",
"hour": "{0} hour", "hours_short": "{0}h",
"hours": "{0} hours", "minutes": "{0} minute | {0} minutes",
"hour_short": "{0}h", "minutes_short": "{0}min",
"hours_short": "{0}h", "months": "{0} month | {0} months",
"months_short": "{0}mo",
"seconds": "{0} second | {0} seconds",
"seconds_short": "{0}s",
"weeks": "{0} week | {0} weeks",
"weeks_short": "{0}w",
"years": "{0} year | {0} years",
"years_short": "{0}y"
},
"in_future": "in {0}", "in_future": "in {0}",
"in_past": "{0} ago", "in_past": "{0} ago",
"minute": "{0} minute",
"minutes": "{0} minutes",
"minute_short": "{0}min",
"minutes_short": "{0}min",
"month": "{0} month",
"months": "{0} months",
"month_short": "{0}mo",
"months_short": "{0}mo",
"now": "just now", "now": "just now",
"now_short": "now", "now_short": "now"
"second": "{0} second",
"seconds": "{0} seconds",
"second_short": "{0}s",
"seconds_short": "{0}s",
"week": "{0} week",
"weeks": "{0} weeks",
"week_short": "{0}w",
"weeks_short": "{0}w",
"year": "{0} year",
"years": "{0} years",
"year_short": "{0}y",
"years_short": "{0}y"
}, },
"timeline": { "timeline": {
"collapse": "Collapse", "collapse": "Collapse",

View File

@ -10,31 +10,29 @@ export const relativeTime = (date, nowThreshold = 1) => {
if (typeof date === 'string') date = Date.parse(date) if (typeof date === 'string') date = Date.parse(date)
const round = Date.now() > date ? Math.floor : Math.ceil const round = Date.now() > date ? Math.floor : Math.ceil
const d = Math.abs(Date.now() - date) const d = Math.abs(Date.now() - date)
let r = { num: round(d / YEAR), key: 'time.years' } let r = { num: round(d / YEAR), key: 'time.unit.years' }
if (d < nowThreshold * SECOND) { if (d < nowThreshold * SECOND) {
r.num = 0 r.num = 0
r.key = 'time.now' r.key = 'time.now'
} else if (d < MINUTE) { } else if (d < MINUTE) {
r.num = round(d / SECOND) r.num = round(d / SECOND)
r.key = 'time.seconds' r.key = 'time.unit.seconds'
} else if (d < HOUR) { } else if (d < HOUR) {
r.num = round(d / MINUTE) r.num = round(d / MINUTE)
r.key = 'time.minutes' r.key = 'time.unit.minutes'
} else if (d < DAY) { } else if (d < DAY) {
r.num = round(d / HOUR) r.num = round(d / HOUR)
r.key = 'time.hours' r.key = 'time.unit.hours'
} else if (d < WEEK) { } else if (d < WEEK) {
r.num = round(d / DAY) r.num = round(d / DAY)
r.key = 'time.days' r.key = 'time.unit.days'
} else if (d < MONTH) { } else if (d < MONTH) {
r.num = round(d / WEEK) r.num = round(d / WEEK)
r.key = 'time.weeks' r.key = 'time.unit.weeks'
} else if (d < YEAR) { } else if (d < YEAR) {
r.num = round(d / MONTH) r.num = round(d / MONTH)
r.key = 'time.months' r.key = 'time.unit.months'
} }
// Remove plural form when singular
if (r.num === 1) r.key = r.key.slice(0, -1)
return r return r
} }

View File

@ -11,30 +11,30 @@ describe('DateUtils', () => {
it('rounds down for past', () => { it('rounds down for past', () => {
const time = Date.now() - 1.8 * DateUtils.HOUR const time = Date.now() - 1.8 * DateUtils.HOUR
expect(DateUtils.relativeTime(time)).to.eql({ num: 1, key: 'time.hour' }) expect(DateUtils.relativeTime(time)).to.eql({ num: 1, key: 'time.unit.hours' })
}) })
it('rounds up for future', () => { it('rounds up for future', () => {
const time = Date.now() + 1.8 * DateUtils.HOUR const time = Date.now() + 1.8 * DateUtils.HOUR
expect(DateUtils.relativeTime(time)).to.eql({ num: 2, key: 'time.hours' }) expect(DateUtils.relativeTime(time)).to.eql({ num: 2, key: 'time.unit.hours' })
}) })
it('uses plural when necessary', () => { it('uses plural when necessary', () => {
const time = Date.now() - 3.8 * DateUtils.WEEK const time = Date.now() - 3.8 * DateUtils.WEEK
expect(DateUtils.relativeTime(time)).to.eql({ num: 3, key: 'time.weeks' }) expect(DateUtils.relativeTime(time)).to.eql({ num: 3, key: 'time.unit.weeks' })
}) })
it('works with date string', () => { it('works with date string', () => {
const time = Date.now() - 4 * DateUtils.MONTH const time = Date.now() - 4 * DateUtils.MONTH
const dateString = new Date(time).toISOString() const dateString = new Date(time).toISOString()
expect(DateUtils.relativeTime(dateString)).to.eql({ num: 4, key: 'time.months' }) expect(DateUtils.relativeTime(dateString)).to.eql({ num: 4, key: 'time.unit.months' })
}) })
}) })
describe('relativeTimeShort', () => { describe('relativeTimeShort', () => {
it('returns the short version of the same relative time', () => { it('returns the short version of the same relative time', () => {
const time = Date.now() + 2 * DateUtils.YEAR const time = Date.now() + 2 * DateUtils.YEAR
expect(DateUtils.relativeTimeShort(time)).to.eql({ num: 2, key: 'time.years_short' }) expect(DateUtils.relativeTimeShort(time)).to.eql({ num: 2, key: 'time.unit.years_short' })
}) })
}) })
}) })