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

Fallback to language only locale + support uppercase locales #1524

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 9 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@ const parseLocale = (preset, object, isLocal) => {
let l
if (!preset) return L
if (typeof preset === 'string') {
if (Ls[preset]) {
l = preset
const presetLower = preset.toLowerCase()
if (Ls[presetLower]) {
l = presetLower
}
if (object) {
Ls[preset] = object
l = preset
Ls[presetLower] = object
l = presetLower
}
const presetSplit = preset.split('-')
if (!l && presetSplit.length) {
return parseLocale(presetSplit[0])
}
} else {
const { name } = preset
Expand Down
10 changes: 10 additions & 0 deletions test/plugin/localizedFormat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ it('Uses the localized lowercase formats if defined', () => {
['l', 'll', 'lll', 'llll'].forEach(option => expect(znDate.format(option)).toBe(znDate.format(znCn.formats[option])))
})

it('Uses fallback to xx if xx-yy not available', () => {
expect(dayjs('2019-02-01').locale('es-yy').format('L'))
Copy link
Owner

Choose a reason for hiding this comment

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

it will be more clear to use format('MMMM') to test if the locale is correct and not the default English

.toBe('01/02/2019')
})

it('Uses xx-yy if xx-YY is provided', () => {
expect(dayjs('2019-02-01').locale('es-US').format('L'))
.toBe('01/02/2019')
})

it('Uses the localized uppercase formats as a base for lowercase formats, if not defined', () => {
const date = new Date()
const spanishDate = dayjs(date, { locale: es });
Expand Down