Skip to content

Commit

Permalink
fix: Update ru, uk, cs locale to support relativeTime with plural
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkun committed Jan 6, 2020
1 parent 4bd9250 commit 3f080f7
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 12 deletions.
69 changes: 58 additions & 11 deletions src/locale/cs.js
Original file line number Diff line number Diff line change
@@ -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('_'),
Expand All @@ -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
}
}

Expand Down
52 changes: 52 additions & 0 deletions test/locale/cs.test.js
Original file line number Diff line number Diff line change
@@ -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))
})
})
2 changes: 1 addition & 1 deletion test/plugin/relativeTime.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3f080f7

Please sign in to comment.