diff --git a/src/index.js b/src/index.js index 4f96f9c9..413b5bec 100644 --- a/src/index.js +++ b/src/index.js @@ -346,6 +346,7 @@ class Dayjs { } locale(preset, object) { + if (!preset) return this.$L const that = this.clone() that.$L = parseLocale(preset, object, true) return that diff --git a/test/locale.test.js b/test/locale.test.js index 06560b6b..8438bd09 100644 --- a/test/locale.test.js +++ b/test/locale.test.js @@ -1,4 +1,5 @@ import MockDate from 'mockdate' +import moment from 'moment' import dayjs from '../src' import es from '../src/locale/es' @@ -44,6 +45,17 @@ it('set global locale', () => { .toBe('Saturday 28, April') }) +it('get instance locale name', () => { + expect(dayjs().locale()).toBe('en') + expect(dayjs().locale()).toBe(moment().locale()) + expect(dayjs().locale('es').locale()).toBe('es') + expect(dayjs().locale('es').locale()).toBe(moment().locale('es').locale()) + dayjs.locale(es) + moment.locale('es') + expect(dayjs().locale()).toBe('es') + expect(dayjs().locale()).toBe(moment().locale()) +}) + it('immutable instance locale', () => { dayjs.locale('en') const origin = dayjs('2018-4-28') diff --git a/types/index.d.ts b/types/index.d.ts index 22ea1172..d6f547d2 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -85,6 +85,8 @@ declare namespace dayjs { isAfter(date: ConfigType, unit?: OpUnitType): boolean + locale(): string + locale(preset: string | { name: string, [key: string]: any }, object?: { [key: string]: any }): Dayjs }