language

package
v5.1.15+incompatible Latest Latest
Warning

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

Go to latest
Published: May 26, 2020 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const CLDRVersion = "32"

CLDRVersion is the CLDR version from which the tables in this package are derived.

View Source
const NumLanguages = 8665
View Source
const NumRegions = 357
View Source
const NumScripts = 242

Variables

View Source
var AliasMap = [164]FromTo{}/* 164 elements not displayed */

AliasMap maps langIDs to their suggested replacements. Size: 656 bytes, 164 elements

View Source
var AliasTypes = [164]AliasType{}/* 164 elements not displayed */

Size: 164 bytes, 164 elements

View Source
var ErrDuplicateKey = errors.New("language: different values for same key in -u extension")

ErrDuplicateKey is returned when a tag contains the same key twice with different values in the -u section.

View Source
var ErrMissingLikelyTagsData = errors.New("missing likely tags data")

ErrMissingLikelyTagsData indicates no information was available to compute likely values of missing tags.

View Source
var ErrSyntax = errors.New("language: tag is not well-formed")

ErrSyntax is returned by any of the parsing functions when the input is not well-formed, according to BCP 47. TODO: return the position at which the syntax error occurred?

Functions

func ParseExtension

func ParseExtension(s string) (ext string, err error)

ParseExtension parses s as an extension and returns it on success.

Types

type AliasType

type AliasType int8

AliasType is the type of an alias in AliasMap.

const (
	Deprecated AliasType = iota
	Macro
	Legacy

	AliasTypeUnknown AliasType = -1
)

type Builder

type Builder struct {
	Tag Tag
	// contains filtered or unexported fields
}

A Builder allows constructing a Tag from individual components. Its main user is Compose in the top-level language package.

func (*Builder) AddExt

func (b *Builder) AddExt(e string)

AddExt adds extension e to the tag. e must be a valid extension as returned by Tag.Extension. If the extension already exists, it will be discarded, except for a -u extension, where non-existing key-type pairs will added.

func (*Builder) AddVariant

func (b *Builder) AddVariant(v ...string)

AddVariant adds any number of variants.

func (*Builder) ClearExtensions

func (b *Builder) ClearExtensions()

ClearExtensions removes any extensions previously added, including those copied from a Tag in SetTag.

func (*Builder) ClearVariants

func (b *Builder) ClearVariants()

ClearVariants removes any variants previously added, including those copied from a Tag in SetTag.

func (*Builder) Make

func (b *Builder) Make() Tag

Make returns a new Tag from the current settings.

func (*Builder) SetExt

func (b *Builder) SetExt(e string)

SetExt sets the extension e to the tag. e must be a valid extension as returned by Tag.Extension. If the extension already exists, it will be overwritten, except for a -u extension, where the individual key-type pairs will be set.

func (*Builder) SetTag

func (b *Builder) SetTag(t Tag)

SetTag copies all the settings from a given Tag. Any previously set values are discarded.

type CompactCoreInfo

type CompactCoreInfo uint32

CompactCoreInfo is a compact integer with the three core tags encoded.

func GetCompactCore

func GetCompactCore(t Tag) (cci CompactCoreInfo, ok bool)

GetCompactCore generates a uint32 value that is guaranteed to be unique for different language, region, and script values.

func (CompactCoreInfo) Tag

func (c CompactCoreInfo) Tag() Tag

Tag generates a tag from c.

type FromTo

type FromTo struct {
	From uint16
	To   uint16
}

type Language

type Language uint16

func BaseLanguages

func BaseLanguages() []Language

BaseLanguages returns the list of all supported base languages. It generates the list by traversing the internal structures.

func MustParseBase

func MustParseBase(s string) Language

MustParseBase is like ParseBase, but panics if the given base cannot be parsed. It simplifies safe initialization of Base values.

func ParseBase

func ParseBase(s string) (Language, error)

ParseBase parses a 2- or 3-letter ISO 639 code. It returns a ValueError if s is a well-formed but unknown language identifier or another error if another error occurred.

func (Language) Canonicalize

func (id Language) Canonicalize() (Language, AliasType)

func (Language) ISO3

func (b Language) ISO3() string

ISO3 returns the ISO 639-3 language code.

func (Language) IsPrivateUse

func (b Language) IsPrivateUse() bool

IsPrivateUse reports whether this language code is reserved for private use.

func (Language) String

func (b Language) String() string

String returns the BCP 47 representation of the langID. Use b as variable name, instead of id, to ensure the variable used is consistent with that of Base in which this type is embedded.

