當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Javascript Intl.getCanonicalLocales()用法及代碼示例

JavaScript中的Intl.getCanonicalLocales()方法是一個標準內置對象,該對象返回包含規範語言環境名稱的數組。

用法:

Intl.getCanonicalLocales(locales)

參數:此方法接受上述和以下描述的單個參數:



  • locales:此參數包含要獲取規範語言環境名稱的字符串值的列表。

返回值:此方法返回一個包含規範語言環境名稱的數組。

以下示例說明了JavaScript中的Intl.getCanonicalLocales()方法:

範例1:

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

輸出:

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

範例2:

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); 
}

輸出:

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

支持的瀏覽器:以下列出了Intl.getCanonicalLocales()方法支持的瀏覽器:

  • 穀歌瀏覽器
  • Firefox
  • Opera
  • Safari
  • Edge



相關用法


注:本文由純淨天空篩選整理自SHUBHAMSINGH10大神的英文原創作品 JavaScript | Intl.getCanonicalLocales() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。