Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update dayjs locale mapping #382

Merged
merged 1 commit into from
Apr 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 75 additions & 28 deletions src/generate/dayjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,70 @@ dayjs.extend((o, c) => {

type IlocaleMapObject = Record<string, string>;
const localeMap: IlocaleMapObject = {
// ar_EG:
// az_AZ:
// bg_BG:
bn_BD: 'bn-bd',
by_BY: 'be',
// ca_ES:
// cs_CZ:
// da_DK:
// de_DE:
// el_GR:
en_GB: 'en-gb',
en_US: 'en',
// es_ES:
// et_EE:
// fa_IR:
// fi_FI:
fr_BE: 'fr', // todo: dayjs has no fr_BE locale, use fr at present
fr_CA: 'fr-ca',
// fr_FR:
// ga_IE:
// gl_ES:
// he_IL:
// hi_IN:
// hr_HR:
// hu_HU:
hy_AM: 'hy-am',
// id_ID:
// is_IS:
// it_IT:
// ja_JP:
// ka_GE:
// kk_KZ:
// km_KH:
kmr_IQ: 'ku',
// kn_IN:
// ko_KR:
// ku_IQ: // previous ku in antd
// lt_LT:
// lv_LV:
// mk_MK:
// ml_IN:
// mn_MN:
// ms_MY:
// nb_NO:
// ne_NP:
nl_BE: 'nl-be',
// nl_NL:
// pl_PL:
pt_BR: 'pt-br',
// pt_PT:
// ro_RO:
// ru_RU:
// sk_SK:
// sl_SI:
// sr_RS:
// sv_SE:
// ta_IN:
// th_TH:
// tr_TR:
// uk_UA:
// ur_PK:
// vi_VN:
zh_CN: 'zh-cn',
zh_HK: 'zh-hk',
zh_TW: 'zh-tw',
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个 map 是 moment 和 dayjs 的差异,还是不是可以在 dayjs 下单独长期维护一个。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个是 dayjs 和 antd locale 的差异 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

但是后面也可以考虑 dayjs 自己做, moment 的 locale 命名和 dayjs 是一致的,但是在解析 locale string 的时候 moment 会把 zh_HK 解析成 zh-hk


Expand All @@ -47,18 +108,18 @@ const parseNoMatchNotice = () => {
const generateConfig: GenerateConfig<Dayjs> = {
// get
getNow: () => dayjs(),
getFixedDate: string => dayjs(string, ['YYYY-M-DD', 'YYYY-MM-DD']),
getEndDate: date => date.endOf('month'),
getWeekDay: date => {
getFixedDate: (string) => dayjs(string, ['YYYY-M-DD', 'YYYY-MM-DD']),
getEndDate: (date) => date.endOf('month'),
getWeekDay: (date) => {
const clone = date.locale('en');
return clone.weekday() + clone.localeData().firstDayOfWeek();
},
getYear: date => date.year(),
getMonth: date => date.month(),
getDate: date => date.date(),
getHour: date => date.hour(),
getMinute: date => date.minute(),
getSecond: date => date.second(),
getYear: (date) => date.year(),
getMonth: (date) => date.month(),
getDate: (date) => date.date(),
getHour: (date) => date.hour(),
getMinute: (date) => date.minute(),
getSecond: (date) => date.second(),

// set
addYear: (date, diff) => date.add(diff, 'year'),
Expand All @@ -73,26 +134,14 @@ const generateConfig: GenerateConfig<Dayjs> = {

// Compare
isAfter: (date1, date2) => date1.isAfter(date2),
isValidate: date => date.isValid(),
isValidate: (date) => date.isValid(),

locale: {
getWeekFirstDay: locale =>
dayjs()
.locale(parseLocale(locale))
.localeData()
.firstDayOfWeek(),
getWeekFirstDay: (locale) => dayjs().locale(parseLocale(locale)).localeData().firstDayOfWeek(),
getWeekFirstDate: (locale, date) => date.locale(parseLocale(locale)).weekday(0),
getWeek: (locale, date) => date.locale(parseLocale(locale)).week(),
getShortWeekDays: locale =>
dayjs()
.locale(parseLocale(locale))
.localeData()
.weekdaysMin(),
getShortMonths: locale =>
dayjs()
.locale(parseLocale(locale))
.localeData()
.monthsShort(),
getShortWeekDays: (locale) => dayjs().locale(parseLocale(locale)).localeData().weekdaysMin(),
getShortMonths: (locale) => dayjs().locale(parseLocale(locale)).localeData().monthsShort(),
format: (locale, date, format) => date.locale(parseLocale(locale)).format(format),
parse: (locale, text, formats) => {
const localeStr = parseLocale(locale);
Expand All @@ -103,9 +152,7 @@ const generateConfig: GenerateConfig<Dayjs> = {
// parse Wo
const year = formatText.split('-')[0];
const weekStr = formatText.split('-')[1];
const firstWeek = dayjs(year, 'YYYY')
.startOf('year')
.locale(localeStr);
const firstWeek = dayjs(year, 'YYYY').startOf('year').locale(localeStr);
for (let j = 0; j <= 52; j += 1) {
const nextWeek = firstWeek.add(j, 'week');
if (nextWeek.format('Wo') === weekStr) {
Expand Down