From a66daa95c8665eb3408b75696820231327f05527 Mon Sep 17 00:00:00 2001 From: iamkun Date: Mon, 6 Jan 2020 14:55:05 +0800 Subject: [PATCH 1/4] fix: Add UpdateLocale plugin to update a locale's properties --- src/plugin/updateLocale/index.js | 12 ++++++ test/plugin/updateLocale.test.js | 64 ++++++++++++++++++++++++++++++++ types/plugin/minMax.d.ts | 2 +- types/plugin/updateLocale.d.ts | 8 ++++ 4 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 src/plugin/updateLocale/index.js create mode 100644 test/plugin/updateLocale.test.js create mode 100644 types/plugin/updateLocale.d.ts diff --git a/src/plugin/updateLocale/index.js b/src/plugin/updateLocale/index.js new file mode 100644 index 000000000..8c3a2e9a2 --- /dev/null +++ b/src/plugin/updateLocale/index.js @@ -0,0 +1,12 @@ +export default (option, Dayjs, dayjs) => { + dayjs.updateLocale = function (locale, customConfig) { + const localeList = dayjs.Ls + const localeConfig = localeList[locale] + const customConfigKeys = Object.keys(customConfig) + if (!localeConfig || customConfigKeys.length === 0) return + customConfigKeys.forEach((c) => { + localeConfig[c] = customConfig[c] + }) + } +} + diff --git a/test/plugin/updateLocale.test.js b/test/plugin/updateLocale.test.js new file mode 100644 index 000000000..d1b06f3ad --- /dev/null +++ b/test/plugin/updateLocale.test.js @@ -0,0 +1,64 @@ +import MockDate from 'mockdate' +import moment from 'moment' +import dayjs from '../../src' +import updateLocale from '../../src/plugin/updateLocale' +import localizedFormat from '../../src/plugin/localizedFormat' +import '../../src/locale/zh-cn' + +dayjs.extend(updateLocale) +dayjs.extend(localizedFormat) + +beforeEach(() => { + MockDate.set(new Date()) +}) + +afterEach(() => { + MockDate.reset() +}) + +const newLocale = { + months: new Array(12).fill('testMonth'), + formats: { // formats for dayjs and longDateFormat for momentjs + LT: '[testFormat]' + }, + longDateFormat: { + LT: '[testFormat]' + } +} + +const formatString = 'MMMM LT' + +describe('Update locale', () => { + it('Invalid argument', () => { + dayjs.updateLocale('InvalidLocaleName', {}) + dayjs.updateLocale('en', { + invalidConfig: [] + }) + + expect(dayjs().format(formatString)) + .toEqual(moment().format(formatString)) + }) + + it('Update build-in en locale', () => { + moment.updateLocale('en', newLocale) + dayjs.updateLocale('en', newLocale) + + expect(dayjs().format(formatString)) + .toEqual('testMonth testFormat') + + expect(dayjs().format(formatString)) + .toEqual(moment().format(formatString)) + }) + + it('Update imported zh-cn locale', () => { + moment.updateLocale('zh-cn', newLocale) + dayjs.updateLocale('zh-cn', newLocale) + dayjs.locale('zh-cn') + moment.locale('zh-cn') + expect(dayjs().format(formatString)) + .toEqual('testMonth testFormat') + + expect(dayjs().format(formatString)) + .toEqual(moment().format(formatString)) + }) +}) diff --git a/types/plugin/minMax.d.ts b/types/plugin/minMax.d.ts index 1cca06ef2..f1673500c 100644 --- a/types/plugin/minMax.d.ts +++ b/types/plugin/minMax.d.ts @@ -1,4 +1,4 @@ -import { PluginFunc, ConfigType } from 'dayjs' +import { PluginFunc } from 'dayjs' declare const plugin: PluginFunc export = plugin diff --git a/types/plugin/updateLocale.d.ts b/types/plugin/updateLocale.d.ts new file mode 100644 index 000000000..d07f10cf3 --- /dev/null +++ b/types/plugin/updateLocale.d.ts @@ -0,0 +1,8 @@ +import { PluginFunc } from 'dayjs' + +declare const plugin: PluginFunc +export = plugin + +declare module 'dayjs' { + export function updateLocale(localeName: String, customConfig: Object): void +} From 511ea61c1835c20bd10249ebee3a58c332e8041f Mon Sep 17 00:00:00 2001 From: iamkun Date: Mon, 6 Jan 2020 15:09:10 +0800 Subject: [PATCH 2/4] chore: Update plugin logic --- src/plugin/updateLocale/index.js | 5 +++-- test/plugin/updateLocale.test.js | 17 ++++++++++++----- types/plugin/updateLocale.d.ts | 2 +- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/plugin/updateLocale/index.js b/src/plugin/updateLocale/index.js index 8c3a2e9a2..7126f7e1f 100644 --- a/src/plugin/updateLocale/index.js +++ b/src/plugin/updateLocale/index.js @@ -2,11 +2,12 @@ export default (option, Dayjs, dayjs) => { dayjs.updateLocale = function (locale, customConfig) { const localeList = dayjs.Ls const localeConfig = localeList[locale] - const customConfigKeys = Object.keys(customConfig) - if (!localeConfig || customConfigKeys.length === 0) return + if (!localeConfig) return + const customConfigKeys = customConfig ? Object.keys(customConfig) : [] customConfigKeys.forEach((c) => { localeConfig[c] = customConfig[c] }) + return localeConfig // eslint-disable-line consistent-return } } diff --git a/test/plugin/updateLocale.test.js b/test/plugin/updateLocale.test.js index d1b06f3ad..ecbcbd41c 100644 --- a/test/plugin/updateLocale.test.js +++ b/test/plugin/updateLocale.test.js @@ -30,15 +30,22 @@ const formatString = 'MMMM LT' describe('Update locale', () => { it('Invalid argument', () => { - dayjs.updateLocale('InvalidLocaleName', {}) - dayjs.updateLocale('en', { - invalidConfig: [] - }) - + const result = dayjs.updateLocale('InvalidLocaleName', {}) + expect(result) + .toEqual(undefined) expect(dayjs().format(formatString)) .toEqual(moment().format(formatString)) }) + it('Return value', () => { + const result1 = dayjs.updateLocale('en') + expect(typeof result1).toEqual('object') + const result2 = dayjs.updateLocale('en', {}) + expect(typeof result2).toEqual('object') + const result3 = dayjs.updateLocale('en', newLocale) + expect(typeof result3).toEqual('object') + }) + it('Update build-in en locale', () => { moment.updateLocale('en', newLocale) dayjs.updateLocale('en', newLocale) diff --git a/types/plugin/updateLocale.d.ts b/types/plugin/updateLocale.d.ts index d07f10cf3..44bce470d 100644 --- a/types/plugin/updateLocale.d.ts +++ b/types/plugin/updateLocale.d.ts @@ -4,5 +4,5 @@ declare const plugin: PluginFunc export = plugin declare module 'dayjs' { - export function updateLocale(localeName: String, customConfig: Object): void + export function updateLocale(localeName: String, customConfig: Object): any } From 59da36be1a387850f90d6f355895b710d3075177 Mon Sep 17 00:00:00 2001 From: iamkun Date: Mon, 6 Jan 2020 15:11:47 +0800 Subject: [PATCH 3/4] docs: Update zhcn i18n doc --- docs/zh-cn/I18n.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/docs/zh-cn/I18n.md b/docs/zh-cn/I18n.md index ca9ebf702..f0caf1d13 100644 --- a/docs/zh-cn/I18n.md +++ b/docs/zh-cn/I18n.md @@ -93,6 +93,20 @@ const localeObject = { months: 'Enero_Febrero ... '.split('_'), // 月份 Array monthsShort: 'Jan_F'.split('_'), // 可选, 短的月份 Array, 如果没提供则使用前三个字符 ordinal: n => `${n}º`, // 序号生成工厂函数 Function (number) => return number + output + formats: { + // 时间日期格式 - 长 + LTS: 'h:mm:ss A', + LT: 'h:mm A', + L: 'MM/DD/YYYY', + LL: 'MMMM D, YYYY', + LLL: 'MMMM D, YYYY h:mm A', + LLLL: 'dddd, MMMM D, YYYY h:mm A', + // 时间日期格式 - 短 + l: 'D/M/YYYY', + ll: 'D MMM, YYYY', + lll: 'D MMM, YYYY h:mm A', + llll: 'ddd, MMM D, YYYY h:mm A' + }, relativeTime: { // 相对时间, %s %d 不用翻译 future: 'in %s', // e.g. in 2 hours, %s been replaced with 2hours From 0ef81edd3408578a56e2354d5393c076f66dd93c Mon Sep 17 00:00:00 2001 From: iamkun Date: Mon, 6 Jan 2020 15:19:57 +0800 Subject: [PATCH 4/4] docs: Update UpdateLocale doc --- docs/en/API-reference.md | 6 ++++++ docs/en/I18n.md | 4 +++- docs/en/Plugin.md | 13 +++++++++++++ docs/es-es/API-reference.md | 6 ++++++ docs/es-es/I18n.md | 4 +++- docs/es-es/Plugin.md | 13 +++++++++++++ docs/ja/API-reference.md | 6 ++++++ docs/ja/I18n.md | 2 ++ docs/ja/Plugin.md | 13 +++++++++++++ docs/ko/API-reference.md | 6 ++++++ docs/ko/I18n.md | 2 ++ docs/ko/Plugin.md | 13 +++++++++++++ docs/pt-br/API-reference.md | 6 ++++++ docs/pt-br/I18n.md | 2 ++ docs/pt-br/Plugin.md | 13 +++++++++++++ docs/zh-cn/API-reference.md | 6 ++++++ docs/zh-cn/I18n.md | 4 +++- docs/zh-cn/Plugin.md | 13 +++++++++++++ 18 files changed, 129 insertions(+), 3 deletions(-) diff --git a/docs/en/API-reference.md b/docs/en/API-reference.md index a4e97c914..8dd7d7979 100644 --- a/docs/en/API-reference.md +++ b/docs/en/API-reference.md @@ -514,3 +514,9 @@ plugin [`MinMax`](./Plugin.md#minmax) `.calendar` to display calendar time plugin [`Calendar`](./Plugin.md#calendar) + +### UpdateLocale + +`.updateLocale` to update a locale's properties + +plugin [`UpdateLocale`](./Plugin.md#updateLocale) diff --git a/docs/en/I18n.md b/docs/en/I18n.md index 045cb8c1e..401063aaf 100644 --- a/docs/en/I18n.md +++ b/docs/en/I18n.md @@ -77,7 +77,9 @@ dayjs() ## Customize -You could create your own locale. +You could update locale config via plugin [`UpdateLocale`](./Plugin.md#updateLocale) + +You could also create your own locale. Feel free to open a pull request to share your locale. diff --git a/docs/en/Plugin.md b/docs/en/Plugin.md index cc9688951..c8ba4fc7b 100644 --- a/docs/en/Plugin.md +++ b/docs/en/Plugin.md @@ -442,6 +442,19 @@ dayjs().calendar(null, { }) ``` +### UpdateLocale + +- UpdateLocale adds `.updateLocale` API to update a locale's properties. + +```javascript +import updateLocale from 'dayjs/plugin/updateLocale' +dayjs.extend(updateLocale) + +dayjs.updateLocale('en', { + months : String[] +}) +``` + ## Customize You could build your own Day.js plugin to meet different needs. diff --git a/docs/es-es/API-reference.md b/docs/es-es/API-reference.md index 57b3b42d5..be0f30029 100644 --- a/docs/es-es/API-reference.md +++ b/docs/es-es/API-reference.md @@ -514,3 +514,9 @@ plugin [`MinMax`](./Plugin.md#minmax) `.calendar` to display calendar time plugin [`Calendar`](./Plugin.md#calendar) + +### UpdateLocale + +`.updateLocale` to update a locale's properties + +plugin [`UpdateLocale`](./Plugin.md#updateLocale) diff --git a/docs/es-es/I18n.md b/docs/es-es/I18n.md index 551f38787..989269708 100644 --- a/docs/es-es/I18n.md +++ b/docs/es-es/I18n.md @@ -81,7 +81,9 @@ dayjs() ## Personalización -You could create your own locale. +You could update locale config via plugin [`UpdateLocale`](./Plugin.md#updateLocale) + +You could also create your own locale. Feel free to open a pull request to share your locale. diff --git a/docs/es-es/Plugin.md b/docs/es-es/Plugin.md index 7d44c3ad9..e5a7c1469 100644 --- a/docs/es-es/Plugin.md +++ b/docs/es-es/Plugin.md @@ -438,6 +438,19 @@ dayjs().calendar(null, { }) ``` +### UpdateLocale + +- UpdateLocale adds `.updateLocale` API to update a locale's properties. + +```javascript +import updateLocale from 'dayjs/plugin/updateLocale' +dayjs.extend(updateLocale) + +dayjs.updateLocale('en', { + months : String[] +}) +``` + ## Personalización Puedes construir tu propio complemento de Day.js para cubrir tus necesidades. diff --git a/docs/ja/API-reference.md b/docs/ja/API-reference.md index 8a1152c16..0eba8f216 100644 --- a/docs/ja/API-reference.md +++ b/docs/ja/API-reference.md @@ -514,3 +514,9 @@ UTC でパースや表示をしたい場合は、[`UTC`](./Plugin.md#utc)プラ `.calendar`で与えた日付のカレンダー上の情報が得られます。 プラグイン [`Calendar`](./Plugin.md#calendar) + +### UpdateLocale + +`.updateLocale` to update a locale's properties + +plugin [`UpdateLocale`](./Plugin.md#updateLocale) diff --git a/docs/ja/I18n.md b/docs/ja/I18n.md index 70f013500..a0dab94f0 100644 --- a/docs/ja/I18n.md +++ b/docs/ja/I18n.md @@ -77,6 +77,8 @@ dayjs() ## カスタマイズ +You could update locale config via plugin [`UpdateLocale`](./Plugin.md#updateLocale) + 独自のロケールを作成することもできます。 あなたのプラグインを共有する pull request を是非送ってみてください。 diff --git a/docs/ja/Plugin.md b/docs/ja/Plugin.md index d0f4a7a23..b8e7580e4 100644 --- a/docs/ja/Plugin.md +++ b/docs/ja/Plugin.md @@ -447,6 +447,19 @@ dayjs().calendar(null, { }) ``` +### UpdateLocale + +- UpdateLocale adds `.updateLocale` API to update a locale's properties. + +```javascript +import updateLocale from 'dayjs/plugin/updateLocale' +dayjs.extend(updateLocale) + +dayjs.updateLocale('en', { + months : String[] +}) +``` + ## カスタマイズ さまざまなニーズに合わせて独自の Day.js プラグインを構築することができます。 diff --git a/docs/ko/API-reference.md b/docs/ko/API-reference.md index e15ffafd9..70cb5d67d 100644 --- a/docs/ko/API-reference.md +++ b/docs/ko/API-reference.md @@ -513,3 +513,9 @@ plugin [`MinMax`](./Plugin.md#minmax) `.calendar` to display calendar time plugin [`Calendar`](./Plugin.md#calendar) + +### UpdateLocale + +`.updateLocale` to update a locale's properties + +plugin [`UpdateLocale`](./Plugin.md#updateLocale) diff --git a/docs/ko/I18n.md b/docs/ko/I18n.md index 421b05295..5df968cdc 100644 --- a/docs/ko/I18n.md +++ b/docs/ko/I18n.md @@ -77,6 +77,8 @@ dayjs() ## Customize +You could update locale config via plugin [`UpdateLocale`](./Plugin.md#updateLocale) + 당신만의 locale을 만들 수 있습니다. locale을 공휴하기위해 풀 리퀘스트를 여십시오. diff --git a/docs/ko/Plugin.md b/docs/ko/Plugin.md index dba61fdc6..90a9e3423 100644 --- a/docs/ko/Plugin.md +++ b/docs/ko/Plugin.md @@ -439,6 +439,19 @@ dayjs().calendar(null, { }) ``` +### UpdateLocale + +- UpdateLocale adds `.updateLocale` API to update a locale's properties. + +```javascript +import updateLocale from 'dayjs/plugin/updateLocale' +dayjs.extend(updateLocale) + +dayjs.updateLocale('en', { + months : String[] +}) +``` + ## Customize 다양한 요구를 충족하기위해 자신만의 Day.js 플러그인을 만들 수 있습니다. diff --git a/docs/pt-br/API-reference.md b/docs/pt-br/API-reference.md index 23cb01dac..18c165e81 100644 --- a/docs/pt-br/API-reference.md +++ b/docs/pt-br/API-reference.md @@ -512,3 +512,9 @@ plugin [`MinMax`](./Plugin.md#minmax) `.calendar` to display calendar time plugin [`Calendar`](./Plugin.md#calendar) + +### UpdateLocale + +`.updateLocale` to update a locale's properties + +plugin [`UpdateLocale`](./Plugin.md#updateLocale) diff --git a/docs/pt-br/I18n.md b/docs/pt-br/I18n.md index f072f95e4..4a383ecf2 100644 --- a/docs/pt-br/I18n.md +++ b/docs/pt-br/I18n.md @@ -77,6 +77,8 @@ dayjs() ## Customizar +You could update locale config via plugin [`UpdateLocale`](./Plugin.md#updateLocale) + Você pode criar o seu próprio _locale_. Sinta-se a vontade para abrir uma pull request e compartilhar sua _locale_. diff --git a/docs/pt-br/Plugin.md b/docs/pt-br/Plugin.md index 22ebe9e8a..8e16f5f58 100644 --- a/docs/pt-br/Plugin.md +++ b/docs/pt-br/Plugin.md @@ -438,6 +438,19 @@ dayjs().calendar(null, { }) ``` +### UpdateLocale + +- UpdateLocale adds `.updateLocale` API to update a locale's properties. + +```javascript +import updateLocale from 'dayjs/plugin/updateLocale' +dayjs.extend(updateLocale) + +dayjs.updateLocale('en', { + months : String[] +}) +``` + ## Customizar Você também pode construir seu próprio plugin Day.js para diferentes necessidades. diff --git a/docs/zh-cn/API-reference.md b/docs/zh-cn/API-reference.md index d5cd5753d..02465a8a1 100644 --- a/docs/zh-cn/API-reference.md +++ b/docs/zh-cn/API-reference.md @@ -564,3 +564,9 @@ plugin [`MinMax`](./Plugin.md#minmax) `.calendar` 来显示日历时间 plugin [`Calendar`](./Plugin.md#calendar) + +### 更新语言配置 + +`.updateLocale` 来更新语言配置的属性 + +plugin [`UpdateLocale`](./Plugin.md#updateLocale) diff --git a/docs/zh-cn/I18n.md b/docs/zh-cn/I18n.md index f0caf1d13..55b4aa108 100644 --- a/docs/zh-cn/I18n.md +++ b/docs/zh-cn/I18n.md @@ -77,7 +77,9 @@ dayjs() ## 自定义 -你可以根据需要自由的编写一个 Day.js 语言配置 +你可以使用 [`UpdateLocale`](./Plugin.md#updateLocale) 插件来更新语言配置 + +你还可以根据需要自由的编写一个 Day.js 语言配置 同时欢迎提交 PR 与大家分享你的语言配置 diff --git a/docs/zh-cn/Plugin.md b/docs/zh-cn/Plugin.md index 57838c742..89bdf56ce 100644 --- a/docs/zh-cn/Plugin.md +++ b/docs/zh-cn/Plugin.md @@ -441,6 +441,19 @@ dayjs().calendar(null, { }) ``` +### UpdateLocale + +- UpdateLocale 增加了 `.updateLocale` API 来更新语言配置的属性。 + +```javascript +import updateLocale from 'dayjs/plugin/updateLocale' +dayjs.extend(updateLocale) + +dayjs.updateLocale('en', { + months : String[] +}) +``` + ## 自定义 你可以根据需要自由的编写一个 Day.js 插件