Skip to content

Commit

Permalink
fix: update Duration plugin to support global locale (#1008)
Browse files Browse the repository at this point in the history
fix #1007
  • Loading branch information
iamkun authored Aug 18, 2020
1 parent d1bdd36 commit 1c49c83
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/plugin/duration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const prettyUnit = unit => `${$u.p(unit)}s`
class Duration {
constructor(input, unit, locale) {
this.$d = {}
this.$l = locale || 'en'
this.$l = locale
if (unit) {
return wrapper(input * unitToMS[prettyUnit(unit)], this)
}
Expand Down Expand Up @@ -172,7 +172,8 @@ export default (option, Dayjs, dayjs) => {
$d = dayjs
$u = dayjs().$utils()
dayjs.duration = function (input, unit) {
return wrapper(input, {}, unit)
const $l = dayjs.locale()
return wrapper(input, { $l }, unit)
}
dayjs.isDuration = isDuration
}
9 changes: 9 additions & 0 deletions test/plugin/duration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ describe('Humanize', () => {
expect(dayjs.duration(1, 'minutes').locale('fr').humanize(true)).toBe('dans une minute')
expect(dayjs.duration(1, 'minutes').locale('es').humanize(true)).toBe('en un minuto')
})
it('Global Locale', () => {
dayjs.locale('en')
expect(dayjs.duration(1, 'minutes').humanize(true)).toBe('in a minute')
dayjs.locale('fr')
expect(dayjs.duration(1, 'minutes').humanize(true)).toBe('dans une minute')
dayjs.locale('es')
expect(dayjs.duration(1, 'minutes').humanize(true)).toBe('en un minuto')
dayjs.locale('en')
})
})

describe('Clone', () => {
Expand Down

0 comments on commit 1c49c83

Please sign in to comment.