pluralrules

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: 13 Imported by: 0

Documentation

Overview

Package pluralrules implements the ECMA-402 Intl.PluralRules constructor.

locales, _ := locale.ParseList("en-US")
rules, _ := pluralrules.New(locales, pluralrules.Options{})
category, _ := rules.Select(pluralrules.Int(1))
_ = category

See README.md for usage examples and SPECS/40-pluralrules.md for the contract.

Example

Example demonstrates Intl.PluralRules.prototype.select from ECMA-402.

package main

import (
	"fmt"

	"github.com/agentable/go-intl/locale"
	"github.com/agentable/go-intl/pluralrules"
)

func main() {
	rules, err := pluralrules.New(mustLocaleList("en"), pluralrules.Options{})
	if err != nil {
		panic(err)
	}

	one, err := rules.Select(pluralrules.Int(1))
	if err != nil {
		panic(err)
	}
	other, err := rules.Select(pluralrules.Int(2))
	if err != nil {
		panic(err)
	}
	fmt.Println(one)
	fmt.Println(other)

}

func mustLocaleList(tags ...string) locale.List {
	locales, err := locale.ParseList(tags...)
	if err != nil {
		panic(err)
	}
	return locales
}
Output:
one
other
Example (Options)

Example_options demonstrates Intl.PluralRules constructor options from ECMA-402.

package main

import (
	"fmt"

	"github.com/agentable/go-intl/locale"
	"github.com/agentable/go-intl/pluralrules"
)

func main() {
	rules, err := pluralrules.New(mustLocaleList("en"), pluralrules.Options{
		Type: pluralrules.Ordinal,
	})
	if err != nil {
		panic(err)
	}

	for _, n := range []int64{1, 2, 3, 4} {
		category, err := rules.Select(pluralrules.Int(n))
		if err != nil {
			panic(err)
		}
		fmt.Printf("%d: %s\n", n, category)
	}

}

func mustLocaleList(tags ...string) locale.List {
	locales, err := locale.ParseList(tags...)
	if err != nil {
		panic(err)
	}
	return locales
}
Output:
1: one
2: two
3: few
4: other

Index

Examples

Constants

View Source
const (
	LookupLocaleMatcher  LocaleMatcher = "lookup"
	BestFitLocaleMatcher LocaleMatcher = "best fit"

	StandardNotation    Notation = "standard"
	ScientificNotation  Notation = "scientific"
	EngineeringNotation Notation = "engineering"
	CompactNotation     Notation = "compact"

	ShortCompactDisplay CompactDisplay = "short"
	LongCompactDisplay  CompactDisplay = "long"

	CeilRoundingMode       RoundingMode = "ceil"
	FloorRoundingMode      RoundingMode = "floor"
	ExpandRoundingMode     RoundingMode = "expand"
	TruncRoundingMode      RoundingMode = "trunc"
	HalfCeilRoundingMode   RoundingMode = "halfCeil"
	HalfFloorRoundingMode  RoundingMode = "halfFloor"
	HalfExpandRoundingMode RoundingMode = "halfExpand"
	HalfTruncRoundingMode  RoundingMode = "halfTrunc"
	HalfEvenRoundingMode   RoundingMode = "halfEven"

	AutoRoundingPriority          RoundingPriority = "auto"
	MorePrecisionRoundingPriority RoundingPriority = "morePrecision"
	LessPrecisionRoundingPriority RoundingPriority = "lessPrecision"

	AutoTrailingZeroDisplay           TrailingZeroDisplay = "auto"
	StripIfIntegerTrailingZeroDisplay TrailingZeroDisplay = "stripIfInteger"
)

Variables

This section is empty.

Functions

func SupportedLocalesOf

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

Types

type Category

type Category uint8

Category is one ECMA-402 plural category returned by Select and SelectRange.

const (
	Zero Category = iota
	One
	Two
	Few
	Many
	Other
)

func (Category) MarshalText added in v0.2.8

func (c Category) MarshalText() ([]byte, error)

func (Category) String added in v0.2.8

func (c Category) String() string

type CompactDisplay

type CompactDisplay string

type LocaleMatcher

type LocaleMatcher string

