Open In App

JavaScript Intl.getCanonicalLocales() Method

Last Updated : 27 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The Intl.getCanonicalLocales() method in JavaScript is a Standard built-in object which returns an array containing the canonical locale names. Elements will be checked for structural validity and duplicates will be removed.

Syntax: 

Intl.getCanonicalLocales(locales)

Parameters: This method accepts a single parameter as mentioned above and described below: 

  • locales: This parameter holds a list of string values for which to get the canonical locale names.

Return value: This method returns an array containing the canonical locale names.

Below examples illustrate the Intl.getCanonicalLocales() method in JavaScript:

Example 1: This example shiows the use of the Intl.getCanonicalLocales() method in JavaScript.

javascript




<script>
    console.log(Intl.getCanonicalLocales('EN-US'));
    console.log(Intl.getCanonicalLocales(['EN-US', 'Fr']));
      
    try {
        Intl.getCanonicalLocales('GeeksforGeeks');
    } catch (error) {
        console.log(error);
    }
</script>


Output: 

Array ["en-US"]
Array ["en-US", "fr"]
RangeError: Incorrect locale information provided

Example 2: This example shiows the use of the Intl.getCanonicalLocales() method in JavaScript.

javascript




<script>
    console.log(Intl.getCanonicalLocales('EN-US'));
    console.log(Intl.getCanonicalLocales(['EN-US', 'Fr']));
      
    try {
        console.log(Intl.getCanonicalLocales(['Tr', 'UT']));
    } catch (error) {
        console.log(error);
    }
</script>


Output: 

Array ["en-US"]
Array ["en-US", "fr"]
Array ["tr", "ut"]

Supported Browsers: The browsers supported by Intl.getCanonicalLocales() method are listed below: 

  • Google Chrome 54 and above
  • Firefox 48 and above
  • Safari 11 and above
  • Edge 16 and above


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads