Skip to content

Commit 790cd1a

Browse files
authored
fix: Fix dayjs.locale() returns current global locale (#602)
* fix: Fix dayjs.locale() returns current global locale * update localeData plugin
1 parent 1761eba commit 790cd1a

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

src/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const isDayjs = d => d instanceof Dayjs // eslint-disable-line no-use-before-def
1010

1111
const parseLocale = (preset, object, isLocal) => {
1212
let l
13-
if (!preset) return null
13+
if (!preset) return L
1414
if (typeof preset === 'string') {
1515
if (Ls[preset]) {
1616
l = preset
@@ -66,7 +66,7 @@ const parseDate = (cfg) => {
6666

6767
class Dayjs {
6868
constructor(cfg) {
69-
this.$L = this.$L || parseLocale(cfg.locale, null, true) || L
69+
this.$L = this.$L || parseLocale(cfg.locale, null, true)
7070
this.parse(cfg) // for plugin
7171
}
7272

src/plugin/localeData/index.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default (o, c) => { // locale needed later
1+
export default (o, c, dayjs) => { // locale needed later
22
const proto = c.prototype
33
const localeData = function () {
44
return {
@@ -12,5 +12,12 @@ export default (o, c) => { // locale needed later
1212
proto.localeData = function () {
1313
return localeData.bind(this)()
1414
}
15+
16+
dayjs.localeData = () => {
17+
const localeObject = dayjs.Ls[dayjs.locale()]
18+
return {
19+
firstDayOfWeek: () => localeObject.weekStart || 0
20+
}
21+
}
1522
}
1623

test/locale.test.js

+10
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,14 @@ describe('Instance locale inheritance', () => {
110110
expect(esDayjs.add(1, 'minute').format(format))
111111
.toBe('sábado 28, Abril')
112112
})
113+
114+
it('dayjs.locale() returns locale name', () => {
115+
dayjs.locale(es)
116+
moment.locale('es')
117+
expect(dayjs.locale()).toBe(moment.locale())
118+
119+
dayjs.locale('en')
120+
moment.locale('en')
121+
expect(dayjs.locale()).toBe(moment.locale())
122+
})
113123
})

test/plugin/localeData.test.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import MockDate from 'mockdate'
22
import moment from 'moment'
33
import dayjs from '../../src'
44
import localeData from '../../src/plugin/localeData'
5+
import '../../src/locale/zh-cn'
56

67
dayjs.extend(localeData)
78

@@ -13,7 +14,7 @@ afterEach(() => {
1314
MockDate.reset()
1415
})
1516

16-
it('localeData', () => {
17+
it('instance localeData', () => {
1718
const d = dayjs()
1819
const m = moment()
1920
const dayjsLocaleData = dayjs().localeData()
@@ -24,3 +25,16 @@ it('localeData', () => {
2425
expect(dayjsLocaleData.weekdaysMin(d)).toBe(momentLocaleData.weekdaysMin(m))
2526
expect(dayjsLocaleData.weekdaysShort(d)).toBe(momentLocaleData.weekdaysShort(m))
2627
})
28+
29+
it('global localeData', () => {
30+
dayjs.locale('zh-cn')
31+
moment.locale('zh-cn')
32+
let dayjsLocaleData = dayjs.localeData()
33+
let momentLocaleData = moment.localeData()
34+
expect(dayjsLocaleData.firstDayOfWeek()).toBe(momentLocaleData.firstDayOfWeek())
35+
dayjs.locale('en')
36+
moment.locale('en')
37+
dayjsLocaleData = dayjs.localeData()
38+
momentLocaleData = moment.localeData()
39+
expect(dayjsLocaleData.firstDayOfWeek()).toBe(momentLocaleData.firstDayOfWeek())
40+
})

0 commit comments

Comments
 (0)