{ "type": "module", "source": "doc/api/intl.md", "introduced_in": "v8.2.0", "miscs": [ { "textRaw": "Internationalization support", "name": "Internationalization support", "introduced_in": "v8.2.0", "type": "misc", "desc": "
Node.js has many features that make it easier to write internationalized\nprograms. Some of them are:
\nIntl objectString.prototype.localeCompare() and\nDate.prototype.toLocaleString()require('node:buffer').transcode()require('node:util').TextDecoderRegExp Unicode Property EscapesNode.js and the underlying V8 engine use\nInternational Components for Unicode (ICU) 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.
", "miscs": [ { "textRaw": "Options for building Node.js", "name": "options_for_building_node.js", "desc": "To control how ICU is used in Node.js, four configure options are available\nduring compilation. Additional details on how to compile Node.js are documented\nin BUILDING.md.
--with-intl=none/--without-intl--with-intl=system-icu--with-intl=small-icu--with-intl=full-icu (default)An overview of available Node.js and JavaScript features for each configure\noption:
| Feature | \nnone | \nsystem-icu | \nsmall-icu | \nfull-icu | \n
|---|---|---|---|---|
String.prototype.normalize() | \nnone (function is no-op) | \nfull | \nfull | \nfull | \n
String.prototype.to*Case() | \nfull | \nfull | \nfull | \nfull | \n
Intl | \nnone (object does not exist) | \npartial/full (depends on OS) | \npartial (English-only) | \nfull | \n
String.prototype.localeCompare() | \npartial (not locale-aware) | \nfull | \nfull | \nfull | \n
String.prototype.toLocale*Case() | \npartial (not locale-aware) | \nfull | \nfull | \nfull | \n
Number.prototype.toLocaleString() | \npartial (not locale-aware) | \npartial/full (depends on OS) | \npartial (English-only) | \nfull | \n
Date.prototype.toLocale*String() | \npartial (not locale-aware) | \npartial/full (depends on OS) | \npartial (English-only) | \nfull | \n
| Legacy URL Parser | \npartial (no IDN support) | \nfull | \nfull | \nfull | \n
| WHATWG URL Parser | \npartial (no IDN support) | \nfull | \nfull | \nfull | \n
require('node:buffer').transcode() | \nnone (function does not exist) | \nfull | \nfull | \nfull | \n
| REPL | \npartial (inaccurate line editing) | \nfull | \nfull | \nfull | \n
require('node:util').TextDecoder | \npartial (basic encodings support) | \npartial/full (depends on OS) | \npartial (Unicode-only) | \nfull | \n
RegExp Unicode Property Escapes | \nnone (invalid RegExp error) | \nfull | \nfull | \nfull | \n
The \"(not locale-aware)\" designation denotes that the function carries out its\noperation just like the non-Locale version of the function, if one\nexists. For example, under none mode, Date.prototype.toLocaleString()'s\noperation is identical to that of Date.prototype.toString().
If this option is chosen, ICU is disabled and most internationalization\nfeatures mentioned above will be unavailable in the resulting node binary.
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.
\nFunctionalities that only require the ICU library itself, such as\nString.prototype.normalize() and the WHATWG URL parser, are fully\nsupported under system-icu. Features that require ICU locale data in\naddition, such as Intl.DateTimeFormat may be fully or partially\nsupported, depending on the completeness of the ICU data installed on the\nsystem.
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 node executable.
Functionalities that only require the ICU library itself, such as\nString.prototype.normalize() and the WHATWG URL parser, are fully\nsupported under small-icu. Features that require ICU locale data in addition,\nsuch as Intl.DateTimeFormat, generally only work with the English locale:
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 either \"M01\" or \"January\" on small-icu, depending on the user’s default locale\n// Should print \"enero\"\n\nThis mode provides a balance between features and binary size.
", "modules": [ { "textRaw": "Providing ICU data at runtime", "name": "providing_icu_data_at_runtime", "desc": "If the small-icu 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 /some/directory, it can be made available to ICU\nthrough either:
The NODE_ICU_DATA environment variable:
env NODE_ICU_DATA=/some/directory node\n\nThe --icu-data-dir CLI parameter:
node --icu-data-dir=/some/directory\n\n(If both are specified, the --icu-data-dir CLI parameter takes precedence.)
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 icudt6X[bl].dat, where 6X denotes\nthe intended ICU version, and b or l indicates the system's endianness.\nCheck \"ICU Data\" article in the ICU User Guide for other supported formats\nand more details on ICU data in general.
The full-icu npm module can greatly simplify ICU data installation by\ndetecting the ICU version of the running node executable and downloading the\nappropriate data file. After installing the module through npm i full-icu,\nthe data file will be available at ./node_modules/full-icu. This path can be\nthen passed either to NODE_ICU_DATA or --icu-data-dir as shown above to\nenable full Intl support.
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 --with-intl flag is passed. The official binaries\nare also built in this mode.
To verify that ICU is enabled at all (system-icu, small-icu, or\nfull-icu), simply checking the existence of Intl should suffice:
const hasICU = typeof Intl === 'object';\n\nAlternatively, checking for process.versions.icu, a property defined only\nwhen ICU is enabled, works too:
const hasICU = typeof process.versions.icu === 'string';\n\nTo check for support for a non-English locale (i.e. full-icu or\nsystem-icu), Intl.DateTimeFormat can be a good distinguishing factor:
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\nFor more verbose tests for Intl support, the following resources may be found\nto be helpful: