Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add correct Estonian relativeTime locale for past and future tense #790

Merged
merged 4 commits into from
Jan 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 30 additions & 19 deletions src/locale/et.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
// Estonian [et]
import dayjs from 'dayjs'

function relativeTimeWithTense(number, withoutSuffix, key, isFuture) {
const format = {
s: ['mõne sekundi', 'mõni sekund', 'paar sekundit'],
m: ['ühe minuti', 'üks minut'],
mm: ['%d minuti', '%d minutit'],
h: ['ühe tunni', 'tund aega', 'üks tund'],
hh: ['%d tunni', '%d tundi'],
d: ['ühe päeva', 'üks päev'],
M: ['kuu aja', 'kuu aega', 'üks kuu'],
MM: ['%d kuu', '%d kuud'],
y: ['ühe aasta', 'aasta', 'üks aasta'],
yy: ['%d aasta', '%d aastat']
}
if (withoutSuffix) {
return (format[key][2] ? format[key][2] : format[key][1]).replace('%d', number)
}
return (isFuture ? format[key][0] : format[key][1]).replace('%d', number)
}

const locale = {
name: 'et', // Estonian
weekdays: 'pühapäev_esmaspäev_teisipäev_kolmapäev_neljapäev_reede_laupäev'.split('_'), // Note weekdays are not capitalized in Estonian
Expand All @@ -10,28 +29,20 @@ const locale = {
monthsShort: 'jaan_veebr_märts_apr_mai_juuni_juuli_aug_sept_okt_nov_dets'.split('_'),
ordinal: n => `${n}.`,
weekStart: 1,
/*
* This relativeTime is currently configured for having proper past
* tense forms since Estonian needs a separate version for future tense
* and I think past tense is a more common use case for this kind of library.
*
* Doing this properly requires this issue to be fixed:
* https://github.com/iamkun/dayjs/issues/302
*/
relativeTime: {
future: '%s pärast',
past: '%s tagasi',
s: 'mõni sekund', // for past tense
m: 'minut', // for past tense
mm: '%d minutit', // for past tense
h: 'tund', // for past tense
hh: '%d tundi', // for past tense
d: 'päev', // for past tense
dd: '%d päeva', // for past tense
M: 'kuu', // for past tense
MM: '%d kuud', // for past tense
y: 'aasta', // for past tense
yy: '%d aastat' // for past tense
s: relativeTimeWithTense,
m: relativeTimeWithTense,
mm: relativeTimeWithTense,
h: relativeTimeWithTense,
hh: relativeTimeWithTense,
d: relativeTimeWithTense,
dd: '%d päeva',
M: relativeTimeWithTense,
MM: relativeTimeWithTense,
y: relativeTimeWithTense,
yy: relativeTimeWithTense
},
formats: {
LT: 'H:mm',
Expand Down
38 changes: 38 additions & 0 deletions test/locale/et.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import moment from 'moment'
import MockDate from 'mockdate'
import dayjs from '../../src'
import relativeTime from '../../src/plugin/relativeTime'
import '../../src/locale/et'

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
[43, 'minute'], // 44 minutes
[21, 'hour'], // 21 hours
[25, 'day'], // 25 days
[10, 'month'], // 2 month
[18, 'month'] // 2 years
]

T.forEach((t) => {
dayjs.locale('et')
moment.locale('et')
expect(dayjs().from(dayjs().add(t[0], t[1])))
.toBe(moment().from(moment().add(t[0], t[1])))
expect(dayjs().from(dayjs().subtract(t[0], t[1])))
.toBe(moment().from(moment().subtract(t[0], t[1])))
expect(dayjs().from(dayjs().add(t[0], t[1]), true))
.toBe(moment().from(moment().add(t[0], t[1]), true))
})
})