func (Language) StringToBuf

func (id Language) StringToBuf(b []byte) int

StringToBuf writes the string to b and returns the number of bytes written. cap(b) must be >= 3.

func (Language) SuppressScript

func (b Language) SuppressScript() Script

SuppressScript returns the script marked as SuppressScript in the IANA language tag repository, or 0 if there is no such script.

type Region

type Region uint16

func EncodeM49

func EncodeM49(r int) (Region, error)

EncodeM49 returns the Region for the given UN M.49 code. It returns an error if r is not a valid code.

func MustParseRegion

func MustParseRegion(s string) Region

MustParseRegion is like ParseRegion, but panics if the given region cannot be parsed. It simplifies safe initialization of Region values.

func ParseRegion

func ParseRegion(s string) (Region, error)

ParseRegion parses a 2- or 3-letter ISO 3166-1 or a UN M.49 code. It returns a ValueError if s is a well-formed but unknown region identifier or another error if another error occurred.

func (Region) Canonicalize

func (r Region) Canonicalize() Region

Canonicalize returns the region or a possible replacement if the region is deprecated. It will not return a replacement for deprecated regions that are split into multiple regions.

func (Region) Contains

func (r Region) Contains(c Region) bool

Contains returns whether Region c is contained by Region r. It returns true if c == r.

func (Region) ISO3

func (r Region) ISO3() string

ISO3 returns the 3-letter ISO code of r. Note that not all regions have a 3-letter ISO code. In such cases this method returns "ZZZ".

func (Region) IsCountry

func (r Region) IsCountry() bool

IsCountry returns whether this region is a country or autonomous area. This includes non-standard definitions from CLDR.

func (Region) IsGroup

func (r Region) IsGroup() bool

IsGroup returns whether this region defines a collection of regions. This includes non-standard definitions from CLDR.

func (Region) IsPrivateUse

func (r Region) IsPrivateUse() bool

IsPrivateUse reports whether r has the ISO 3166 User-assigned status. This may include private-use tags that are assigned by CLDR and used in this implementation. So IsPrivateUse and IsCountry can be simultaneously true.

func (Region) M49

func (r Region) M49() int

M49 returns the UN M.49 encoding of r, or 0 if this encoding is not defined for r.

func (Region) String

func (r Region) String() string

String returns the BCP 47 representation for the region. It returns "ZZ" for an unspecified region.

func (Region) TLD

func (r Region) TLD() (Region, error)

TLD returns the country code top-level domain (ccTLD). UK is returned for GB. In all other cases it returns either the region itself or an error.

This method may return an error for a region for which there exists a canonical form with a ccTLD. To get that ccTLD canonicalize r first. The region will already be canonicalized it was obtained from a Tag that was obtained using any of the default methods.

type Script

type Script uint8

func MustParseScript

func MustParseScript(s string) Script

MustParseScript is like ParseScript, but panics if the given script cannot be parsed. It simplifies safe initialization of Script values.

func ParseScript

func ParseScript(s string) (Script, error)

ParseScript parses a 4-letter ISO 15924 code. It returns a ValueError if s is a well-formed but unknown script identifier or another error if another error occurred.

func (Script) IsPrivateUse

func (s Script) IsPrivateUse() bool

IsPrivateUse reports whether this script code is reserved for private use.

func (Script) String

func (s Script) String() string

String returns the script code in title case. It returns "Zzzz" for an unspecified script.

type Tag

type Tag struct {
	LangID   Language
	RegionID Region
	// TODO: we will soon run out of positions for ScriptID. Idea: instead of
	// storing lang, region, and ScriptID codes, store only the compact index and
	// have a lookup table from this code to its expansion. This greatly speeds
	// up table lookup, speed up common variant cases.
	// This will also immediately free up 3 extra bytes. Also, the pVariant
	// field can now be moved to the lookup table, as the compact index uniquely
	// determines the offset of a possible variant.
	ScriptID Script
	// contains filtered or unexported fields
}

Tag represents a BCP 47 language tag. It is used to specify an instance of a specific language or locale. All language tag values are guaranteed to be well-formed. The zero value of Tag is Und.

var Und Tag

Und is the root language.

func Make

func Make(s string) Tag

Make is a convenience wrapper for Parse that omits the error. In case of an error, a sensible default is returned.

func MustParse

func MustParse(s string) Tag

MustParse is like Parse, but panics if the given BCP 47 tag cannot be parsed. It simplifies safe initialization of Tag values.

