
Definitely, programmers living in the U.S. (and few additional locations in Mexico and Canada) are luckier than us about the age-old problem of internationalizing methods.
This makes their life so much easier because they don't have to think about internationalization: not about converting to distinct date formats, not about the idiosyncrasies occurring from language translations, and definitely not about the odds about other countries ' lists and currencies.
These issues for other professionals are extremely difficult. For instance, the absence of international standardization (and the failure to convert numbers) happened in a mismatch of metric units versus English units, causing NASA's Mars Orbiter to miss orbit around Mars in 1999.
While the numbers with which our business operates are not quite as big, and our mistakes result in bugs in the code that we can generally solve in a couple of hours or days, we can definitely sympathize with NASA technicians, and we cann't stop thinking about how a easy error like not double checking that everybody was using the same readings in the code of Orbiter caused such a loss.
That's why, when we heard the recent release of the ECMAScript Internationalization API, we were pleased to see what sorts of changes would make JavaScript better and more standardized for everyone using it around the globe.
International Date Time
Mozilla’s MDN Web Docs, provides the best description of the latest international API and the characteristics that come with it:
The INTL object provides access to several constructors as well as functionality common to the internationalization constructors and other language sensitive functions.-MDN Web Docs
There are six properties that come with the new Intl object, but in our perspective, only five of them deserve our attention:
Intl.DateTimeFormat
Intl.RelativeTimeFormat
Intl.ListFormat
Intl.NumberFormat
Intl.PluralRules
Well, let's talk about the International Date Time Format.
The Intl.DateTimeFormat
object is a constructor for objects that allows the formatting of date and time.
At its core, the date time format outputs formatted time stamps based on local and options objects.
Here is an overview using locales
:
//Date.UTC(year, month, day, hours, minutes, seconds, millisec) var date = new Date(Date.UTC(2019, 6, 8, 3, 0, 0));
// using US date time formatting
console.log(new Intl.DateTimeFormat('en-US').format(date));
// expected output: "7/8/2019" // using Great Britain date time formatting
console.log(new Intl.DateTimeFormat('en-GB').format(date)); // expected output: "8/7/2019"
Users can also provide extra parameters, such as:
- nu - the number systems used in different countries,
- ca - the calendar to use,
- hc - hour cycle ( h12, h24, etc.).
After entering locales
, we can switch to the the optional options object
which can include properties such as:
- timezone: "UTC” or something like"America/New_York",
- hourCycle : for example "h12" or "h23",
- formatMatcher : This algorithm can retrieve the properties used to describe the date and time and format the outputs to match.
formatMatcher
subsets like:
- weekday, year, month, day, hour, minute, second
- hour, minute, second
Additionally subset properties have different options. Here arethe options for weekday:
- "long" (e.g., Monday),
- "short" (e.g., Mon),
- "narrow" (e.g., M).
Example:
var date = new Date(Date.UTC(2019, 6, 11, 3, 0, 0));
// calculate a weekday along with a long date in Japanese
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
console.log(new Intl.DateTimeFormat('ja-JP', options).format(date)); // expected output: "2019年11月7日水曜日"
// an application may want to use UTC and make that visible in German
options.timeZone = 'UTC'; options.timeZoneName = 'short';
console.log(new Intl.DateTimeFormat('es-ES', options).format(date)); // expected output: "Jueves, 11 Julio 2019, UTC"
// sometimes you want to be more precise in Australian English
options = { hour: 'numeric', minute: 'numeric', second: 'numeric', timeZone: 'Australia/Sydney', timeZoneName: 'short' };
console.log(new Intl.DateTimeFormat('en-AU', options).format(date)); // expected output: "2:00:00 pm AEDT"
// even the US needs 24-hour time for some occasions
options = { year: 'numeric', month: 'numeric', day: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric', hour12: false, timeZone: 'America/Los_Angeles' };
console.log(new Intl.DateTimeFormat('en-US', options).format(date)); // expected output: "11/7/2019, 16:00:00"
// to specify options but use the browser's default locale, use 'default'
console.log(new Intl.DateTimeFormat('default', options).format(date)); // expected output: "11/7/2019, 16:00:00"
This is a fantastic feature. It will allow you to correctly translate dates and times and format them in so many specific ways that packages like moment.js, created specifically for the analysis, validation and manipulation of JavaScript dates and times, will become useless.
The International Relative Time Format
The Intl.RelativeTimeFormat object is a constructor for objects that enable language-sensitive relative time formatting.
Like the Intl.DateTimeFormat
, this object accepts a locales parameter and an options object.
Like for options
, has a variety properties such as:
- localeMatcher ---> a locale matching algorithm, with the options "best fit" or "lookup",
- numeric ---> the format of the output message,
- style ---> the length of the internationalized message.
style follows the same lines as weekday, in that it has three different values:
- "long" (default, ex. 5 months),
- "short" (ex in 5 mo.),
- or "narrow" (ex, in 5 mo.).
narrow
and short
could be the same depending on the country.
International List Format
The Intl.ListFormat object is a constructor for objects that enable language-sensitive list formatting.
It's hard enough to turn an array in a language into something that looks like a list understandable to people with the addition of ands or ors, so...try to do it in multiple languages.
the first optional parameter accepted by ListFormat is a locales specification.
The second object consists of options
with parameters like:
- localeMatcher — same options as with Intl.RelativeTimeFormat,
- type - the format of the output message. Possible values are "conjunction", which stands for "and"-based lists ( 1, 2, and 3) or ( 1, 2, or 3).
- style - the length of the formatted message. Values include "long", "short", or "narrow".
Example
const vehicles = ['Motorcycle', 'Bus', 'Car']; const formatter = new Intl.ListFormat('en', { style: 'long', type: 'conjunction' }); console.log(formatter.format(vehicles)); // expected output: "Motorcycle, Bus, and Car" const formatter2 = new Intl.ListFormat('de', { style: 'short', type: 'disjunction' }); console.log(formatter2.format(vehicles)); // expected output: "Motorcycle, Bus oder Car" const formatter3 = new Intl.ListFormat('en', { style: 'narrow', type: 'unit' }); console.log(formatter3.format(vehicles)); // expected output: "Motorcycle Bus Car"
The International Number Format
The Intl.NumberFormat object is a constructor for objects that enable language sensitive number formatting.
Although the Intl.NumberFormat definition may seem complicated, the implications are considerable. It is an object builder that allows the formatting of language-sensitive numbers. And do you know what that means? Formatting for international currencies is greatly simplified.
As well as the locales and nu - the numbering system to be used, the Intl.NumberFormat object can accept the following options parameters:
- localeMatcher,
- style - Possible values are "decimal" for plain number formatting, "currency" for currency formatting, and "percent" for percent formatting; "decimal” is the default style.
- currency - The currency to use in currency formatting. Possible values are the ISO 4217 currency codes, such as "USD" for the US dollar, "EUR" for the euro, or "CNY" for the Chinese RMB. The options are "symbol" like €, "code" to use the ISO currency code, or "name" such as "dollar".
- useGrouping - Whether to use grouping separators, such as thousands separators or thousand/lakh/crore separators. Possible values are true and false; the default is true.
We believe that the options minimumIntegerDigits and maximumSignificantDigits, have a limited utility, for now
var number = 123456.789; console.log(new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(number)); // expected output: "123.456,79 €" // the Japanese yen doesn't use a minor unit console.log(new Intl.NumberFormat('ja-JP', { style: 'currency', currency: 'JPY' }).format(number)); // expected output: "¥123,457" // limit to three significant digits console.log(new Intl.NumberFormat('en-IN', { maximumSignificantDigits: 3 }).format(number)); // expected output: "1,23,000"
The International Plural Rules
The Intl.PluralRules object is a constructor for objects that enable plural sensitive formatting and plural language rules.
When you think about the rules for plurals in English, you will surely come to the conclusion that this is quite complicated: "a dog" becomes "two dogs" but "a goose" becomes "two geese", "one" becomes "two" but beyond "two" becomes "few" or "many" or "others" when describing a vague and larger number of objects, and so on, and so on - do you understand where we want to go?
Now imagine you have to know unwritten rules like this in more than one language..... like us writing in English, Italian and spanol :-).
In this case the International Plural Rules become a lifesaver.
Here's the parameters accepted:
International Plural Ruler accepts locales, and its options object takes in the following parameters:
- localeMatcher — The locale matching algorithm to use. Possible values are "lookup" and "best fit"; the default is "best fit".
- type — the type to use. Possible values are: "cardinal" for cardinal numbers (referring to the quantity of things). This is the default value. Or "ordinal" for ordinal number (referring to the ordering or ranking of things, e.g. "1st", "2nd", "3rd" in English).
You can use Intl.PluralRules in several ways:
1. Without specifying a locale: a string formatted in the default locale and with the default options is returned. This meyhod is useful for distinguishing between singular and plural forms such as "cats" and "cats".
2. Use locales, in order to get the format of the language used in the user interface of your application.
3. Plural results can be customized using the options argument with the property type that you can set to ordinal.
Conclusions
Translating websites and applications is quite challenging, but learning how to correctly manage date and time formats, currencies and item lists with their numerous specific language rules is a real challenge.
The latest version of ECMAScript Internationalization API aims to make some of these difficult tasks smoother by letting its formatting methods, based on locales and any other option you want to specify.
For JavaScript developers working with international requirements, like us, this is a huge step forward.