type Notation

type Notation string

type Options

type Options struct {
	LocaleMatcher            LocaleMatcher
	Type                     Type
	MinimumIntegerDigits     *int
	MinimumFractionDigits    *int
	MaximumFractionDigits    *int
	MinimumSignificantDigits *int
	MaximumSignificantDigits *int
	RoundingIncrement        *int
	RoundingMode             RoundingMode
	RoundingPriority         RoundingPriority
	TrailingZeroDisplay      TrailingZeroDisplay
	Notation                 Notation
	CompactDisplay           CompactDisplay
}

Options configures PluralRules construction.

The zero value uses cardinal rules with ECMA-402 default digit handling.

type PluralRules

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

func New

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

func (*PluralRules) ResolvedOptions

func (f *PluralRules) ResolvedOptions() ResolvedOptions

func (*PluralRules) Select

func (f *PluralRules) Select(v Value) (Category, error)

Select returns the plural category for a numeric value.

func (*PluralRules) SelectRange

func (f *PluralRules) SelectRange(start, end Value) (Category, error)

SelectRange returns the plural category for a numeric range.

Example

ExamplePluralRules_SelectRange demonstrates Intl.PluralRules.prototype.selectRange from ECMA-402.

package main

import (
	"fmt"

	"github.com/agentable/go-intl/locale"
	"github.com/agentable/go-intl/pluralrules"
)

func main() {
	rules, err := pluralrules.New(mustLocaleList("en"), pluralrules.Options{})
	if err != nil {
		panic(err)
	}

	category, err := rules.SelectRange(pluralrules.Int(1), pluralrules.Int(2))
	if err != nil {
		panic(err)
	}
	fmt.Println(category)

}

func mustLocaleList(tags ...string) locale.List {
	locales, err := locale.ParseList(tags...)
	if err != nil {
		panic(err)
	}
	return locales
}
Output:
other

type ResolvedOptions

type ResolvedOptions struct {
	Locale                   locale.Locale       `json:"locale"`
	Type                     Type                `json:"type"`
	MinimumIntegerDigits     int                 `json:"minimumIntegerDigits"`
	MinimumFractionDigits    *int                `json:"minimumFractionDigits,omitempty"`
	MaximumFractionDigits    *int                `json:"maximumFractionDigits,omitempty"`
	MinimumSignificantDigits *int                `json:"minimumSignificantDigits,omitempty"`
	MaximumSignificantDigits *int                `json:"maximumSignificantDigits,omitempty"`
	PluralCategories         []Category          `json:"pluralCategories"`
	Notation                 Notation            `json:"notation"`
	CompactDisplay           CompactDisplay      `json:"compactDisplay"`
	RoundingIncrement        int                 `json:"roundingIncrement"`
	RoundingMode             RoundingMode        `json:"roundingMode"`
	RoundingPriority         RoundingPriority    `json:"roundingPriority"`
	TrailingZeroDisplay      TrailingZeroDisplay `json:"trailingZeroDisplay"`
}

type RoundingMode

type RoundingMode string

type RoundingPriority

type RoundingPriority string

type TrailingZeroDisplay

type TrailingZeroDisplay string

type Type

type Type uint8

Type selects cardinal or ordinal plural rules.

const (
	// Cardinal selects plural categories for quantities such as "1 item".
	Cardinal Type = iota
	// Ordinal selects plural categories for ordinals such as "1st".
	Ordinal
)

func (Type) MarshalText

func (t Type) MarshalText() ([]byte, error)

func (Type) String

func (t Type) String() string

type Value

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

Value is an opaque ECMA-402 numeric input for PluralRules methods.

func BigInt

func BigInt(v *big.Int) Value

BigInt returns an arbitrary-precision integer numeric value. A nil value is treated as zero.

func Decimal

func Decimal(s string) (Value, error)

Decimal parses a finite ECMA-402 decimal-string bridge value.

func Float

func Float(v float64) Value

Float returns a float64 numeric value.

func Int

func Int(v int64) Value

Int returns a signed integer numeric value.

func Uint

func Uint(v uint64) Value

Uint returns an unsigned integer numeric value.

Jump to

Keyboard shortcuts

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