func Parse

func Parse(s string) (t Tag, err error)

Parse parses the given BCP 47 string and returns a valid Tag. If parsing failed it returns an error and any part of the tag that could be parsed. If parsing succeeded but an unknown value was found, it returns ValueError. The Tag returned in this case is just stripped of the unknown value. All other values are preserved. It accepts tags in the BCP 47 format and extensions to this standard defined in https://www.unicode.org/reports/tr35/#Unicode_Language_and_Locale_Identifiers.

func (Tag) Extension

func (t Tag) Extension(x byte) (ext string, ok bool)

Extension returns the extension of type x for tag t. It will return false for ok if t does not have the requested extension. The returned extension will be invalid in this case.

func (Tag) Extensions

func (t Tag) Extensions() []string

Extensions returns all extensions of t.

func (Tag) HasExtensions

func (t Tag) HasExtensions() bool

HasExtensions reports whether t has extensions.

func (Tag) HasString

func (t Tag) HasString() bool

HasString reports whether this tag defines more than just the raw components.

func (Tag) HasVariants

func (t Tag) HasVariants() bool

HasVariants reports whether t has variants.

func (Tag) IsPrivateUse

func (t Tag) IsPrivateUse() bool

IsPrivateUse reports whether the Tag consists solely of an IsPrivateUse use tag.

func (Tag) IsRoot

func (t Tag) IsRoot() bool

IsRoot returns true if t is equal to language "und".

func (Tag) MarshalText

func (t Tag) MarshalText() (text []byte, err error)

MarshalText implements encoding.TextMarshaler.

func (Tag) Maximize

func (t Tag) Maximize() (Tag, error)

Maximize returns a new tag with missing tags filled in.

func (Tag) Parent

func (t Tag) Parent() Tag

Parent returns the CLDR parent of t. In CLDR, missing fields in data for a specific language are substituted with fields from the parent language. The parent for a language may change for newer versions of CLDR.

func (Tag) Raw

func (t Tag) Raw() (b Language, s Script, r Region)

Raw returns the raw base language, script and region, without making an attempt to infer their values. TODO: consider removing

func (*Tag) RemakeString

func (t *Tag) RemakeString()

RemakeString is used to update t.str in case lang, script or region changed. It is assumed that pExt and pVariant still point to the start of the respective parts.

func (Tag) SetTypeForKey

func (t Tag) SetTypeForKey(key, value string) (Tag, error)

SetTypeForKey returns a new Tag with the key set to type, where key and type are of the allowed values defined for the Unicode locale extension ('u') in https://www.unicode.org/reports/tr35/#Unicode_Language_and_Locale_Identifiers. An empty value removes an existing pair with the same key.

func (Tag) String

func (t Tag) String() string

String returns the canonical string representation of the language tag.

func (Tag) TypeForKey

func (t Tag) TypeForKey(key string) string

TypeForKey returns the type associated with the given key, where key and type are of the allowed values defined for the Unicode locale extension ('u') in https://www.unicode.org/reports/tr35/#Unicode_Language_and_Locale_Identifiers. TypeForKey will traverse the inheritance chain to get the correct value.

func (*Tag) UnmarshalText

func (t *Tag) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

func (Tag) VariantOrPrivateUseTags

func (t Tag) VariantOrPrivateUseTags() string

VariantOrPrivateUseTags returns variants or private use tags.

func (Tag) Variants

func (t Tag) Variants() string

Variants returns the part of the tag holding all variants or the empty string if there are no variants defined.

type ValueError

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

ValueError is returned by any of the parsing functions when the input is well-formed but the respective subtag is not recognized as a valid value.

func NewValueError

func NewValueError(tag []byte) ValueError

NewValueError creates a new ValueError.

func (ValueError) Error

func (e ValueError) Error() string

Error implements the error interface.

func (ValueError) Subtag

func (e ValueError) Subtag() string

Subtag returns the subtag for which the error occurred.

type Variant

type Variant struct {
	ID uint8
	// contains filtered or unexported fields
}

Variant represents a registered variant of a language as defined by BCP 47.

func ParseVariant

func ParseVariant(s string) (Variant, error)

ParseVariant parses and returns a Variant. An error is returned if s is not a valid variant.

func (Variant) String

func (v Variant) String() string

String returns the string representation of the variant.

Directories

Path Synopsis
Package compact defines a compact representation of language tags.
Package compact defines a compact representation of language tags.

Jump to

Keyboard shortcuts

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