-
Notifications
You must be signed in to change notification settings - Fork 22.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reorganize JS numbers/dates/strings guides
- Loading branch information
Showing
19 changed files
with
355 additions
and
468 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
files/en-us/web/javascript/guide/internationalization/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
--- | ||
title: Internationalization | ||
slug: Web/JavaScript/Guide/Internationalization | ||
page-type: guide | ||
--- | ||
|
||
{{jsSidebar("JavaScript Guide")}} {{PreviousNext("Web/JavaScript/Guide/Iterators_and_generators", "Web/JavaScript/Guide/Meta_programming")}} | ||
|
||
The {{jsxref("Intl")}} object is the namespace for the ECMAScript Internationalization API, which provides a wide range of locale- and culture-sensitive data and operations. | ||
|
||
## Date and time formatting | ||
|
||
The {{jsxref("Intl.DateTimeFormat")}} object is useful for formatting date and time. The following formats a date for English as used in the United States. (The result is different in another time zone.) | ||
|
||
```js | ||
// July 17, 2014 00:00:00 UTC: | ||
const july172014 = new Date("2014-07-17"); | ||
|
||
const options = { | ||
year: "2-digit", | ||
month: "2-digit", | ||
day: "2-digit", | ||
hour: "2-digit", | ||
minute: "2-digit", | ||
timeZoneName: "short", | ||
}; | ||
const americanDateTime = new Intl.DateTimeFormat("en-US", options).format; | ||
|
||
// Local timezone vary depending on your settings | ||
// In CEST, logs: 07/17/14, 02:00 AM GMT+2 | ||
// In PDT, logs: 07/16/14, 05:00 PM GMT-7 | ||
console.log(americanDateTime(july172014)); | ||
``` | ||
|
||
## Number formatting | ||
|
||
The {{jsxref("Intl.NumberFormat")}} object is useful for formatting numbers, for example currencies. | ||
|
||
```js | ||
const gasPrice = new Intl.NumberFormat("en-US", { | ||
style: "currency", | ||
currency: "USD", | ||
minimumFractionDigits: 3, | ||
}); | ||
|
||
console.log(gasPrice.format(5.259)); // $5.259 | ||
|
||
const hanDecimalRMBInChina = new Intl.NumberFormat("zh-CN-u-nu-hanidec", { | ||
style: "currency", | ||
currency: "CNY", | ||
}); | ||
|
||
console.log(hanDecimalRMBInChina.format(1314.25)); // ¥ 一,三一四.二五 | ||
``` | ||
|
||
## Collation | ||
|
||
The {{jsxref("Intl.Collator")}} object is useful for comparing and sorting strings. | ||
|
||
For example, there are actually two different sort orders in German, _phonebook_ and _dictionary_. Phonebook sort emphasizes sound, and it's as if "ä", "ö", and so on were expanded to "ae", "oe", and so on prior to sorting. | ||
|
||
```js | ||
const names = ["Hochberg", "Hönigswald", "Holzman"]; | ||
|
||
const germanPhonebook = new Intl.Collator("de-DE-u-co-phonebk"); | ||
|
||
// as if sorting ["Hochberg", "Hoenigswald", "Holzman"]: | ||
console.log(names.sort(germanPhonebook.compare).join(", ")); | ||
// "Hochberg, Hönigswald, Holzman" | ||
``` | ||
|
||
Some German words conjugate with extra umlauts, so in dictionaries it's sensible to order ignoring umlauts (except when ordering words differing _only_ by umlauts: _schon_ before _schön_). | ||
|
||
```js | ||
const germanDictionary = new Intl.Collator("de-DE-u-co-dict"); | ||
|
||
// as if sorting ["Hochberg", "Honigswald", "Holzman"]: | ||
console.log(names.sort(germanDictionary.compare).join(", ")); | ||
// "Hochberg, Holzman, Hönigswald" | ||
``` | ||
|
||
For more information about the {{jsxref("Intl")}} API, see also [Introducing the JavaScript Internationalization API](https://hacks.mozilla.org/2014/12/introducing-the-javascript-internationalization-api/). | ||
|
||
{{PreviousNext("Web/JavaScript/Guide/Iterators_and_generators", "Web/JavaScript/Guide/Meta_programming")}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.