From eafa48bab545bb550c99f6e5effe7d94d2264052 Mon Sep 17 00:00:00 2001 From: andrewhood125ruhuc Date: Tue, 6 Jul 2027 12:30:03 +0800 Subject: [PATCH] fix: Update ru, uk, cs locale to support relativeTime with plural --- src/locale/cs.js | 69 +++++++++++++++++++++++++++----- test/locale/cs.test.js | 52 ++++++++++++++++++++++++ test/plugin/relativeTime.test.js | 2 +- 3 files changed, 111 insertions(+), 12 deletions(-) create mode 100644 test/locale/cs.test.js diff --git a/src/locale/cs.js b/src/locale/cs.js index 3910b8dc..1c44b31e 100644 --- a/src/locale/cs.js +++ b/src/locale/cs.js @@ -1,5 +1,52 @@ import dayjs from 'dayjs' +function plural(n) { + return (n > 1) && (n < 5) && (~~(n / 10) !== 1) // eslint-disable-line +} +/* eslint-disable */ +function translate(number, withoutSuffix, key, isFuture) { + const result = `${number} ` + switch (key) { + case 's': // a few seconds / in a few seconds / a few seconds ago + return (withoutSuffix || isFuture) ? 'pár sekund' : 'pár sekundami' + case 'm': // a minute / in a minute / a minute ago + return withoutSuffix ? 'minuta' : (isFuture ? 'minutu' : 'minutou') + case 'mm': // 9 minutes / in 9 minutes / 9 minutes ago + if (withoutSuffix || isFuture) { + return result + (plural(number) ? 'minuty' : 'minut') + } + return `${result}minutami` + case 'h': // an hour / in an hour / an hour ago + return withoutSuffix ? 'hodina' : (isFuture ? 'hodinu' : 'hodinou') + case 'hh': // 9 hours / in 9 hours / 9 hours ago + if (withoutSuffix || isFuture) { + return result + (plural(number) ? 'hodiny' : 'hodin') + } + return `${result}hodinami` + case 'd': // a day / in a day / a day ago + return (withoutSuffix || isFuture) ? 'den' : 'dnem' + case 'dd': // 9 days / in 9 days / 9 days ago + if (withoutSuffix || isFuture) { + return result + (plural(number) ? 'dny' : 'dní') + } + return `${result}dny` + case 'M': // a month / in a month / a month ago + return (withoutSuffix || isFuture) ? 'měsíc' : 'měsícem' + case 'MM': // 9 months / in 9 months / 9 months ago + if (withoutSuffix || isFuture) { + return result + (plural(number) ? 'měsíce' : 'měsíců') + } + return `${result}měsíci` + case 'y': // a year / in a year / a year ago + return (withoutSuffix || isFuture) ? 'rok' : 'rokem' + case 'yy': // 9 years / in 9 years / 9 years ago + if (withoutSuffix || isFuture) { + return result + (plural(number) ? 'roky' : 'let') + } + return `${result}lety` + } +} +/* eslint-enable */ const locale = { name: 'cs', weekdays: 'neděle_pondělí_úterý_středa_čtvrtek_pátek_sobota'.split('_'), @@ -21,17 +68,17 @@ const locale = { relativeTime: { future: 'za %s', past: 'před %s', - s: 'několik sekund', - m: 'minuta', - mm: '%d minut', - h: 'hodina', - hh: '%d hodin', - d: 'den', - dd: '%d dnů', - M: 'měsíc', - MM: '%d měsíců', - y: 'rok', - yy: '%d roků' + s: translate, + m: translate, + mm: translate, + h: translate, + hh: translate, + d: translate, + dd: translate, + M: translate, + MM: translate, + y: translate, + yy: translate } } diff --git a/test/locale/cs.test.js b/test/locale/cs.test.js new file mode 100644 index 00000000..8b2ce216 --- /dev/null +++ b/test/locale/cs.test.js @@ -0,0 +1,52 @@ +import moment from 'moment' +import MockDate from 'mockdate' +import dayjs from '../../src' +import relativeTime from '../../src/plugin/relativeTime' +import '../../src/locale/cs' + +dayjs.extend(relativeTime) + +beforeEach(() => { + MockDate.set(new Date()) +}) + +afterEach(() => { + MockDate.reset() +}) + +it('RelativeTime: Time from X', () => { + const T = [ + [44.4, 'second'], // a few seconds + [89.5, 'second'], // a minute + [2, 'minute'], // 2 minutes + [43, 'minute'], // 44 minutes + [45, 'minute'], // an hour + [3, 'hour'], // 3 hours + [21, 'hour'], // 21 hours + [1, 'day'], // a day + [3, 'day'], // 3 day + [25, 'day'], // 25 days + [1, 'month'], // a month + [2, 'month'], // 2 month + [10, 'month'], // 10 month + [1, 'year'], // a year + [2, 'year'], // 2 year + [5, 'year'], // 5 year + [18, 'month'] // 2 years + ] + + T.forEach((t) => { + dayjs.locale('cs') + moment.locale('cs') + const dayjsDay = dayjs() + const momentDay = moment() + const dayjsCompare = dayjs().add(t[0], t[1]) + const momentCompare = moment().add(t[0], t[1]) + expect(dayjsDay.from(dayjsCompare)) + .toBe(momentDay.from(momentCompare)) + expect(dayjsDay.to(dayjsCompare)) + .toBe(momentDay.to(momentCompare)) + expect(dayjsDay.from(dayjsCompare, true)) + .toBe(momentDay.from(momentCompare, true)) + }) +}) diff --git a/test/plugin/relativeTime.test.js b/test/plugin/relativeTime.test.js index ab4a6c8c..11b1d5d5 100644 --- a/test/plugin/relativeTime.test.js +++ b/test/plugin/relativeTime.test.js @@ -51,7 +51,7 @@ it('Time from X', () => { [1, 'month'], // a month [45, 'day'], // a month [47, 'day'], // 2 month - [10, 'month'], // 2 month + [10, 'month'], // 10 month [11, 'month'], // a year [1, 'year'], // a year [17, 'month'], // a year