From 1c4b0da1468522e59dc9ee646d10dd2b31477d99 Mon Sep 17 00:00:00 2001 From: zvizvi <5633910@gmail.com> Date: Sun, 27 Dec 2020 05:40:07 +0200 Subject: [PATCH] fix: update Hebrew [he] locale for double units (#1287) --- src/locale/he.js | 46 ++++++++++++++++++++++++++++++++---------- test/locale/he.test.js | 45 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 11 deletions(-) create mode 100644 test/locale/he.test.js diff --git a/src/locale/he.js b/src/locale/he.js index c2a641e65..ab837f106 100644 --- a/src/locale/he.js +++ b/src/locale/he.js @@ -1,6 +1,30 @@ // Hebrew [he] import dayjs from 'dayjs' +const texts = { + s: 'מספר שניות', + ss: '%d שניות', + m: 'דקה', + mm: '%d דקות', + h: 'שעה', + hh: '%d שעות', + hh2: 'שעתיים', + d: 'יום', + dd: '%d ימים', + dd2: 'יומיים', + M: 'חודש', + MM: '%d חודשים', + MM2: 'חודשיים', + y: 'שנה', + yy: '%d שנים', + yy2: 'שנתיים' +} + +function relativeTimeFormatter(number, withoutSuffix, key) { + const text = texts[key + (number === 2 ? '2' : '')] || texts[key] + return text.replace('%d', number) +} + const locale = { name: 'he', weekdays: 'ראשון_שני_שלישי_רביעי_חמישי_שישי_שבת'.split('_'), @@ -11,17 +35,17 @@ const locale = { relativeTime: { future: 'בעוד %s', past: 'לפני %s', - s: 'כמה שניות', - m: 'דקה', - mm: '%d דקות', - h: 'שעה', - hh: '%d שעות', - d: 'יום', - dd: '%d ימים', - M: 'חודש', - MM: '%d חודשים', - y: 'שנה', - yy: '%d שנים' + s: relativeTimeFormatter, + m: relativeTimeFormatter, + mm: relativeTimeFormatter, + h: relativeTimeFormatter, + hh: relativeTimeFormatter, + d: relativeTimeFormatter, + dd: relativeTimeFormatter, + M: relativeTimeFormatter, + MM: relativeTimeFormatter, + y: relativeTimeFormatter, + yy: relativeTimeFormatter }, ordinal: n => n, format: { diff --git a/test/locale/he.test.js b/test/locale/he.test.js new file mode 100644 index 000000000..e78851f09 --- /dev/null +++ b/test/locale/he.test.js @@ -0,0 +1,45 @@ +import moment from 'moment' +import dayjs from '../../src' +import relativeTime from '../../src/plugin/relativeTime' +import '../../src/locale/he' + +dayjs.extend(relativeTime) + +it('RelativeTime: Time from X', () => { + const T = [ + [44.4, 'second'], // a few seconds + [89.5, 'second'], // a minute + [2, 'minute'], // 2 minutes + [43, 'minute'], // 43 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('he') + moment.locale('he') + + 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)) + }) +})