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 ¶
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 ¶
Types ¶
type Category ¶
type Category uint8
Category is one ECMA-402 plural category returned by Select and SelectRange.
func (Category) MarshalText ¶ added in v0.2.8
type CompactDisplay ¶
type CompactDisplay string
type LocaleMatcher ¶
type LocaleMatcher 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 (*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.
func (Type) MarshalText ¶
type Value ¶
type Value struct {
// contains filtered or unexported fields
}
Value is an opaque ECMA-402 numeric input for PluralRules methods.
func BigInt ¶
BigInt returns an arbitrary-precision integer numeric value. A nil value is treated as zero.