Documentation
¶
Overview ¶
Package listformat implements the ECMA-402 Intl.ListFormat constructor.
locales, _ := locale.ParseList("en-US")
format, _ := listformat.New(locales, listformat.Options{})
out := format.Format([]string{"apples", "bananas"})
_ = out
See README.md for usage examples and SPECS/41-listformat.md for the contract.
Example ¶
Example demonstrates Intl.ListFormat.prototype.format from ECMA-402.
package main
import (
"fmt"
"github.com/agentable/go-intl/listformat"
"github.com/agentable/go-intl/locale"
)
func main() {
format, err := listformat.New(mustLocaleList("en-US"), listformat.Options{})
if err != nil {
panic(err)
}
fmt.Println(format.Format([]string{"apples", "bananas", "cherries"}))
}
func mustLocaleList(tags ...string) locale.List {
locales, err := locale.ParseList(tags...)
if err != nil {
panic(err)
}
return locales
}
Output: apples, bananas, and cherries
Example (Options) ¶
Example_options demonstrates Intl.ListFormat constructor options from ECMA-402.
package main
import (
"fmt"
"github.com/agentable/go-intl/listformat"
"github.com/agentable/go-intl/locale"
)
func main() {
format, err := listformat.New(mustLocaleList("en-US"), listformat.Options{
Type: listformat.Disjunction,
})
if err != nil {
panic(err)
}
fmt.Println(format.Format([]string{"red", "green", "blue"}))
}
func mustLocaleList(tags ...string) locale.List {
locales, err := locale.ParseList(tags...)
if err != nil {
panic(err)
}
return locales
}
Output: red, green, or blue
Index ¶
Examples ¶
Constants ¶
View Source
const ( LookupLocaleMatcher LocaleMatcher = "lookup" BestFitLocaleMatcher LocaleMatcher = "best fit" Conjunction Type = "conjunction" Disjunction Type = "disjunction" Unit Type = "unit" LongStyle Style = "long" ShortStyle Style = "short" NarrowStyle Style = "narrow" PartElement PartType = "element" PartLiteral PartType = "literal" )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ListFormat ¶
type ListFormat struct {
// contains filtered or unexported fields
}
func (*ListFormat) Format ¶
func (f *ListFormat) Format(list []string) string
func (*ListFormat) FormatToParts ¶
func (f *ListFormat) FormatToParts(list []string) []Part
Example ¶
ExampleListFormat_FormatToParts demonstrates Intl.ListFormat.prototype.formatToParts from ECMA-402.
package main
import (
"fmt"
"github.com/agentable/go-intl/listformat"
"github.com/agentable/go-intl/locale"
)
func main() {
format, err := listformat.New(mustLocaleList("en-US"), listformat.Options{})
if err != nil {
panic(err)
}
for _, part := range format.FormatToParts([]string{"apples", "bananas"}) {
fmt.Printf("%s=%q\n", part.Type, part.Value)
}
}
func mustLocaleList(tags ...string) locale.List {
locales, err := locale.ParseList(tags...)
if err != nil {
panic(err)
}
return locales
}
Output: element="apples" literal=" and " element="bananas"
func (*ListFormat) ResolvedOptions ¶
func (f *ListFormat) ResolvedOptions() ResolvedOptions
type LocaleMatcher ¶
type LocaleMatcher string
type Options ¶
type Options struct {
LocaleMatcher LocaleMatcher
Type Type
Style Style
}
type ResolvedOptions ¶
Click to show internal directories.
Click to hide internal directories.