Use Intl
Lynx has no Intl where your app runs. Turn on the APIs your code uses, and code that formats, names or sorts by locale works as it does in a browser.
Why you need it
Without Intl:
new Intl.DisplayNames()throws on a phone, while your tests, which run in Node, pass.toLocaleString()ignores the locale. It formats numbers theen-USway, and dates as01/15/2026, 01:05:09 PM.localeCompare()compares character codes, so'é'sorts after'z'.
Turn it on
List the Intl APIs your code uses in the build's intl option:
Then install the FormatJS polyfills they use. The build stops and lists the ones that are missing. For the three above:
The build adds them to the bundle for iOS and Android, before your code, on both threads. The web preview uses the browser's own Intl, so its bundle doesn't change.
Code you share with a web app then works as it is:
The build tells you what's missing
When your code, or a package it uses, needs an Intl API that intl doesn't list, the build prints a warning for it, with the files and the entry to add:
It looks at the code that goes into the bundle for iOS and Android, on both threads, and warns about:
- The
Intlconstructors an entry adds, such asnew Intl.PluralRules()orIntl.NumberFormat(). WithoutIntlthey throw. - The locale methods given what Lynx ignores:
toLocaleDateString()andtoLocaleTimeString()with a locale or options,localeCompare()with a locale or options, andtoLocaleString()with options other thanminimumFractionDigits,maximumFractionDigitsanduseGrouping, or with a locale that isn't English. These don't throw. They format the fixed way listed above.
It leaves out:
- Code behind a feature check: in a file that tests for
Intl(typeof Intl !== 'undefined','Intl' in globalThis), or for one of its APIs (typeof Intl.PluralRules === 'function'), it reports nothing for what the file checks. - Calls whose arguments are variables, such as
n.toLocaleString(locale, options), since the build can't tell what they hold. - Angular's pipes, which don't use
Intl(see Angular's pipes), and the web preview, whose browser hasIntl. toLocaleString()with only a locale, once the build addsNumberFormatorDateTimeFormat. The build can't tell whether it's called on a date or a number, so either counts for it, including aNumberFormatadded for another entry:RelativeTimeFormatandDateTimeFormatadd it (see what each entry adds). With onlyNumberFormatorRelativeTimeFormatlisted,date.toLocaleString('de-DE')isn't reported, and on the device it still formats the date the fixed way. If you format dates like that, addDateTimeFormattoo.- Everything, in a build with Rsbuild's persistent cache (
performance.buildCache). The build doesn't read a cached module again, so rather than report only the modules that changed, it reports nothing, even with"intlChecks": "error". Turn the cache off for a build you rely on the check for, such as a release.
A package's file counts when it's in the bundle, even if your app never calls the function in it that uses Intl. If the code never runs on a phone, turn the check off.
Make it an error, or turn it off
The intlChecks build option sets how the build reports them:
"warning", the default: prints them, and the build passes."error": fails the build. A build with Rsbuild's persistent cache isn't checked (see above)."off": prints nothing.
Without the Angular CLI, pass pluginAngularLynx({ intlChecks: 'error' }).
What each entry adds
Every entry also needs @formatjs/intl-getcanonicallocales and @formatjs/intl-locale. They add Intl.getCanonicalLocales() and Intl.Locale.
Some entries need others too, which the build adds for you:
NumberFormatusesPluralRules.DateTimeFormatandRelativeTimeFormatuseNumberFormat.
Locales
- The build adds the data of your app's locales:
i18n.sourceLocaleand each locale ini18n.localesin angular.json. One bundle serves every language, so it holds them all. - A locale FormatJS has no data for gets a warning, and formats in your source locale.
- Without a locale, as in
new Intl.NumberFormat()orn.toLocaleString(), they use the phone's language (appLocale) when it's one of your locales. Otherwise they use your source locale. That's howprovideLocale()picks Angular'sLOCALE_ID. - In Lynx Explorer, which sends no
appLocale, it's always your source locale.
Time zones
Dates format in the phone's time zone, as in a browser. The build reads the zone's offsets from the device's clock, summer time included.
The apps ng build --platform makes also send the zone's name as appTimeZone. So resolvedOptions().timeZone is, for example, 'Europe/Paris', and timeZoneName: 'long' gives Central European Summer Time.
Lynx Explorer sends no name. There the zone is 'Etc/Unknown', and its name shows as an offset, such as GMT+02:00.
Other time zones
timeZone: 'UTC' and offsets such as timeZone: '+05:30' work as they are. Other zones, such as timeZone: 'America/New_York', need FormatJS's time zone data. Add it to polyfills:
It adds about 1.6 MB to the bundle, and to the web preview's too, where it does nothing.
Sorting
FormatJS has no Intl.Collator, so the Collator entry is AngularLynx's own.
When you build, it asks Node how each of your locales orders text. Node has the full ICU library browsers sort with. The build asks about:
- the letters of Latin, Greek, Cyrillic, Armenian, Hebrew, Arabic, Devanagari, Thai and Japanese kana
- punctuation and symbols
- their accents and cases
Then it sorts with that, as the Unicode Collation Algorithm does: by letter, then accent, then case. It supports:
- Each language's own letters, such as Swedish
äafterz, Spanishñafternand Czechchafterh - The options
numeric,sensitivity,caseFirstandignorePunctuation
On every pair of 120 test words in 10 languages, it orders them as Node does.
What's approximate
- Chinese, Japanese and Korean ideographs and syllables sort after the other scripts, by Unicode code point. There's no pinyin or stroke order.
- Contractions of three letters or more, such as Hungarian
dzs, sort as their letters. So do Thai's vowels that come before a consonant. - Text written as a letter plus a separate accent (
a+ U+0308) sorts as the letter, in a language that makes the accented letter one of its own, as Swedish doesä. A phone's keyboard types an accented letter as one character, so this is rare. usage: 'search'compares as'sort'does.- The
collationoption ('phonebk','pinyin') is ignored:resolvedOptions().collationis always'default'.
Bundle size
Intl data is large, so add only what your code uses. Each API's code and data go into both threads' halves of the bundle, so they count twice in its size.
Measured as what each adds to the iOS and Android bundle:
About 550 kB of each is the same code: Intl.Locale and Intl.getCanonicalLocales, with their data. So two entries add less than their sum.
If the bundle outgrows the initial budget in angular.json, raise it.
Angular's pipes
Angular's DatePipe, DecimalPipe, CurrencyPipe, PercentPipe and I18nPluralPipe don't use Intl. They format with the locale data you register with registerLocaleData(), for the LOCALE_ID that provideLocale() sets.
So they work without the intl option, and the option doesn't change them. For each locale:
- Register Angular's data for the pipes.
- List the locale in
i18n.localesfor code that callsIntl.
Both follow the phone's language the same way.
Without the Angular CLI
If you run RSpeedy yourself, or your build target in angular.json isn't AngularLynx's, pass the same list to the plugin in lynx.config.ts:
Related
- Internationalization
- Read and switch the locale
- The build's options: the
intlandintlChecksoptions - The JavaScript environment
