diff --git a/src/generate/dateFns.ts b/src/generate/dateFns.ts index c1c97d91c..6076f4160 100644 --- a/src/generate/dateFns.ts +++ b/src/generate/dateFns.ts @@ -31,7 +31,19 @@ import { import * as locales from 'date-fns/locale'; import type { GenerateConfig } from '.'; +// date-fns has no es-US locale and its `es` locale starts the week on Monday. +// The `es_US` picker locale means Spanish with the US week start, so keep the +// Spanish formatting and override the first day of the week. +const esUS: Locale = { + ...locales.es, + options: { ...locales.es.options, weekStartsOn: 0 }, +}; + const getLocale = (locale: string): Locale => { + if (locale === 'es_US') { + return esUS; + } + return ( locales[locale] || locales[locale.replace(/_/g, '')] || locales[locale.replace(/_.*$/g, '')] ); diff --git a/src/generate/dayjs.ts b/src/generate/dayjs.ts index a4fa37804..1fa2f8536 100644 --- a/src/generate/dayjs.ts +++ b/src/generate/dayjs.ts @@ -39,6 +39,7 @@ const localeMap: IlocaleMapObject = { // el_GR: en_GB: 'en-gb', en_US: 'en', + es_US: 'es-us', // es_ES: // et_EE: // fa_IR: diff --git a/tests/generate.spec.tsx b/tests/generate.spec.tsx index f0a7aed60..c555e7b15 100644 --- a/tests/generate.spec.tsx +++ b/tests/generate.spec.tsx @@ -9,6 +9,7 @@ import { getMoment } from './util/commonUtil'; import 'dayjs/locale/zh-cn'; import 'dayjs/locale/ko'; import 'dayjs/locale/fr'; +import 'dayjs/locale/es-us'; import type { GenerateConfig } from '../src/generate'; describe('Picker.Generate', () => { @@ -415,3 +416,24 @@ describe('Generate:date-fns', () => { expect(dateFnsGenerateConfig.getMinute(parsed)).toEqual(30); }); }); + + +describe('Generate: es_US', () => { + it('dayjs keeps the US week start and formats in Spanish', () => { + expect(dayjsGenerateConfig.locale.getWeekFirstDay('es_US')).toEqual(0); + + const date = dayjsGenerateConfig.locale.parse('es_US', '2018-01-15', ['YYYY-MM-DD'])!; + + expect(dayjsGenerateConfig.locale.format('es_US', date, 'MMMM')).toEqual('enero'); + expect(dayjsGenerateConfig.locale.format('es_US', date, 'dddd')).toEqual('lunes'); + }); + + it('date-fns keeps the US week start and formats in Spanish', () => { + expect(dateFnsGenerateConfig.locale.getWeekFirstDay('es_US')).toEqual(0); + + const date = new Date(2018, 0, 15); + + expect(dateFnsGenerateConfig.locale.format('es_US', date, 'MMMM')).toEqual('enero'); + expect(dateFnsGenerateConfig.locale.format('es_US', date, 'EEEE')).toEqual('lunes'); + }); +});