OMG, I wrote a rather complex code block that has been live for about 2 years now that I was quite proud of until watching this video and reaching the realization that I could’ve done it much more simply using RelativeTimeFormat. I’m conflicted now on just leaving that code alone (since it has been running trouble-free for such a long time now) or going back to refactor. Here I’ve been contented, thinking that routine was fully optimized. Thanks, Kyle, for putting the spotlight on Intl. This is truly eye-opening and will definitely affect my future projects.
Honestly, I would just leave it as is unless you need to update the code or add to it. Most likely the refactor would be pretty quick, though, so you could do it if you want to.
@@WebDevSimplified Funny you should say that. I opened up my source all motivated to refactor but stopped myself at the last minute. Over the years, part of how I know I continue to progress as a developer is my newest code almost always looks better to me than my older code. But, every so often (stating it as humbly as possible), I look back and think, "Wow, that's good work. I did a good job." It's a rare but satisfying feeling to look at your past efforts and appreciate the quality of your logic and the elegance of your implementation. This particular code is especially solid, works universally, has no special dependencies, and has caused zero problems for all its users so, in the end, I decided to leave it be. I'm glad your advice on the matter turned out to be along the same lines. A refactor would probably be fairly quick but I think it's a good discipline to let your code go once you know it to be stable. If your code doesn't embarrass you when you look back on it with new eyes, it's probably optimal, at least in the practical real-world sense. That's my personal view on the matter for whatever it's worth. Nevertheless, I made myself an abstraction so I can get sharp on the ins and outs of Intl for future projects. And I'm still immensely grateful to you for shedding light on this gem. I am sure I'm not alone for having overlooked all the useful capabilities hiding in Intl under the false assumption that it only pertained to situations requiring a lot of user localization. No question this will definitely be part of my arsenal going forward. So, thank you again, Kyle. Please keep up the great work. I appreciate you immensely and often get great value and insights from your videos. Take pride in knowing you are a force for good in this world. Cheers!
I had a "jaw moment" learning this from you, Kyle. Thanks for the video, the hype about Intl was well deserved. I've solved most of the problems you brought up with libraries like date-fns, or wacky one-time code. Now an Intl fan boy lol. Also, it occurred to me that if you can apply a bunch of formatters to dates specifically, it's probably best to keep them in standard ISO format. I tend to be pretty inconsistent about this, but will basically always store times as such going forward. For anyone with knowledge, is conversion from a date string to date object considered a "slow" operation in javascript?
Good to know. I wonder if Intl will have more convenient utilities that will enable us to replace libraries such as date-fns for basic formatting and dates comparisons, for example.
Hey kyle ! Thats an awesome content you have shared as like others you create often. Please create some real word enterprise web application tutorial if you can.
Allow to clarify a little bit more on how you can use the PluralRules subclass The PluralRules subclass of the Intl API in JavaScript helps developers with pluralization, which is choosing the right word form based on the grammar rules of a language. Let's take Kyle's example about, in French, the word "mouse" ("souris") does not change form depending on the quantity, but lets say the user wants the website to be in English: const pluralRule = new Intl.PluralRules("en"); function getFormedAmountOfMice(quantity) { const formedQuantity = pluralRule.select(quantity); switch (formedQuantity) { case 'one': return `${quantity} mouse`; case 'other': return `${quantity} mice`; } } The function returns a string that displays the correct form of the word based on the plural form selected by the pluralRule object