Documentation
¶
Overview ¶
Package relativetimeformat implements the ECMA-402 Intl.RelativeTimeFormat constructor.
locales, _ := locale.ParseList("en-US")
format, _ := relativetimeformat.New(locales, relativetimeformat.Options{})
out, _ := format.Format(relativetimeformat.Int(-1), relativetimeformat.Day)
_ = out
See README.md for usage examples and SPECS/42-relativetimeformat.md for the contract.
Example ¶
Example demonstrates Intl.RelativeTimeFormat.prototype.format from ECMA-402.
package main
import (
"fmt"
"github.com/agentable/go-intl/locale"
"github.com/agentable/go-intl/relativetimeformat"
)
func main() {
format, err := relativetimeformat.New(mustLocaleList("en-US"), relativetimeformat.Options{})
if err != nil {
panic(err)
}
out, err := format.Format(relativetimeformat.Int(3), relativetimeformat.Day)
if err != nil {
panic(err)
}
fmt.Println(out)
}
func mustLocaleList(tags ...string) locale.List {
locales, err := locale.ParseList(tags...)
if err != nil {
panic(err)
}
return locales
}
Output: in 3 days
Example (Options) ¶
Example_options demonstrates Intl.RelativeTimeFormat constructor options from ECMA-402.
package main
import (
"fmt"
"github.com/agentable/go-intl/locale"
"github.com/agentable/go-intl/relativetimeformat"
)
func main() {
format, err := relativetimeformat.New(mustLocaleList("en-US"), relativetimeformat.Options{
Numeric: relativetimeformat.NumericAuto,
})
if err != nil {
panic(err)
}
out, err := format.Format(relativetimeformat.Int(-1), relativetimeformat.Day)
if err != nil {
panic(err)
}
fmt.Println(out)
}
func mustLocaleList(tags ...string) locale.List {
locales, err := locale.ParseList(tags...)
if err != nil {
panic(err)
}
return locales
}
Output: yesterday
Index ¶
Examples ¶
Constants ¶
View Source
const ( LookupLocaleMatcher LocaleMatcher = "lookup" BestFitLocaleMatcher LocaleMatcher = "best fit" LongStyle Style = "long" ShortStyle Style = "short" NarrowStyle Style = "narrow" NumericAlways Numeric = "always" NumericAuto Numeric = "auto" Second Unit = "second" Minute Unit = "minute" Hour Unit = "hour" Day Unit = "day" Week Unit = "week" Month Unit = "month" Quarter Unit = "quarter" Year Unit = "year" // PartType values emitted by Format/FormatToParts. // ECMA-402 §17.5.6 PartitionRelativeTimePattern produces literal parts // for the pattern, and embeds NumberFormat partition records for the value. // RelativeTimeFormat's embedded NumberFormat uses Style="decimal" with no // Notation, so only the number-style-neutral part types can appear. PartLiteral PartType = "literal" PartInteger PartType = "integer" PartGroup PartType = "group" PartDecimal PartType = "decimal" PartFraction PartType = "fraction" PartPlusSign PartType = "plusSign" PartMinusSign PartType = "minusSign" PartInfinity PartType = "infinity" PartNaN PartType = "nan" )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type LocaleMatcher ¶
type LocaleMatcher string
type Options ¶
type Options struct {
LocaleMatcher LocaleMatcher
NumberingSystem string
Style Style
Numeric Numeric
}
type RelativeTimeFormat ¶
type RelativeTimeFormat struct {
// contains filtered or unexported fields
}
func (*RelativeTimeFormat) Format ¶ added in v0.2.8
func (f *RelativeTimeFormat) Format(value Value, unit Unit) (string, error)
func (*RelativeTimeFormat) FormatToParts ¶ added in v0.2.8
func (f *RelativeTimeFormat) FormatToParts(value Value, unit Unit) ([]Part, error)
Example ¶
ExampleRelativeTimeFormat_FormatToParts demonstrates Intl.RelativeTimeFormat.prototype.formatToParts from ECMA-402.
package main
import (
"fmt"
"github.com/agentable/go-intl/locale"
"github.com/agentable/go-intl/relativetimeformat"
)
func main() {
format, err := relativetimeformat.New(mustLocaleList("en-US"), relativetimeformat.Options{})
if err != nil {
panic(err)
}
parts, err := format.FormatToParts(relativetimeformat.Int(-2), relativetimeformat.Day)
if err != nil {
panic(err)
}
for _, part := range parts {
fmt.Printf("%s=%q unit=%q\n", part.Type, part.Value, part.Unit)
}
}
func mustLocaleList(tags ...string) locale.List {
locales, err := locale.ParseList(tags...)
if err != nil {
panic(err)
}
return locales
}
Output: integer="2" unit="day" literal=" days ago" unit=""
func (*RelativeTimeFormat) ResolvedOptions ¶
func (f *RelativeTimeFormat) ResolvedOptions() ResolvedOptions
type ResolvedOptions ¶
type Value ¶ added in v0.2.8
type Value struct {
// contains filtered or unexported fields
}
Value is an opaque ECMA-402 numeric input for RelativeTimeFormat methods.
Click to show internal directories.
Click to hide internal directories.