From 37ab09f3730000d565b7c064e51ed5abd7bf818a Mon Sep 17 00:00:00 2001 From: iamkun Date: Thu, 7 Mar 2019 18:45:08 +0800 Subject: [PATCH] chore: Add locale --- src/plugin/badMutable/index.js | 7 +++++++ test/plugin/badMutable.test.js | 13 +++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/plugin/badMutable/index.js b/src/plugin/badMutable/index.js index d8c362400..6d28124b4 100644 --- a/src/plugin/badMutable/index.js +++ b/src/plugin/badMutable/index.js @@ -22,5 +22,12 @@ export default (o, c) => { // locale needed later this.init() return this } + + const oldLocale = proto.locale + proto.locale = function (preset, object) { + if (!preset) return this.$L + this.$L = oldLocale.bind(this)(preset, object).$L + return this + } } diff --git a/test/plugin/badMutable.test.js b/test/plugin/badMutable.test.js index e2413df84..be9333251 100644 --- a/test/plugin/badMutable.test.js +++ b/test/plugin/badMutable.test.js @@ -2,6 +2,7 @@ import MockDate from 'mockdate' import moment from 'moment' import dayjs from '../../src' import badMutable from '../../src/plugin/badMutable' +import '../../src/locale/zh-cn' dayjs.extend(badMutable) @@ -148,3 +149,15 @@ it('daysInMonth', () => { expect(d.daysInMonth()).toBe(m.daysInMonth()) expect(d.format()).toBe(m.format()) }) + +it('Locale', () => { + const d = dayjs() + const m = moment() + const format = 'MMMM' + expect(d.locale()).toBe(m.locale()) + expect(d.format(format)).toBe(m.format(format)) + d.locale('zh-cn') + m.locale('zh-cn') + expect(d.locale()).toBe(m.locale()) + expect(d.format(format)).toBe(m.format(format)) +})