Skip to content

Commit 3f080f7

Browse files
committed
fix: Update ru, uk, cs locale to support relativeTime with plural
1 parent 4bd9250 commit 3f080f7

File tree

3 files changed

+111
-12
lines changed

3 files changed

+111
-12
lines changed

src/locale/cs.js

+58-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,52 @@
11
import dayjs from 'dayjs'
22

3+
function plural(n) {
4+
return (n > 1) && (n < 5) && (~~(n / 10) !== 1) // eslint-disable-line
5+
}
6+
/* eslint-disable */
7+
function translate(number, withoutSuffix, key, isFuture) {
8+
const result = `${number} `
9+
switch (key) {
10+
case 's': // a few seconds / in a few seconds / a few seconds ago
11+
return (withoutSuffix || isFuture) ? 'pár sekund' : 'pár sekundami'
12+
case 'm': // a minute / in a minute / a minute ago
13+
return withoutSuffix ? 'minuta' : (isFuture ? 'minutu' : 'minutou')
14+
case 'mm': // 9 minutes / in 9 minutes / 9 minutes ago
15+
if (withoutSuffix || isFuture) {
16+
return result + (plural(number) ? 'minuty' : 'minut')
17+
}
18+
return `${result}minutami`
19+
case 'h': // an hour / in an hour / an hour ago
20+
return withoutSuffix ? 'hodina' : (isFuture ? 'hodinu' : 'hodinou')
21+
case 'hh': // 9 hours / in 9 hours / 9 hours ago
22+
if (withoutSuffix || isFuture) {
23+
return result + (plural(number) ? 'hodiny' : 'hodin')
24+
}
25+
return `${result}hodinami`
26+
case 'd': // a day / in a day / a day ago
27+
return (withoutSuffix || isFuture) ? 'den' : 'dnem'
28+
case 'dd': // 9 days / in 9 days / 9 days ago
29+
if (withoutSuffix || isFuture) {
30+
return result + (plural(number) ? 'dny' : 'dní')
31+
}
32+
return `${result}dny`
33+
case 'M': // a month / in a month / a month ago
34+
return (withoutSuffix || isFuture) ? 'měsíc' : 'měsícem'
35+
case 'MM': // 9 months / in 9 months / 9 months ago
36+
if (withoutSuffix || isFuture) {
37+
return result + (plural(number) ? 'měsíce' : 'měsíců')
38+
}
39+
return `${result}měsíci`
40+
case 'y': // a year / in a year / a year ago
41+
return (withoutSuffix || isFuture) ? 'rok' : 'rokem'
42+
case 'yy': // 9 years / in 9 years / 9 years ago
43+
if (withoutSuffix || isFuture) {
44+
return result + (plural(number) ? 'roky' : 'let')
45+
}
46+
return `${result}lety`
47+
}
48+
}
49+
/* eslint-enable */
350
const locale = {
451
name: 'cs',
552
weekdays: 'neděle_pondělí_úterý_středa_čtvrtek_pátek_sobota'.split('_'),
@@ -21,17 +68,17 @@ const locale = {
2168
relativeTime: {
2269
future: 'za %s',
2370
past: 'před %s',
24-
s: 'několik sekund',
25-
m: 'minuta',
26-
mm: '%d minut',
27-
h: 'hodina',
28-
hh: '%d hodin',
29-
d: 'den',
30-
dd: '%d dnů',
31-
M: 'měsíc',
32-
MM: '%d měsíců',
33-
y: 'rok',
34-
yy: '%d roků'
71+
s: translate,
72+
m: translate,
73+
mm: translate,
74+
h: translate,
75+
hh: translate,
76+
d: translate,
77+
dd: translate,
78+
M: translate,
79+
MM: translate,
80+
y: translate,
81+
yy: translate
3582
}
3683
}
3784

test/locale/cs.test.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import moment from 'moment'
2+
import MockDate from 'mockdate'
3+
import dayjs from '../../src'
4+
import relativeTime from '../../src/plugin/relativeTime'
5+
import '../../src/locale/cs'
6+
7+
dayjs.extend(relativeTime)
8+
9+
beforeEach(() => {
10+
MockDate.set(new Date())
11+
})
12+
13+
afterEach(() => {
14+
MockDate.reset()
15+
})
16+
17+
it('RelativeTime: Time from X', () => {
18+
const T = [
19+
[44.4, 'second'], // a few seconds
20+
[89.5, 'second'], // a minute
21+
[2, 'minute'], // 2 minutes
22+
[43, 'minute'], // 44 minutes
23+
[45, 'minute'], // an hour
24+
[3, 'hour'], // 3 hours
25+
[21, 'hour'], // 21 hours
26+
[1, 'day'], // a day
27+
[3, 'day'], // 3 day
28+
[25, 'day'], // 25 days
29+
[1, 'month'], // a month
30+
[2, 'month'], // 2 month
31+
[10, 'month'], // 10 month
32+
[1, 'year'], // a year
33+
[2, 'year'], // 2 year
34+
[5, 'year'], // 5 year
35+
[18, 'month'] // 2 years
36+
]
37+
38+
T.forEach((t) => {
39+
dayjs.locale('cs')
40+
moment.locale('cs')
41+
const dayjsDay = dayjs()
42+
const momentDay = moment()
43+
const dayjsCompare = dayjs().add(t[0], t[1])
44+
const momentCompare = moment().add(t[0], t[1])
45+
expect(dayjsDay.from(dayjsCompare))
46+
.toBe(momentDay.from(momentCompare))
47+
expect(dayjsDay.to(dayjsCompare))
48+
.toBe(momentDay.to(momentCompare))
49+
expect(dayjsDay.from(dayjsCompare, true))
50+
.toBe(momentDay.from(momentCompare, true))
51+
})
52+
})

test/plugin/relativeTime.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ it('Time from X', () => {
5151
[1, 'month'], // a month
5252
[45, 'day'], // a month
5353
[47, 'day'], // 2 month
54-
[10, 'month'], // 2 month
54+
[10, 'month'], // 10 month
5555
[11, 'month'], // a year
5656
[1, 'year'], // a year
5757
[17, 'month'], // a year

0 commit comments

Comments
 (0)