Skip to content

Commit

Permalink
fix: Relative times in Finnish locale
Browse files Browse the repository at this point in the history
  • Loading branch information
ttsirkia committed Feb 15, 2020
1 parent 523c038 commit 8ea3b66
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 20 deletions.
61 changes: 41 additions & 20 deletions src/locale/fi.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
// Finnish [fi]
import dayjs from 'dayjs'

function relativeTimeFormatter(number, withoutSuffix, key, isFuture) {
const past = {
s: 'muutama sekunti',
m: 'minuutti',
mm: '%d minuuttia',
h: 'tunti',
hh: '%d tuntia',
d: 'päivä',
dd: '%d päivää',
M: 'kuukausi',
MM: '%d kuukautta',
y: 'vuosi',
yy: '%d vuotta'
}
const future = {
s: 'muutaman sekunnin',
m: 'minuutin',
mm: '%d minuutin',
h: 'tunnin',
hh: '%d tunnin',
d: 'päivän',
dd: '%d päivän',
M: 'kuukauden',
MM: '%d kuukauden',
y: 'vuoden',
yy: '%d vuoden'
}
return ((isFuture && !withoutSuffix) ? future : past)[key].replace('%d', number)
}

const locale = {
name: 'fi', // Finnish
weekdays: 'sunnuntai_maanantai_tiistai_keskiviikko_torstai_perjantai_lauantai'.split('_'), // Note weekdays are not capitalized in Finnish
Expand All @@ -10,29 +40,20 @@ const locale = {
monthsShort: 'tammi_helmi_maalis_huhti_touko_kesä_heinä_elo_syys_loka_marras_joulu'.split('_'),
ordinal: n => `${n}.`,
weekStart: 1,
/*
* This relativeTime is currently configured for having proper past
* tense forms since Finnish 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 kuluttua',
past: '%s sitten',
s: 'muutama sekunti', // for past tense
m: 'minuutti', // for past tense
mm: '%d minuuttia', // for past tense
h: 'tunti', // for past tense
hh: '%d tuntia', // for past tense
d: 'päivä', // for past tense
dd: '%d päivää', // for past tense
M: 'kuukausi', // for past tense
MM: '%d kuukautta', // for past tense
y: 'vuosi', // for past tense
yy: '%d vuotta' // for past tense
s: relativeTimeFormatter,
m: relativeTimeFormatter,
mm: relativeTimeFormatter,
h: relativeTimeFormatter,
hh: relativeTimeFormatter,
d: relativeTimeFormatter,
dd: relativeTimeFormatter,
M: relativeTimeFormatter,
MM: relativeTimeFormatter,
y: relativeTimeFormatter,
yy: relativeTimeFormatter
},
formats: {
LT: 'HH.mm',
Expand Down
27 changes: 27 additions & 0 deletions test/locale/fi.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import MockDate from 'mockdate'
import dayjs from '../../src'
import relativeTime from '../../src/plugin/relativeTime'
import '../../src/locale/fi'

dayjs.extend(relativeTime)

beforeEach(() => {
MockDate.set(new Date())
})

afterEach(() => {
MockDate.reset()
})

it('Finnish locale relative time in past and future', () => {
expect(dayjs().add(1, 'd').locale('fi').fromNow())
.toBe('päivän kuluttua')
expect(dayjs().add(2, 'd').locale('fi').fromNow())
.toBe('2 päivän kuluttua')
expect(dayjs().add(2, 'd').locale('fi').fromNow(true))
.toBe('2 päivää')
expect(dayjs().add(-1, 'd').locale('fi').fromNow())
.toBe('päivä sitten')
expect(dayjs().add(-2, 'd').locale('fi').fromNow())
.toBe('2 päivää sitten')
})

0 comments on commit 8ea3b66

Please sign in to comment.