Internationalization Support
Node.js has many features that make it easier to write internationalized programs. Some of them are:- Locale-sensitive or Unicode-aware functions in the ECMAScript Language Specification:
String.prototype.normalize()String.prototype.toLowerCase()String.prototype.toUpperCase()
- All functionality described in the ECMAScript Internationalization API Specification (ECMA-402):
Intlobject- Locale-sensitive methods like
String.prototype.localeCompare()andDate.prototype.toLocaleString()
- The WHATWG URL parser’s internationalized domain names (IDNs) support
require('node:buffer').transcode()- More accurate REPL line editing
require('node:util').TextDecoderRegExpUnicode Property Escapes
Options for Building Node.js
To control how ICU is used in Node.js, fourconfigure options are available during compilation:
--with-intl=none/--without-intl--with-intl=system-icu--with-intl=small-icu--with-intl=full-icu(default)
Feature Availability
| Feature | none | system-icu | small-icu | full-icu |
|---|---|---|---|---|
String.prototype.normalize() | none (no-op) | full | full | full |
Intl object | none | partial/full | partial (English-only) | full |
String.prototype.localeCompare() | partial | full | full | full |
| WHATWG URL Parser (IDN support) | none | full | full | full |
require('node:buffer').transcode() | none | full | full | full |
| REPL | partial | full | full | full |
require('node:util').TextDecoder | partial | partial/full | partial (Unicode-only) | full |
RegExp Unicode Property Escapes | none | full | full | full |
Disable All Internationalization Features (none)
If this option is chosen, ICU is disabled and most internationalization features mentioned above will be unavailable in the resultingnode binary.
Build with Pre-installed ICU (system-icu)
Node.js can link against an ICU build already installed on the system. In fact, most Linux distributions already come with ICU installed, and this option would make it possible to reuse the same set of data used by other components in the OS. Functionalities that only require the ICU library itself are fully supported. Features that require ICU locale data may be fully or partially supported, depending on the completeness of the ICU data installed on the system.Embed Limited ICU Data (small-icu)
This option makes the resulting binary link against the ICU library statically, and includes a subset of ICU data (typically only the English locale) within thenode executable.
Functionalities that only require the ICU library itself are fully supported. Features that require ICU locale data generally only work with the English locale:
Providing ICU Data at Runtime
If thesmall-icu option is used, one can still provide additional locale data at runtime so that the JS methods would work for all ICU locales. The data file can be made available to ICU through:
-
The
--with-icu-default-data-dirconfigure option: -
The
NODE_ICU_DATAenvironment variable: -
The
--icu-data-dirCLI parameter:
--icu-data-dir CLI parameter has the highest precedence, then the NODE_ICU_DATA environment variable, then the --with-icu-default-data-dir configure option.
The most common name for the data file is icudtX[bl].dat, where X denotes the intended ICU version, and b or l indicates the system’s endianness. The name of the data file corresponding to the current Node.js version can be computed with:
Embed the Entire ICU (full-icu)
This option makes the resulting binary link against ICU statically and include a full set of ICU data. A binary created this way has no further external dependencies and supports all locales, but might be rather large. This is the default behavior if no--with-intl flag is passed. The official binaries are also built in this mode.
Detecting Internationalization Support
To verify that ICU is enabled at all (system-icu, small-icu, or full-icu), simply checking the existence of Intl should suffice:
process.versions.icu, a property defined only when ICU is enabled, works too:
full-icu or system-icu), Intl.DateTimeFormat can be a good distinguishing factor:
Using Intl API
Date and Time Formatting
Number Formatting
Collation (String Comparison)
Plural Rules
List Formatting
Relative Time Formatting
String Normalization
Best Practices
- Always use
IntlAPIs for locale-sensitive operations instead of custom implementations - Test with multiple locales to ensure your application works correctly in different languages
- Use appropriate ICU build based on your deployment requirements
- Cache formatters when possible as creating new
Intlobjects can be expensive - Handle missing locale data gracefully when using
small-icu