Skip to content

Commit

Permalink
feat: Fallback to language only locale + support uppercase locales (#…
Browse files Browse the repository at this point in the history
…1524)

* Support uppercase region as in xx-ZZ + fallback to xx if xx-yy does not exist

* code review fix, split preset just once

* review fixes + don't recurse if split length is 1

Co-authored-by: Johan Linderoth <johan@codeability.se>
  • Loading branch information
codeability-ab and Johan Linderoth authored Feb 28, 2022
1 parent f9055a7 commit 9138dc2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
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 > 1) {
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('en-yy').format('MMMM'))
.toBe('February')
})

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

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

0 comments on commit 9138dc2

Please sign in to comment.