-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Fallback to language only locale + support uppercase locales #1524
Conversation
Codecov Report
@@ Coverage Diff @@
## dev #1524 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 176 176
Lines 1980 1984 +4
Branches 502 503 +1
=========================================
+ Hits 1980 1984 +4
Continue to review full report at Codecov.
|
@iamkun will this pull request be considered for merge? |
src/index.js
Outdated
Ls[presetLower] = object | ||
l = presetLower | ||
} | ||
if (!l && preset.split('-').length > 1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe we can simplify the logic to
const presetSplit = preset.split('-')
if (!l && presetSplit.length) {
return parseLocale(presetSplit[0])
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. I'll fix it!
test/plugin/localizedFormat.test.js
Outdated
@@ -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')) |
There was a problem hiding this comment.
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
l = presetLower | ||
} | ||
const presetSplit = preset.split('-') | ||
if (!l && presetSplit.length > 1) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 😁
There was a problem hiding this comment.
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.
@iamkun is there anything stopping you from merging? Anything I need to do? |
I would love to see this PR merged. It is exactly fixing all the issues I've encountered on my React Native App, when trying to load the correct locale based on system preferences |
@iamkun Can we merge this one please? |
THX |
# [1.11.0](v1.10.8...v1.11.0) (2022-03-14) ### Bug Fixes * Add Kirundi (rn) locale ([#1793](#1793)) ([74e5247](74e5247)) * add missing date shorthand D type definition ([#1752](#1752)) ([b045baf](b045baf)) * Add relative time to Galician (gl) and fix ordinals ([#1800](#1800)) ([dcbf170](dcbf170)) * update German locales (de-at, de-ch) ([#1775](#1775)) ([f9055a7](f9055a7)) * update Icelandic [is] locale relativeTime config ([#1796](#1796)) ([76f9e17](76f9e17)) * Update index.d.ts note ([#1716](#1716)) ([5a108ff](5a108ff)) * Update locale German [de] monthsShort ([#1746](#1746)) ([4a7b7d0](4a7b7d0)) * update meridiem function to Kurdish (ku) locale ([#1725](#1725)) ([efd3904](efd3904)) * update updateLocal plugin typescript types ([#1692](#1692)) ([c7a3f73](c7a3f73)) ### Features * Fallback to language only locale + support uppercase locales ([#1524](#1524)) ([9138dc2](9138dc2))
🎉 This PR is included in version 1.11.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
# [1.11.0](iamkun/dayjs@v1.10.8...v1.11.0) (2022-03-14) ### Bug Fixes * Add Kirundi (rn) locale ([#1793](iamkun/dayjs#1793)) ([74e5247](iamkun/dayjs@74e5247)) * add missing date shorthand D type definition ([#1752](iamkun/dayjs#1752)) ([b045baf](iamkun/dayjs@b045baf)) * Add relative time to Galician (gl) and fix ordinals ([#1800](iamkun/dayjs#1800)) ([dcbf170](iamkun/dayjs@dcbf170)) * update German locales (de-at, de-ch) ([#1775](iamkun/dayjs#1775)) ([f9055a7](iamkun/dayjs@f9055a7)) * update Icelandic [is] locale relativeTime config ([#1796](iamkun/dayjs#1796)) ([76f9e17](iamkun/dayjs@76f9e17)) * Update index.d.ts note ([#1716](iamkun/dayjs#1716)) ([5a108ff](iamkun/dayjs@5a108ff)) * Update locale German [de] monthsShort ([#1746](iamkun/dayjs#1746)) ([4a7b7d0](iamkun/dayjs@4a7b7d0)) * update meridiem function to Kurdish (ku) locale ([#1725](iamkun/dayjs#1725)) ([efd3904](iamkun/dayjs@efd3904)) * update updateLocal plugin typescript types ([#1692](iamkun/dayjs#1692)) ([c7a3f73](iamkun/dayjs@c7a3f73)) ### Features * Fallback to language only locale + support uppercase locales ([#1524](iamkun/dayjs#1524)) ([9138dc2](iamkun/dayjs@9138dc2))
# [1.11.0](iamkun/dayjs@v1.10.8...v1.11.0) (2022-03-14) ### Bug Fixes * Add Kirundi (rn) locale ([#1793](iamkun/dayjs#1793)) ([74e5247](iamkun/dayjs@74e5247)) * add missing date shorthand D type definition ([#1752](iamkun/dayjs#1752)) ([b045baf](iamkun/dayjs@b045baf)) * Add relative time to Galician (gl) and fix ordinals ([#1800](iamkun/dayjs#1800)) ([dcbf170](iamkun/dayjs@dcbf170)) * update German locales (de-at, de-ch) ([#1775](iamkun/dayjs#1775)) ([f9055a7](iamkun/dayjs@f9055a7)) * update Icelandic [is] locale relativeTime config ([#1796](iamkun/dayjs#1796)) ([76f9e17](iamkun/dayjs@76f9e17)) * Update index.d.ts note ([#1716](iamkun/dayjs#1716)) ([5a108ff](iamkun/dayjs@5a108ff)) * Update locale German [de] monthsShort ([#1746](iamkun/dayjs#1746)) ([4a7b7d0](iamkun/dayjs@4a7b7d0)) * update meridiem function to Kurdish (ku) locale ([#1725](iamkun/dayjs#1725)) ([efd3904](iamkun/dayjs@efd3904)) * update updateLocal plugin typescript types ([#1692](iamkun/dayjs#1692)) ([c7a3f73](iamkun/dayjs@c7a3f73)) ### Features * Fallback to language only locale + support uppercase locales ([#1524](iamkun/dayjs#1524)) ([9138dc2](iamkun/dayjs@9138dc2))
# [1.11.0](iamkun/dayjs@v1.10.8...v1.11.0) (2022-03-14) ### Bug Fixes * Add Kirundi (rn) locale ([#1793](iamkun/dayjs#1793)) ([74e5247](iamkun/dayjs@74e5247)) * add missing date shorthand D type definition ([#1752](iamkun/dayjs#1752)) ([b045baf](iamkun/dayjs@b045baf)) * Add relative time to Galician (gl) and fix ordinals ([#1800](iamkun/dayjs#1800)) ([dcbf170](iamkun/dayjs@dcbf170)) * update German locales (de-at, de-ch) ([#1775](iamkun/dayjs#1775)) ([f9055a7](iamkun/dayjs@f9055a7)) * update Icelandic [is] locale relativeTime config ([#1796](iamkun/dayjs#1796)) ([76f9e17](iamkun/dayjs@76f9e17)) * Update index.d.ts note ([#1716](iamkun/dayjs#1716)) ([5a108ff](iamkun/dayjs@5a108ff)) * Update locale German [de] monthsShort ([#1746](iamkun/dayjs#1746)) ([4a7b7d0](iamkun/dayjs@4a7b7d0)) * update meridiem function to Kurdish (ku) locale ([#1725](iamkun/dayjs#1725)) ([efd3904](iamkun/dayjs@efd3904)) * update updateLocal plugin typescript types ([#1692](iamkun/dayjs#1692)) ([c7a3f73](iamkun/dayjs@c7a3f73)) ### Features * Fallback to language only locale + support uppercase locales ([#1524](iamkun/dayjs#1524)) ([9138dc2](iamkun/dayjs@9138dc2))
Fallback to locale xx if xx-yy is provided and does not exist
Also support locales in uppercase such as en-GB