Opened 5 months ago
Last modified 4 months ago
#51142 new defect (bug)
Invalid Moment.js locale from get_user_locale()
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | 5.0 |
Component: | External Libraries | Keywords: | |
Focuses: | javascript | Cc: |
Description
get_user_locale()
returns a locale in the format en_US
, which is passed directly to Moment.js, e.g. moment.updateLocale( 'en_US', {...} )
.
Since Moment.js doesn't actually support two-part locales, this causes some issues. For example, moment().format( 'Do' )
returns 25
where it should return 25th
. This is because the local *ordinal* is lost as a result of setting an undefined locale.
The locale should either be passed in a format that Moment.js understands, such as en
, or moment.defineLocale( 'en_US', { parentLocale: 'en' } )
should be called before moment.updateLocale( 'en_US', {...} )
. AFAIK these are equivalent.
You can see the issue reproduced here: https://codepen.io/slightlyfaulty/pen/WNwpMmG
Of course, swapping out old dusty Moment.js for its super modern and popular drop-in replacement Day.js is also an option 😎
Change History (3)
#3
@
4 months ago
Related:
- https://github.com/WordPress/gutenberg/issues/15221 - Ordinal suffix token does not work in
wp.date.format
- https://github.com/WordPress/gutenberg/pull/25782 - Use date-fns and date-fns-tz instead of moment
It might be worth mentioning that this doesn't just affect usage of Moment.js itself, but also the
@wordpress/date
package.For example,
format( 'jS' )
returns25
instead of the expected25th
.