Documentation
¶
Overview ¶
Package intl adds the ECMAScript Internationalization API (ECMA-402) to the VMs of package github.com/hajimehoshi/ekuma. Import it for its side effect:
import _ "github.com/hajimehoshi/ekuma/intl"
The global object of every VM then has Intl, with data for every locale in the Unicode Common Locale Data Repository, and Temporal supports the ECMA-402 calendars in addition to ISO 8601. Without the import, a VM has no Intl, and Temporal has only the ISO 8601 calendar.
Programs that import this package must distribute the data notices listed in the README's License section.
github.com/hajimehoshi/ekuma.VM.SetLocale overrides the default locale. Otherwise, the first nonempty LC_ALL, LC_MESSAGES or LANG environment variable selects it. With none set, the default is the user locale on macOS, iOS and Windows, the system locale on Android, or the system locale configuration on Linux. Other systems and Android builds without Cgo use the locale environment variables only. Browser builds use navigator.language. If the host locale is unavailable, C or POSIX, the default is en-US.
Example (Collator) ¶
package main
import (
"context"
"fmt"
"github.com/hajimehoshi/ekuma"
_ "github.com/hajimehoshi/ekuma/intl"
)
func main() {
var vm ekuma.VM
s, err := vm.ParseScript("cities.js", `
const collator = new Intl.Collator("sv");
["Örebro", "Ängelholm", "Alingsås", "Åmål"].sort(collator.compare).join(", ")
`)
if err != nil {
panic(err)
}
v, err := s.Evaluate(context.Background())
if err != nil {
panic(err)
}
fmt.Println(v.String())
}
Output: Alingsås, Åmål, Ängelholm, Örebro
Example (DateTimeFormat) ¶
package main
import (
"context"
"fmt"
"github.com/hajimehoshi/ekuma"
_ "github.com/hajimehoshi/ekuma/intl"
)
func main() {
var vm ekuma.VM
s, err := vm.ParseScript("date.js", `
const format = new Intl.DateTimeFormat("en-GB", {
timeZone: "UTC",
year: "numeric",
month: "long",
day: "numeric",
});
format.format(new Date("2024-01-02T00:00:00Z"))
`)
if err != nil {
panic(err)
}
v, err := s.Evaluate(context.Background())
if err != nil {
panic(err)
}
fmt.Println(v.String())
}
Output: 2 January 2024
Example (DefaultLocale) ¶
package main
import (
"context"
"fmt"
"github.com/hajimehoshi/ekuma"
_ "github.com/hajimehoshi/ekuma/intl"
)
func main() {
var vm ekuma.VM
if err := vm.SetLocale("de-DE"); err != nil {
panic(err)
}
s, err := vm.ParseScript("locale.js", `(1234.5).toLocaleString()`)
if err != nil {
panic(err)
}
v, err := s.Evaluate(context.Background())
if err != nil {
panic(err)
}
fmt.Println(v.String())
}
Output: 1.234,5
Example (JapaneseCalendar) ¶
This example formats the last day of Heisei and the first day of Reiwa.
package main
import (
"context"
"fmt"
"github.com/hajimehoshi/ekuma"
_ "github.com/hajimehoshi/ekuma/intl"
)
func main() {
var vm ekuma.VM
s, err := vm.ParseScript("eras.js", `
const format = new Intl.DateTimeFormat("ja-JP", {
calendar: "japanese",
dateStyle: "full",
timeZone: "UTC",
});
["2019-04-30", "2019-05-01"]
.map((date) => format.format(new Date(date)))
.join("\n")
`)
if err != nil {
panic(err)
}
v, err := s.Evaluate(context.Background())
if err != nil {
panic(err)
}
fmt.Println(v.String())
}
Output: 平成31年4月30日火曜日 令和元年5月1日水曜日
Example (NumberFormat) ¶
package main
import (
"context"
"fmt"
"github.com/hajimehoshi/ekuma"
_ "github.com/hajimehoshi/ekuma/intl"
)
func main() {
var vm ekuma.VM
s, err := vm.ParseScript("price.js", `
const usd = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
});
usd.format(1234.5)
`)
if err != nil {
panic(err)
}
v, err := s.Evaluate(context.Background())
if err != nil {
panic(err)
}
fmt.Println(v.String())
}
Output: $1,234.50
Example (Segmenter) ¶
package main
import (
"context"
"fmt"
"github.com/hajimehoshi/ekuma"
_ "github.com/hajimehoshi/ekuma/intl"
)
func main() {
var vm ekuma.VM
s, err := vm.ParseScript("graphemes.js", `
const segmenter = new Intl.Segmenter("en", { granularity: "grapheme" });
Array.from(segmenter.segment("A🇯🇵B"), ({ segment }) => segment).join(" | ")
`)
if err != nil {
panic(err)
}
v, err := s.Evaluate(context.Background())
if err != nil {
panic(err)
}
fmt.Println(v.String())
}
Output: A | 🇯🇵 | B