locale

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

Documentation

Overview

Package locale implements the ECMA-402 Intl.Locale constructor.

loc, _ := locale.New("en-US", locale.Options{Calendar: "gregory"})
tag := loc.String()
_ = tag

See README.md for usage examples and SPECS/10-locale.md for the contract.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type List

type List []Locale

List is an ordered Intl locale request list.

A nil or empty List represents omitted locales at constructor boundaries.

func ParseList

func ParseList(tags ...string) (List, error)

ParseList parses locale identifiers into an ordered request list.

func (List) Strings

func (l List) Strings() []string

Strings returns the canonical locale identifiers in request order.

type Locale

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

func FromTag

func FromTag(tag language.Tag, opts Options) (Locale, error)

func New

func New(tag string, opts Options) (Locale, error)
Example (Options)

ExampleNew_options demonstrates Intl.Locale constructor options from ECMA-402.

package main

import (
	"fmt"

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

func main() {
	loc, err := locale.New("en", locale.Options{
		Region:   "GB",
		Calendar: "gregory",
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(loc.String())

}
Output:
en-GB-u-ca-gregory

func Parse

func Parse(s string) (Locale, error)
Example

ExampleParse demonstrates Intl.Locale canonicalization from ECMA-402.

package main

import (
	"fmt"

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

func main() {
	loc := mustLocale("en-us-u-ca-gregorian-hc-h23")
	fmt.Println(loc.String())

}

func mustLocale(tag string) locale.Locale {
	loc, err := locale.Parse(tag)
	if err != nil {
		panic(err)
	}
	return loc
}
Output:
en-US-u-ca-gregory-hc-h23

func (Locale) BaseName

func (l Locale) BaseName() string

func (Locale) Calendar

func (l Locale) Calendar() string

func (Locale) CaseFirst

func (l Locale) CaseFirst() string

func (Locale) Collation

func (l Locale) Collation() string

func (Locale) Equal

func (l Locale) Equal(other Locale) bool

func (Locale) FirstDayOfWeek

func (l Locale) FirstDayOfWeek() string

func (Locale) GetCalendars

func (l Locale) GetCalendars() []string

func (Locale) GetCollations

func (l Locale) GetCollations() []string

func (Locale) GetHourCycles

func (l Locale) GetHourCycles() []string

func (Locale) GetNumberingSystems

func (l Locale) GetNumberingSystems() []string

func (Locale) GetTextInfo

func (l Locale) GetTextInfo() TextInfo

func (Locale) GetTimeZones

func (l Locale) GetTimeZones() []string

func (Locale) GetWeekInfo

func (l Locale) GetWeekInfo() WeekInfo
Example

ExampleLocale_GetWeekInfo demonstrates Intl.Locale.prototype.getWeekInfo from ECMA-402.

package main

import (
	"fmt"

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

func main() {
	loc := mustLocale("en-GB")
	info := loc.GetWeekInfo()
	fmt.Println(info.FirstDay)
	fmt.Println(info.Weekend)

}

func mustLocale(tag string) locale.Locale {
	loc, err := locale.Parse(tag)
	if err != nil {
		panic(err)
	}
	return loc
}
Output:
Monday
[Saturday Sunday]

func (Locale) HourCycle

func (l Locale) HourCycle() string

func (Locale) Language

func (l Locale) Language() string

func (Locale) MarshalText

func (l Locale) MarshalText() ([]byte, error)

func (Locale) Maximize

func (l Locale) Maximize() Locale

func (Locale) Minimize

func (l Locale) Minimize() Locale

func (Locale) NumberingSystem

func (l Locale) NumberingSystem() string

func (Locale) Numeric

func (l Locale) Numeric() bool

func (Locale) Region

func (l Locale) Region() string

func (Locale) Script

func (l Locale) Script() string

func (Locale) String

func (l Locale) String() string

func (Locale) Tag

func (l Locale) Tag() language.Tag

func (*Locale) UnmarshalText

func (l *Locale) UnmarshalText(text []byte) error

func (Locale) Variants

func (l Locale) Variants() []string

type Options

type Options struct {
	Language        string
	Script          string
	Region          string
	Variants        []string
	Calendar        string
	Collation       string
	HourCycle       string
	CaseFirst       string
	Numeric         *bool
	NumberingSystem string
	FirstDayOfWeek  string
}

type TextInfo

type TextInfo struct {
	// Direction is the locale text direction. Mirrors getTextInfo() field "direction".
	Direction string `json:"direction"`
}

TextInfo describes locale text direction. Mirrors Intl.Locale.prototype.getTextInfo().

type WeekInfo

type WeekInfo struct {
	// FirstDay is the locale's first day of week. Mirrors getWeekInfo() field "firstDay".
	FirstDay time.Weekday `json:"firstDay"`
	// Weekend is the locale's weekend day range. Mirrors getWeekInfo() field "weekend".
	Weekend []time.Weekday `json:"weekend"`
}

WeekInfo describes locale week data. Mirrors Intl.Locale.prototype.getWeekInfo().

func (WeekInfo) MarshalJSON

func (w WeekInfo) MarshalJSON() ([]byte, error)

MarshalJSON emits ECMA-402 weekday numbers, Monday=1 through Sunday=7.

Jump to

Keyboard shortcuts

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