Skip to content

Commit 1c49c83

Browse files
authored
fix: update Duration plugin to support global locale (#1008)
fix #1007
1 parent d1bdd36 commit 1c49c83

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/plugin/duration/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const prettyUnit = unit => `${$u.p(unit)}s`
2828
class Duration {
2929
constructor(input, unit, locale) {
3030
this.$d = {}
31-
this.$l = locale || 'en'
31+
this.$l = locale
3232
if (unit) {
3333
return wrapper(input * unitToMS[prettyUnit(unit)], this)
3434
}
@@ -172,7 +172,8 @@ export default (option, Dayjs, dayjs) => {
172172
$d = dayjs
173173
$u = dayjs().$utils()
174174
dayjs.duration = function (input, unit) {
175-
return wrapper(input, {}, unit)
175+
const $l = dayjs.locale()
176+
return wrapper(input, { $l }, unit)
176177
}
177178
dayjs.isDuration = isDuration
178179
}

test/plugin/duration.test.js

+9
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@ describe('Humanize', () => {
101101
expect(dayjs.duration(1, 'minutes').locale('fr').humanize(true)).toBe('dans une minute')
102102
expect(dayjs.duration(1, 'minutes').locale('es').humanize(true)).toBe('en un minuto')
103103
})
104+
it('Global Locale', () => {
105+
dayjs.locale('en')
106+
expect(dayjs.duration(1, 'minutes').humanize(true)).toBe('in a minute')
107+
dayjs.locale('fr')
108+
expect(dayjs.duration(1, 'minutes').humanize(true)).toBe('dans une minute')
109+
dayjs.locale('es')
110+
expect(dayjs.duration(1, 'minutes').humanize(true)).toBe('en un minuto')
111+
dayjs.locale('en')
112+
})
104113
})
105114

106115
describe('Clone', () => {

0 commit comments

Comments
 (0)