JavaScript 的國際化是怎樣工作的?
在本文中,你將瞭解國際化在 JavaScript 中的工作原理。國際化是準備軟體以使其能夠支援本地語言和文化環境的過程。它可以包括更改日期和時間格式、更改公制系統格式、語言格式等。
示例 1
在此示例中,我們瞭解日期和時間格式的更改。
var inputDate = new Date(1990, 2, 25); console.log("The date is defined as: ") console.log(inputDate) console.log("The date in United Kingdom format is: ") console.log(new Intl.DateTimeFormat('en-GB').format(inputDate)); console.log("The date in American format is: ") console.log(new Intl.DateTimeFormat('en-US').format(inputDate));
說明
步驟 1 − 將日期時間值定義為“inputDate”變數。
步驟 2 − 使用 DateTimeFormat('en-GB') 函式將日期時間值轉換為英國特定的格式。顯示轉換後的值。
步驟 3 − 使用 DateTimeFormat('en-US') 函式將日期時間值轉換為美國特定的格式。顯示轉換後的值。
示例 2
var inputNumber = 2153.93; console.log("The number is defined as: ") console.log(inputNumber) console.log("The number after converting to France format is: ") console.log(new Intl.NumberFormat('it-FR').format(inputNumber)); console.log("The number after converting to American format is: ") console.log(new Intl.NumberFormat('us-US').format(inputNumber));
說明
步驟 1 − 將一個數值定義為“inputNumber”變數。
步驟 2 − 使用 NumberFormat('it-FR') 函式將數值轉換為法國特定的格式。顯示轉換後的值。
步驟 3 − 使用 NumberFormat('us-US') 函式轉換特定於美國格式的數字值。轉換後顯示值。
廣告