Skip to content

Commit

Permalink
fix: Add UpdateLocale plugin to update a locale's properties (#766)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkun authored Jan 6, 2020
1 parent ba709ec commit 82ce2ba
Show file tree
Hide file tree
Showing 22 changed files with 236 additions and 4 deletions.
6 changes: 6 additions & 0 deletions docs/en/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 3 additions & 1 deletion docs/en/I18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
13 changes: 13 additions & 0 deletions docs/en/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions docs/es-es/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 3 additions & 1 deletion docs/es-es/I18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
13 changes: 13 additions & 0 deletions docs/es-es/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions docs/ja/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 2 additions & 0 deletions docs/ja/I18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ dayjs()

## カスタマイズ

You could update locale config via plugin [`UpdateLocale`](./Plugin.md#updateLocale)

独自のロケールを作成することもできます。

あなたのプラグインを共有する pull request を是非送ってみてください。
Expand Down
13 changes: 13 additions & 0 deletions docs/ja/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 プラグインを構築することができます。
Expand Down
6 changes: 6 additions & 0 deletions docs/ko/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 2 additions & 0 deletions docs/ko/I18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ dayjs()

## Customize

You could update locale config via plugin [`UpdateLocale`](./Plugin.md#updateLocale)

당신만의 locale을 만들 수 있습니다.

locale을 공휴하기위해 풀 리퀘스트를 여십시오.
Expand Down
13 changes: 13 additions & 0 deletions docs/ko/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 플러그인을 만들 수 있습니다.
Expand Down
6 changes: 6 additions & 0 deletions docs/pt-br/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 2 additions & 0 deletions docs/pt-br/I18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_.
Expand Down
13 changes: 13 additions & 0 deletions docs/pt-br/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions docs/zh-cn/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -564,3 +564,9 @@ plugin [`MinMax`](./Plugin.md#minmax)
`.calendar` 来显示日历时间

plugin [`Calendar`](./Plugin.md#calendar)

### 更新语言配置

`.updateLocale` 来更新语言配置的属性

plugin [`UpdateLocale`](./Plugin.md#updateLocale)
18 changes: 17 additions & 1 deletion docs/zh-cn/I18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ dayjs()

## 自定义

你可以根据需要自由的编写一个 Day.js 语言配置
你可以使用 [`UpdateLocale`](./Plugin.md#updateLocale) 插件来更新语言配置

你还可以根据需要自由的编写一个 Day.js 语言配置

同时欢迎提交 PR 与大家分享你的语言配置

Expand All @@ -93,6 +95,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
Expand Down
13 changes: 13 additions & 0 deletions docs/zh-cn/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 插件
Expand Down
13 changes: 13 additions & 0 deletions src/plugin/updateLocale/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default (option, Dayjs, dayjs) => {
dayjs.updateLocale = function (locale, customConfig) {
const localeList = dayjs.Ls
const localeConfig = localeList[locale]
if (!localeConfig) return
const customConfigKeys = customConfig ? Object.keys(customConfig) : []
customConfigKeys.forEach((c) => {
localeConfig[c] = customConfig[c]
})
return localeConfig // eslint-disable-line consistent-return
}
}

71 changes: 71 additions & 0 deletions test/plugin/updateLocale.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
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', () => {
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)

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))
})
})
2 changes: 1 addition & 1 deletion types/plugin/minMax.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PluginFunc, ConfigType } from 'dayjs'
import { PluginFunc } from 'dayjs'

declare const plugin: PluginFunc
export = plugin
Expand Down
8 changes: 8 additions & 0 deletions types/plugin/updateLocale.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { PluginFunc } from 'dayjs'

declare const plugin: PluginFunc
export = plugin

declare module 'dayjs' {
export function updateLocale(localeName: String, customConfig: Object): any
}

0 comments on commit 82ce2ba

Please sign in to comment.