• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1{
2  "type": "module",
3  "source": "doc/api/intl.md",
4  "introduced_in": "v8.2.0",
5  "miscs": [
6    {
7      "textRaw": "Internationalization support",
8      "name": "Internationalization support",
9      "introduced_in": "v8.2.0",
10      "type": "misc",
11      "desc": "<p>Node.js has many features that make it easier to write internationalized\nprograms. Some of them are:</p>\n<ul>\n<li>Locale-sensitive or Unicode-aware functions in the <a href=\"https://tc39.github.io/ecma262/\">ECMAScript Language\nSpecification</a>:\n<ul>\n<li><a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize\"><code>String.prototype.normalize()</code></a></li>\n<li><a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase\"><code>String.prototype.toLowerCase()</code></a></li>\n<li><a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase\"><code>String.prototype.toUpperCase()</code></a></li>\n</ul>\n</li>\n<li>All functionality described in the <a href=\"https://tc39.github.io/ecma402/\">ECMAScript Internationalization API\nSpecification</a> (aka ECMA-402):\n<ul>\n<li><a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl\"><code>Intl</code></a> object</li>\n<li>Locale-sensitive methods like <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare\"><code>String.prototype.localeCompare()</code></a> and\n<a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString\"><code>Date.prototype.toLocaleString()</code></a></li>\n</ul>\n</li>\n<li>The <a href=\"url.html#url_the_whatwg_url_api\">WHATWG URL parser</a>'s <a href=\"https://en.wikipedia.org/wiki/Internationalized_domain_name\">internationalized domain names</a> (IDNs) support</li>\n<li><a href=\"buffer.html#buffer_buffer_transcode_source_fromenc_toenc\"><code>require('buffer').transcode()</code></a></li>\n<li>More accurate <a href=\"repl.html#repl_repl\">REPL</a> line editing</li>\n<li><a href=\"util.html#util_class_util_textdecoder\"><code>require('util').TextDecoder</code></a></li>\n<li><a href=\"https://github.com/tc39/proposal-regexp-unicode-property-escapes\"><code>RegExp</code> Unicode Property Escapes</a></li>\n</ul>\n<p>Node.js and the underlying V8 engine use\n<a href=\"http://site.icu-project.org/\">International Components for Unicode (ICU)</a> to implement these features\nin native C/C++ code. The full ICU data set is provided by Node.js by default.\nHowever, due to the size of the ICU data file, several\noptions are provided for customizing the ICU data set either when\nbuilding or running Node.js.</p>",
12      "miscs": [
13        {
14          "textRaw": "Options for building Node.js",
15          "name": "options_for_building_node.js",
16          "desc": "<p>To control how ICU is used in Node.js, four <code>configure</code> options are available\nduring compilation. Additional details on how to compile Node.js are documented\nin <a href=\"https://github.com/nodejs/node/blob/HEAD/BUILDING.md\">BUILDING.md</a>.</p>\n<ul>\n<li><code>--with-intl=none</code>/<code>--without-intl</code></li>\n<li><code>--with-intl=system-icu</code></li>\n<li><code>--with-intl=small-icu</code></li>\n<li><code>--with-intl=full-icu</code> (default)</li>\n</ul>\n<p>An overview of available Node.js and JavaScript features for each <code>configure</code>\noption:</p>\n<table>\n<thead>\n<tr>\n<th>Feature</th>\n<th><code>none</code></th>\n<th><code>system-icu</code></th>\n<th><code>small-icu</code></th>\n<th><code>full-icu</code></th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize\"><code>String.prototype.normalize()</code></a></td>\n<td>none (function is no-op)</td>\n<td>full</td>\n<td>full</td>\n<td>full</td>\n</tr>\n<tr>\n<td><code>String.prototype.to*Case()</code></td>\n<td>full</td>\n<td>full</td>\n<td>full</td>\n<td>full</td>\n</tr>\n<tr>\n<td><a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl\"><code>Intl</code></a></td>\n<td>none (object does not exist)</td>\n<td>partial/full (depends on OS)</td>\n<td>partial (English-only)</td>\n<td>full</td>\n</tr>\n<tr>\n<td><a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare\"><code>String.prototype.localeCompare()</code></a></td>\n<td>partial (not locale-aware)</td>\n<td>full</td>\n<td>full</td>\n<td>full</td>\n</tr>\n<tr>\n<td><code>String.prototype.toLocale*Case()</code></td>\n<td>partial (not locale-aware)</td>\n<td>full</td>\n<td>full</td>\n<td>full</td>\n</tr>\n<tr>\n<td><a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString\"><code>Number.prototype.toLocaleString()</code></a></td>\n<td>partial (not locale-aware)</td>\n<td>partial/full (depends on OS)</td>\n<td>partial (English-only)</td>\n<td>full</td>\n</tr>\n<tr>\n<td><code>Date.prototype.toLocale*String()</code></td>\n<td>partial (not locale-aware)</td>\n<td>partial/full (depends on OS)</td>\n<td>partial (English-only)</td>\n<td>full</td>\n</tr>\n<tr>\n<td><a href=\"url.html#url_the_whatwg_url_api\">WHATWG URL Parser</a></td>\n<td>partial (no IDN support)</td>\n<td>full</td>\n<td>full</td>\n<td>full</td>\n</tr>\n<tr>\n<td><a href=\"buffer.html#buffer_buffer_transcode_source_fromenc_toenc\"><code>require('buffer').transcode()</code></a></td>\n<td>none (function does not exist)</td>\n<td>full</td>\n<td>full</td>\n<td>full</td>\n</tr>\n<tr>\n<td><a href=\"repl.html#repl_repl\">REPL</a></td>\n<td>partial (inaccurate line editing)</td>\n<td>full</td>\n<td>full</td>\n<td>full</td>\n</tr>\n<tr>\n<td><a href=\"util.html#util_class_util_textdecoder\"><code>require('util').TextDecoder</code></a></td>\n<td>partial (basic encodings support)</td>\n<td>partial/full (depends on OS)</td>\n<td>partial (Unicode-only)</td>\n<td>full</td>\n</tr>\n<tr>\n<td><a href=\"https://github.com/tc39/proposal-regexp-unicode-property-escapes\"><code>RegExp</code> Unicode Property Escapes</a></td>\n<td>none (invalid <code>RegExp</code> error)</td>\n<td>full</td>\n<td>full</td>\n<td>full</td>\n</tr>\n</tbody>\n</table>\n<p>The \"(not locale-aware)\" designation denotes that the function carries out its\noperation just like the non-<code>Locale</code> version of the function, if one\nexists. For example, under <code>none</code> mode, <code>Date.prototype.toLocaleString()</code>'s\noperation is identical to that of <code>Date.prototype.toString()</code>.</p>",
17          "modules": [
18            {
19              "textRaw": "Disable all internationalization features (`none`)",
20              "name": "disable_all_internationalization_features_(`none`)",
21              "desc": "<p>If this option is chosen, ICU is disabled and most internationalization\nfeatures mentioned above will be <strong>unavailable</strong> in the resulting <code>node</code> binary.</p>",
22              "type": "module",
23              "displayName": "Disable all internationalization features (`none`)"
24            },
25            {
26              "textRaw": "Build with a pre-installed ICU (`system-icu`)",
27              "name": "build_with_a_pre-installed_icu_(`system-icu`)",
28              "desc": "<p>Node.js can link against an ICU build already installed on the system. In fact,\nmost Linux distributions already come with ICU installed, and this option would\nmake it possible to reuse the same set of data used by other components in the\nOS.</p>\n<p>Functionalities that only require the ICU library itself, such as\n<a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize\"><code>String.prototype.normalize()</code></a> and the <a href=\"url.html#url_the_whatwg_url_api\">WHATWG URL parser</a>, are fully\nsupported under <code>system-icu</code>. Features that require ICU locale data in\naddition, such as <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat\"><code>Intl.DateTimeFormat</code></a> <em>may</em> be fully or partially\nsupported, depending on the completeness of the ICU data installed on the\nsystem.</p>",
29              "type": "module",
30              "displayName": "Build with a pre-installed ICU (`system-icu`)"
31            },
32            {
33              "textRaw": "Embed a limited set of ICU data (`small-icu`)",
34              "name": "embed_a_limited_set_of_icu_data_(`small-icu`)",
35              "desc": "<p>This option makes the resulting binary link against the ICU library statically,\nand includes a subset of ICU data (typically only the English locale) within\nthe <code>node</code> executable.</p>\n<p>Functionalities that only require the ICU library itself, such as\n<a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize\"><code>String.prototype.normalize()</code></a> and the <a href=\"url.html#url_the_whatwg_url_api\">WHATWG URL parser</a>, are fully\nsupported under <code>small-icu</code>. Features that require ICU locale data in addition,\nsuch as <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat\"><code>Intl.DateTimeFormat</code></a>, generally only work with the English locale:</p>\n<pre><code class=\"language-js\">const january = new Date(9e8);\nconst english = new Intl.DateTimeFormat('en', { month: 'long' });\nconst spanish = new Intl.DateTimeFormat('es', { month: 'long' });\n\nconsole.log(english.format(january));\n// Prints \"January\"\nconsole.log(spanish.format(january));\n// Prints \"M01\" on small-icu\n// Should print \"enero\"\n</code></pre>\n<p>This mode provides a balance between features and binary size.</p>",
36              "modules": [
37                {
38                  "textRaw": "Providing ICU data at runtime",
39                  "name": "providing_icu_data_at_runtime",
40                  "desc": "<p>If the <code>small-icu</code> option is used, one can still provide additional locale data\nat runtime so that the JS methods would work for all ICU locales. Assuming the\ndata file is stored at <code>/some/directory</code>, it can be made available to ICU\nthrough either:</p>\n<ul>\n<li>\n<p>The <a href=\"cli.html#cli_node_icu_data_file\"><code>NODE_ICU_DATA</code></a> environment variable:</p>\n<pre><code class=\"language-bash\">env NODE_ICU_DATA=/some/directory node\n</code></pre>\n</li>\n<li>\n<p>The <a href=\"cli.html#cli_icu_data_dir_file\"><code>--icu-data-dir</code></a> CLI parameter:</p>\n<pre><code class=\"language-bash\">node --icu-data-dir=/some/directory\n</code></pre>\n</li>\n</ul>\n<p>(If both are specified, the <code>--icu-data-dir</code> CLI parameter takes precedence.)</p>\n<p>ICU is able to automatically find and load a variety of data formats, but the\ndata must be appropriate for the ICU version, and the file correctly named.\nThe most common name for the data file is <code>icudt6X[bl].dat</code>, where <code>6X</code> denotes\nthe intended ICU version, and <code>b</code> or <code>l</code> indicates the system's endianness.\nCheck <a href=\"http://userguide.icu-project.org/icudata\">\"ICU Data\"</a> article in the ICU User Guide for other supported formats\nand more details on ICU data in general.</p>\n<p>The <a href=\"https://www.npmjs.com/package/full-icu\">full-icu</a> npm module can greatly simplify ICU data installation by\ndetecting the ICU version of the running <code>node</code> executable and downloading the\nappropriate data file. After installing the module through <code>npm i full-icu</code>,\nthe data file will be available at <code>./node_modules/full-icu</code>. This path can be\nthen passed either to <code>NODE_ICU_DATA</code> or <code>--icu-data-dir</code> as shown above to\nenable full <code>Intl</code> support.</p>",
41                  "type": "module",
42                  "displayName": "Providing ICU data at runtime"
43                }
44              ],
45              "type": "module",
46              "displayName": "Embed a limited set of ICU data (`small-icu`)"
47            },
48            {
49              "textRaw": "Embed the entire ICU (`full-icu`)",
50              "name": "embed_the_entire_icu_(`full-icu`)",
51              "desc": "<p>This option makes the resulting binary link against ICU statically and include\na full set of ICU data. A binary created this way has no further external\ndependencies and supports all locales, but might be rather large. This is\nthe default behavior if no <code>--with-intl</code> flag is passed. The official binaries\nare also built in this mode.</p>",
52              "type": "module",
53              "displayName": "Embed the entire ICU (`full-icu`)"
54            }
55          ],
56          "type": "misc",
57          "displayName": "Options for building Node.js"
58        },
59        {
60          "textRaw": "Detecting internationalization support",
61          "name": "detecting_internationalization_support",
62          "desc": "<p>To verify that ICU is enabled at all (<code>system-icu</code>, <code>small-icu</code>, or\n<code>full-icu</code>), simply checking the existence of <code>Intl</code> should suffice:</p>\n<pre><code class=\"language-js\">const hasICU = typeof Intl === 'object';\n</code></pre>\n<p>Alternatively, checking for <code>process.versions.icu</code>, a property defined only\nwhen ICU is enabled, works too:</p>\n<pre><code class=\"language-js\">const hasICU = typeof process.versions.icu === 'string';\n</code></pre>\n<p>To check for support for a non-English locale (i.e. <code>full-icu</code> or\n<code>system-icu</code>), <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat\"><code>Intl.DateTimeFormat</code></a> can be a good distinguishing factor:</p>\n<pre><code class=\"language-js\">const hasFullICU = (() => {\n  try {\n    const january = new Date(9e8);\n    const spanish = new Intl.DateTimeFormat('es', { month: 'long' });\n    return spanish.format(january) === 'enero';\n  } catch (err) {\n    return false;\n  }\n})();\n</code></pre>\n<p>For more verbose tests for <code>Intl</code> support, the following resources may be found\nto be helpful:</p>\n<ul>\n<li><a href=\"https://github.com/srl295/btest402\">btest402</a>: Generally used to check whether Node.js with <code>Intl</code> support is\nbuilt correctly.</li>\n<li><a href=\"https://github.com/tc39/test262/tree/HEAD/test/intl402\">Test262</a>: ECMAScript's official conformance test suite includes a section\ndedicated to ECMA-402.</li>\n</ul>",
63          "type": "misc",
64          "displayName": "Detecting internationalization support"
65        }
66      ]
67    }
68  ]
69}