Janeth Kent
Licenciada en Bellas Artes y programadora por pasión. Cuando tengo un rato retoco fotos, edito vídeos y diseño cosas. El resto del tiempo escribo en MA-NO WEB DESIGN AND DEVELOPMENT.
Related Posts
Mastering array sorting in JavaScript: a guide to the sort() function
In this article, I will explain the usage and potential of the sort() function in JavaScript. What does the sort() function do? The sort() function allows you to sort the elements of…
Infinite scrolling with native JavaScript using the Fetch API
I have long wanted to talk about how infinite scroll functionality can be implemented in a list of items that might be on any Web page. Infinite scroll is a technique…
Sorting elements with SortableJS and storing them in localStorage
SortableJS is a JavaScript extension that you will be able to use in your developments to offer your users the possibility to drag and drop elements in order to change…
What is a JWT token and how does it work?
JWT tokens are a standard used to create application access tokens, enabling user authentication in web applications. Specifically, it follows the RFC 7519 standard. What is a JWT token A JWT token…
Template Literals in JavaScript
Template literals, also known as template literals, appeared in JavaScript in its ES6 version, providing a new method of declaring strings using inverted quotes, offering several new and improved possibilities. About…
How to use the endsWith method in JavaScript
In this short tutorial, we are going to see what the endsWith method, introduced in JavaScript ES6, is and how it is used with strings in JavaScript. The endsWith method is…
What are javascript symbols and how can they help you?
Symbols are a new primitive value introduced by ES6. Their purpose is to provide us unique identifiers. In this article, we tell you how they work, in which way they…
Callbacks in JavaScript
Callback functions are the same old JavaScript functions. They have no special syntax, as they are simply functions that are passed as an argument to another function. The function that receives…
How to create PDF with JavaScript and jsPDF
Creating dynamic PDF files directly in the browser is possible thanks to the jsPDF JavaScript library. In the last part of this article we have prepared a practical tutorial where I…
How to make your own custom cursor for your website
When I started browsing different and original websites to learn from them, one of the first things that caught my attention was that some of them had their own cursors,…
Node.js and npm: introductory tutorial
In this tutorial we will see how to install and use both Node.js and the npm package manager. In addition, we will also create a small sample application. If you…
How to connect to MySQL with Node.js
Let's see how you can connect to a MySQL database using Node.js, the popular JavaScript runtime environment. Before we start, it is important to note that you must have Node.js installed…