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

fix: de-locale for relativeTime, and added unit-tests #1109

Merged
merged 3 commits into from
Oct 13, 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
44 changes: 33 additions & 11 deletions src/locale/de.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
// German [de]
import dayjs from 'dayjs'

const texts = {
s: 'ein paar Sekunden',
m: ['eine Minute', 'einer Minute'],
mm: '%d Minuten',
h: ['eine Stunde', 'einer Stunde'],
hh: '%d Stunden',
d: ['ein Tag', 'einem Tag'],
dd: ['%d Tage', '%d Tagen'],
M: ['ein Monat', 'einem Monat'],
MM: ['%d Monate', '%d Monaten'],
y: ['ein Jahr', 'einem Jahr'],
yy: ['%d Jahre', '%d Jahren']
}

function relativeTimeFormatter(number, withoutSuffix, key) {
let l = texts[key]
if (Array.isArray(l)) {
l = l[withoutSuffix ? 0 : 1]
}
return l.replace('%d', number)
}

const locale = {
name: 'de',
weekdays: 'Sonntag_Montag_Dienstag_Mittwoch_Donnerstag_Freitag_Samstag'.split('_'),
Expand All @@ -21,17 +43,17 @@ const locale = {
relativeTime: {
future: 'in %s',
past: 'vor %s',
s: 'wenigen Sekunden',
m: 'einer Minute',
mm: '%d Minuten',
h: 'einer Stunde',
hh: '%d Stunden',
d: 'einem Tag',
dd: '%d Tagen',
M: 'einem Monat',
MM: '%d Monaten',
y: 'einem Jahr',
yy: '%d Jahren'
s: relativeTimeFormatter,
m: relativeTimeFormatter,
mm: relativeTimeFormatter,
h: relativeTimeFormatter,
hh: relativeTimeFormatter,
d: relativeTimeFormatter,
dd: relativeTimeFormatter,
M: relativeTimeFormatter,
MM: relativeTimeFormatter,
y: relativeTimeFormatter,
yy: relativeTimeFormatter
}
}

Expand Down
79 changes: 79 additions & 0 deletions test/locale/de.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import relativeTime from '../../src/plugin/relativeTime'
import '../../src/locale/de'

dayjs.extend(relativeTime)

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

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

it('German locale relative time in past and future with suffix', () => {
const cases = [
[1, 's', 'in ein paar Sekunden'],
[-1, 's', 'vor ein paar Sekunden'],
[1, 'm', 'in einer Minute'],
[-1, 'm', 'vor einer Minute'],
[1, 'h', 'in einer Stunde'],
[-1, 'h', 'vor einer Stunde'],
[1, 'd', 'in einem Tag'],
[-1, 'd', 'vor einem Tag'],
[1, 'M', 'in einem Monat'],
[-1, 'M', 'vor einem Monat'],
[2, 'd', 'in 2 Tagen'],
[-2, 'd', 'vor 2 Tagen'],
[10, 'd', 'in 10 Tagen'],
[-10, 'd', 'vor 10 Tagen'],
[6, 'm', 'in 6 Minuten'],
[-6, 'm', 'vor 6 Minuten'],
[5, 'h', 'in 5 Stunden'],
[-5, 'h', 'vor 5 Stunden'],
[3, 'M', 'in 3 Monaten'],
[-3, 'M', 'vor 3 Monaten'],
[4, 'y', 'in 4 Jahren'],
[-4, 'y', 'vor 4 Jahren']
]
cases.forEach((c) => {
expect(dayjs().add(c[0], c[1]).locale('de').fromNow())
.toBe(c[2])
expect(dayjs().add(c[0], c[1]).locale('de').fromNow())
.toBe(moment().add(c[0], c[1]).locale('de').fromNow())
})
})

it('German locale relative time in past and future without suffix', () => {
const cases = [
[1, 's', 'ein paar Sekunden'],
[-1, 's', 'ein paar Sekunden'],
[1, 'm', 'eine Minute'],
[-1, 'm', 'eine Minute'],
[1, 'h', 'eine Stunde'],
[-1, 'h', 'eine Stunde'],
[1, 'd', 'ein Tag'],
[-1, 'd', 'ein Tag'],
[2, 'd', '2 Tage'],
[-2, 'd', '2 Tage'],
[10, 'd', '10 Tage'],
[-10, 'd', '10 Tage'],
[6, 'm', '6 Minuten'],
[-6, 'm', '6 Minuten'],
[5, 'h', '5 Stunden'],
[-5, 'h', '5 Stunden'],
[3, 'M', '3 Monate'],
[-3, 'M', '3 Monate'],
[4, 'y', '4 Jahre'],
[-4, 'y', '4 Jahre']
]
cases.forEach((c) => {
expect(dayjs().add(c[0], c[1]).locale('de').fromNow(true))
.toBe(c[2])
expect(dayjs().add(c[0], c[1]).locale('de').fromNow(true))
.toBe(moment().add(c[0], c[1]).locale('de').fromNow(true))
})
})