Moment.js released version 2.12.0 a couple weeks ago, and with that came a major new feature – locale inheritance.
Defining custom locale data has been possible in Moment.js for a very long time, and it is one of the coolest features of the library.
Suppose I wanted create a locale that was specifically for mobile use, which was in French, but had shorter strings for Moment’s relative time feature. Previous to 2.12, I might have tried something like this:
With 2.11.2 and previous, the result would have been great when I called .fromNow():
But I would have gotten a surprise when I called .calendar():
English! Whoops. This is because there was no locale inheritance in Moment.js. Locales always fell back to English if data was not defined. To create this fr-mobile locale, I would have had to define every locale property available in Moment.
Enter locale inheritance.
Now I can just define a parent locale for my mobile locale:
Previously, there was also an issue with partial object definitions. Suppose I wanted to change just a couple things about relative time in the French locale. I would have tried this:
That didn’t go well did it?
Enter updateLocale in 2.12.
Big thanks to Iskren for getting this in.
Save effort, save file size, be happy!