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 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
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) {
Copy link
Owner

Choose a reason for hiding this comment

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

if (!l && presetSplit.length) {

we could just check the length is above 0

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That was my first attempt but other unit tests break if I don't check that the split actually splits the preset string (i.e it results in more than one array element).

The test "Not supported locale string fallback to previous one (instance)" in locale.test.js fails with "if (!l && presetSplit.length) {"

The "not_supported_locale_string" is split into one element => recursive call, "not_supported_locale_string" is split into one element again => recursive call ... etc

Copy link
Owner

Choose a reason for hiding this comment

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

oh. I see, you can just change the "not_supported_locale_string" test string 😁

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You mean like "not-supported-locale-string"?
But that string will never split into something valid.
I still think it's a good idea to only recurse if the preset actually splits into 2 parts.

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