Skip to content

Commit

Permalink
chore: add zh locale (iamkun#822)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZYinMD authored Mar 10, 2020
1 parent 30aab0c commit 9dd69ed
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
71 changes: 71 additions & 0 deletions src/locale/zh.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Chinese [zh]
import dayjs from 'dayjs'

const locale = {
name: 'zh',
weekdays: '星期日_星期一_星期二_星期三_星期四_星期五_星期六'.split('_'),
weekdaysShort: '周日_周一_周二_周三_周四_周五_周六'.split('_'),
weekdaysMin: '日_一_二_三_四_五_六'.split('_'),
// prettier conflicts with eslint, so
// prettier-ignore
months: '一月_二月_三月_四月_五月_六月_七月_八月_九月_十月_十一月_十二月'.split('_'),
monthsShort: '1月_2月_3月_4月_5月_6月_7月_8月_9月_10月_11月_12月'.split('_'),
ordinal: (number, period) => {
switch (period) {
case 'W':
return `第${number}周`
default:
return `${number}日`
}
},
weekStart: 1,
yearStart: 4,
formats: {
LT: 'HH:mm',
LTS: 'HH:mm:ss',
L: 'YYYY/MM/DD',
LL: 'YYYY年M月D日',
LLL: 'YYYY年M月D日Ah点mm分',
LLLL: 'YYYY年M月D日ddddAh点mm分',
l: 'YYYY/M/D',
ll: 'YYYY年M月D日',
lll: 'YYYY年M月D日 HH:mm',
llll: 'YYYY年M月D日dddd HH:mm'
},
relativeTime: {
future: '%s后',
past: '%s前',
s: '几秒',
m: '1分钟',
mm: '%d分钟',
h: '1小时',
hh: '%d小时',
d: '1天',
dd: '%d天',
M: '1个月',
MM: '%d个月',
y: '1年',
yy: '%d年'
},
meridiem: (hour, minute) => {
// prettier conflicts with eslint, so
// prettier-ignore
const hm = (hour * 100) + minute
if (hm < 600) {
return '凌晨'
} else if (hm < 900) {
return '早上'
} else if (hm < 1130) {
return '上午'
} else if (hm < 1230) {
return '中午'
} else if (hm < 1800) {
return '下午'
}
return '晚上'
}
}

dayjs.locale(locale, null, true)

export default locale
20 changes: 20 additions & 0 deletions test/locale/zh.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import dayjs from '../../src'
import advancedFormat from '../../src/plugin/advancedFormat'
import weekOfYear from '../../src/plugin/weekOfYear'
import '../../src/locale/zh.js'
import '../../src/locale/zh-cn.js'

dayjs.extend(advancedFormat).extend(weekOfYear)

const zh = dayjs().locale('zh')
const zhCN = dayjs().locale('zh-cn')

test('ordinal', () => {
expect(zh.format('wo')).toEqual(`第${zh.format('w')}周`)
})

test('Meridiem', () => {
for (let i = 0; i <= 24; i += 1) {
expect(zh.add(i, 'hour').format('A')).toBe(zhCN.add(i, 'hour').format('A'))
}
})

0 comments on commit 9dd69ed

Please sign in to comment.