Make WordPress Core

Opened 15 months ago

Last modified 14 months ago

#51142 new defect (bug)

Invalid Moment.js locale from get_user_locale()

Reported by: slightlyfaulty Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.0
Component: External Libraries Keywords:
Focuses: javascript Cc:


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)

#1 @slightlyfaulty
15 months ago

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' ) returns 25 instead of the expected 25th.

#2 @SergeyBiryukov
15 months ago

  • Version changed from 5.5 to 5.0

Hi there, welcome to WordPress Trac! Thanks for the report.

Some history of the code in question:

  • Introduced in [44262] / #45145, changing the version to 5.0 as the earliest applicable version.
  • Adjusted in [48075] / #50408 to use moment.updateLocale() instead of moment.locale().

#3 @iandunn
14 months ago


Note: See TracTickets for help on using tickets.