listformat

package
v0.2.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 19, 2026 License: MIT Imports: 8 Imported by: 0

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

func SupportedLocalesOf

func SupportedLocalesOf(locales locale.List, opts Options) (locale.List, error)

Types

type ListFormat

type ListFormat struct {
	// contains filtered or unexported fields
}

func New

func New(locales locale.List, opts Options) (*ListFormat, error)

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 Part

type Part struct {
	Type  PartType `json:"type"`
	Value string   `json:"value"`
}

type PartType

type PartType string

type ResolvedOptions

type ResolvedOptions struct {
	Locale locale.Locale `json:"locale"`
	Type   Type          `json:"type"`
	Style  Style         `json:"style"`
}

type Style

type Style string

type Type

type Type string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL