-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Localize preprocessor feature + french locales ordinal day with long month format bug fix #3662
Localize preprocessor feature + french locales ordinal day with long month format bug fix #3662
Conversation
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.
nice!
c895012
to
ddac6a5
Compare
For those specifically interested in the french locale fix, here is the isolated code: date-fns/src/locale/fr/_lib/localize/index.ts Lines 127 to 148 in ddac6a5
|
Just to make sure, https://github.com/date-fns/date-fns/pull/3662/files#diff-eda4dba459a77e98278dfef21c803e0e1b94faef1d5a3a3c2b42657b8a4a6e58R128 there is an |
Yes, it's been that way for a while now - I wrote about this in #3136 too |
Thanks for the fix and for your time ! |
Appreciate your review and all the efforts to surface this issue. Hope the approach is sound and we can eventually get this merged! |
Oh one other thing I forgot to mention, this only takes into account the https://date-fns.org/docs/format |
I'm not sure to understand the difference between |
Love it, will try to review it carefully and ship with the next version 🙏 |
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.
The change is brilliant! I don't know if it will be helpful outside French, but I'll keep my eyes open. If not, and we meet some other nuance in a locale, I'll use a similar preprocessor approach.
@@ -3,8 +3,7 @@ | |||
import assert from "assert"; | |||
import { pipe } from "fp-ts/function"; | |||
import { flow as jsFnsFlow } from "js-fns"; | |||
// @ts-expect-error - Lodash, come on, bro | |||
import { flow as lodashFlow } from "loedash"; |
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.
Wow, how did it work, and how didn't I miss it? 🤣
Shipped with v3.3.0: https://github.com/date-fns/date-fns/releases/tag/v3.3.0 |
The PR introduces an optional preprocessor function that lives on the locale.localize object. After the
format
function parses and tokenizes, the preprocessor is invoked with the date and a parts array (the tokens and string literals found in the format string). The preprocessor can modify this array as needed.This allows us to nicely fix the long standing #1391 french locale day ordinal with long months format regression. This is the best solution I could think of that would not break any public interface and be low overhead. Hope this can benefit other locales too!
As discussed in #3659, I brought back the unit tests from date-fns v1. I also added an additional date and format to the locale snapshots to better surface day ordinal issues (particularly related to the french language) - apologies for the added noise.
Bonus 1: somewhat unrelated, but working on
format
, I realized that the_originalDate
argument sent to the formatters was never relevant (both date and options._originalDate were always the same object). I removed this to simplify things a bit.Bonus 2: Fixed bad import in main branch that was breaking the tests