How to use Intl API in JavaScript

The Intl API simplifies formatting and manipulating text, numbers, dates, and internationalized currencies. It allows you to handle different data formats by language.

This API solves the difficulty of formatting data for different cultures and languages. It makes it easier to format numbers with currency or date symbols for specific regions.

Using the Intl API, you can create web applications that are accessible and easy to use for people from different regions and cultures.

Get user language

The JavaScript constructors provided by the International API define the language they will use to format dates, text, numbers, etc., according to a familiar pattern. Each constructor takes a language and an options object as arguments. Using these arguments, the constructor matches the requested language(s) with the currently supported languages.

To get the user's language, you can use the navigator.language property . This property returns a string, representing the browser's language version.

 

The value of the navigator.language attribute is the BCP 47 language tag. It contains a language and area code (optional) and other sub-tags. For example, 'en-US' represents English spoken in the US.

You can also use the navigator.languages ​​property to get an array of user-favorite languages, and then sort them in order of preference. The first item in the array represents the main language configuration.

You can create a simple JavaScript function that helps you get the user's language. Here is the sample code:

const getUserLocale = () => {   if (navigator.languages && navigator.languages.length) {     return navigator.languages[0];   }   return navigator.language; }; console.log(getUserLocale());

The getUserLocale function returns the first element of the navigator.languages ​​property if it is available. Otherwise, it will fall back to the navigator.language property, which represents the user's preferred language in older browsers.

Date format for different languages

Format dates using the Intl API, you can use the Intl.DateTimeFormat() constructor. It takes two arguments: a language string and an options object.

Options objects can contain properties that control date formatting.

Some popular choices include:

  1. Weekday : Format the day of the week.
  2. Year : Year format.
  3. Month : Month format.
  4. Day : Date format.

Example how to format date using Intl.DateTimeFormat() constructor:

const date = Date.now() const locale = getUserLocale(); const options = {   weekday: "long",   year: "numeric",   month: "long",   day: "numeric", }; const formatter = new Intl.DateTimeFormat(locale, options); // weekday, month date, year (Friday, March 24, 2023) console.log(formatter.format(date));

This code sets up a formatter object by passing the user's locale to Intl.DateTimeFormat() , along with a group of choices. It then uses the formatter to turn the current date into a string. The options object contains properties that control the format of the date.

Above is how to use Intl API in JavaScript . Hope the article is useful to you.

4.5 ★ | 2 Vote

May be interested

  • Summary of JavaScript exercises with sample codeSummary of JavaScript exercises with sample code
    in order to make your javascript learning easier, tipsmake.com has compiled a number of javascript exercises with sample solutions for you to practice.
  • JavaScript location in HTML FileJavaScript location in HTML File
    there is flexibility in providing javascript code anywhere in an html document. however, the most preferred ways to include javascript in an html file.
  • How to Turn on JavaScriptHow to Turn on JavaScript
    javascript is a language used in many websites to provide additional functionality and access to the user. developers use javascript to deliver a large part of their site's functionality to you, the user. your browser must have javascript...
  • What is the difference between Java and JavaScript?What is the difference between Java and JavaScript?
    although the names of java and javascript seem to be related (javascript seems to be a script in java?), but that's the only similarity. these two languages ​​are not technically related to each other.
  • Void keywords in JavaScriptVoid keywords in JavaScript
    void is an important keyword in javascript that can be used as a unary operator before its single operand, which can be in any type. this operator defines an expression to be evaluated without returning a value.
  • JavaScript code to generate error charts & graphsJavaScript code to generate error charts & graphs
    the example below illustrates a sample variance chart created with javascript that incorporates a column chart. you will also get the source code for reference and editing as you wish.
  • Introduction to 2D Array - 2-dimensional array in JavaScriptIntroduction to 2D Array - 2-dimensional array in JavaScript
    in the following article, we will introduce and show you some basic operations to create and access 2-dimensional arrays - 2d array in javascript. essentially, a 2-dimensional array is a concept of a matrix of matrices - a matrix, used to store information. each 1 element contains 2 separate indexes: row (y) - column and column (x) - column.
  • Operator in JavaScriptOperator in JavaScript
    we see the following simple expression: 4 + 5 is equal to 9. here 4 and 5 are operands and '+' called operator - operator.
  • How to Disable JavaScriptHow to Disable JavaScript
    this wikihow teaches you how to turn off your web browser's javascript support. javascript is responsible for loading dynamic content in webpages, so turning it off can help websites load faster than usual. most web browsers and their...
  • Browser compatibility in JavaScriptBrowser compatibility in JavaScript
    it is extremely important to understand the differences between different web browsers to handle each one in the best way. therefore, you must know which browser your website is running from so that it can provide a suitable solution.