Documentation
¶
Overview ¶
Package intl gives a beastjs.VM the ECMA-402 Intl API, computed in Go.
QuickJS comes without Intl. Register adds it: the constructors Intl.DateTimeFormat, NumberFormat, PluralRules, Collator, Segmenter, RelativeTimeFormat, ListFormat, DisplayNames, DurationFormat and Locale, the supportedLocalesOf of each but Locale, Intl.getCanonicalLocales and Intl.supportedValuesOf. It also replaces the methods that format with a locale: Date.prototype.toLocaleString, toLocaleDateString and toLocaleTimeString, Number.prototype.toLocaleString, BigInt.prototype.toLocaleString, String.prototype.localeCompare, and String.prototype.toLocaleUpperCase and toLocaleLowerCase, which follow the case rules of Turkish, Azerbaijani, Lithuanian and Greek.
Register also makes Array.prototype.toLocaleString and %TypedArray%.prototype.toLocaleString pass their locales and options on to the toLocaleString of each element, as ECMA-402 says, so that [1234.5].toLocaleString("de") gives "1.234,5". Without Intl they pass none, as ECMA-262 says.
Locales and time zones ¶
Locales resolve as in Chromium: a locale without data of its own falls back to its parent, and then to en-US. With no locale given the locale is en-US, and with no time zone given the zone is the VM's default, UTC unless TimeZone sets another. Nothing is read from the host: not its locale, not its zone, not the TZ variable. The zone data is tz 2026e, built into go-intl, so the program needs neither the host's zoneinfo nor time/tzdata.
Compared with Chromium ¶
The answers of DateTimeFormat, NumberFormat and PluralRules are Chromium 156's, checked against what that browser answered for tens of thousands of expressions (testdata/chromium). The known differences are elsewhere: where Segmenter breaks words, a few names of DisplayNames, the 001 time zones, -u-va-posix and unknown -u-rg- regions of Locale, the currencies SLE, XCG and ZWG that supportedValuesOf("currency") lists in addition, how DurationFormat writes a sum of nanoseconds past 2**63, and the words of error messages. Under tz 2026e, three answers for America/Winnipeg differ from that browser's older data.
Cost ¶
The data takes six packs, 9.35 MB of the binary in all. The build tags beastjs_intl_nonames and beastjs_intl_nocjdict leave out the display names and the Chinese and Japanese word dictionary of Segmenter.
The formatters are shared by all the VMs of the process: a registry keeps up to 1024 of them, and an object whose formatter it dropped makes it again, from the same arguments, on its next call, with the same results. A date formatter is told apart by the pattern it writes with, beside its options. docs/internals/changes/time-and-intl.md has the timings.
License of the data ¶
A program that imports this package carries data from Unicode CLDR, ICU and Chromium, and has to ship intl/LICENSE-DATA and the notices of internal/gointl with it. NOTICE.md says which covers what.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Register ¶
Register gives vm Intl and the locale-aware methods of Date, Number, BigInt and String (see the package documentation).
Every VM needs its own Register, a VM made to replace the realm of another one included. Call it before any code of the page runs: it takes the built-ins Intl works with then, so that a page that later changes Array.prototype, JSON or the like does not change Intl. A second Register on the same VM returns an error.
Register only puts stand-ins in place. Intl is installed the first time a page uses it or one of the methods. Nothing needs to be released: vm.Close frees it all.
Example ¶
vm, err := beastjs.NewVM()
if err != nil {
panic(err)
}
defer vm.Close()
if err := Register(vm, TimeZone("Europe/Prague")); err != nil {
panic(err)
}
for _, src := range []string{
"new Intl.DateTimeFormat('en-US', {dateStyle: 'long', timeStyle: 'short', timeZone: 'America/New_York'}).format(new Date(0))",
"new Date(0).toLocaleString('en-US', {timeZone: 'Asia/Tokyo'})",
"Intl.DateTimeFormat().resolvedOptions().timeZone",
"new Intl.NumberFormat('en-US', {style: 'currency', currency: 'USD'}).format(1234.5)",
} {
v, err := vm.Eval(src, beastjs.EvalGlobal)
if err != nil {
panic(err)
}
fmt.Println(v)
}
Output: December 31, 1969 at 7:00 PM 1/1/1970, 9:00:00 AM Europe/Prague $1,234.50
Types ¶
type Option ¶
type Option func(*config)
An Option configures Register.
func TimeZone ¶
TimeZone sets the default time zone of the VM: the one a DateTimeFormat without a timeZone option formats in, and the one its resolvedOptions reports. name is an IANA name, such as "Europe/Prague", "UTC", or an offset, such as "+05:30". Without it the default is UTC, whatever the host's zone is.
The local time of the engine's Date is UTC whatever the zone: getHours, toString and the other methods of Date that are not toLocale* do not follow it.