Documentation ¶
Index ¶
- Constants
- Variables
- func ParseExtension(s string) (ext string, err error)
- type AliasType
- type Builder
- type CompactCoreInfo
- type FromTo
- type Language
- type Region
- func (r Region) Canonicalize() Region
- func (r Region) Contains(c Region) bool
- func (r Region) ISO3() string
- func (r Region) IsCountry() bool
- func (r Region) IsGroup() bool
- func (r Region) IsPrivateUse() bool
- func (r Region) M49() int
- func (r Region) String() string
- func (r Region) TLD() (Region, error)
- type Script
- type Tag
- func (t Tag) Extension(x byte) (ext string, ok bool)
- func (t Tag) Extensions() []string
- func (t Tag) HasExtensions() bool
- func (t Tag) HasString() bool
- func (t Tag) HasVariants() bool
- func (t Tag) IsPrivateUse() bool
- func (t Tag) IsRoot() bool
- func (t Tag) MarshalText() (text []byte, err error)
- func (t Tag) Maximize() (Tag, error)
- func (t Tag) Parent() Tag
- func (t Tag) Raw() (b Language, s Script, r Region)
- func (t *Tag) RemakeString()
- func (t Tag) SetTypeForKey(key, value string) (Tag, error)
- func (t Tag) String() string
- func (t Tag) TypeForKey(key string) string
- func (t *Tag) UnmarshalText(text []byte) error
- func (t Tag) VariantOrPrivateUseTags() string
- func (t Tag) Variants() string
- type ValueError
- type Variant
Constants ¶
const CLDRVersion = "32"
CLDRVersion is the CLDR version from which the tables in this package are derived.
const NumLanguages = 8798
const NumRegions = 358
const NumScripts = 261
Variables ¶
var AliasMap = [193]FromTo{}/* 193 elements not displayed */
AliasMap maps langIDs to their suggested replacements. Size: 772 bytes, 193 elements
var AliasTypes = [193]AliasType{}/* 193 elements not displayed */
Size: 193 bytes, 193 elements
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.
var ErrMissingLikelyTagsData = errors.New("missing likely tags data")
ErrMissingLikelyTagsData indicates no information was available to compute likely values of missing tags.
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 ¶
ParseExtension parses s as an extension and returns it on success.
Types ¶
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 ¶
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 ¶
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.
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.
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 ¶
MustParseBase is like ParseBase, but panics if the given base cannot be parsed. It simplifies safe initialization of Base values.
func ParseBase ¶
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 (Language) IsPrivateUse ¶
IsPrivateUse reports whether this language code is reserved for private use.
func (Language) 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 ¶
StringToBuf writes the string to b and returns the number of bytes written. cap(b) must be >= 3.
func (Language) SuppressScript ¶
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 ¶
EncodeM49 returns the Region for the given UN M.49 code. It returns an error if r is not a valid code.
func MustParseRegion ¶
MustParseRegion is like ParseRegion, but panics if the given region cannot be parsed. It simplifies safe initialization of Region values.
func ParseRegion ¶
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 ¶
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 ¶
Contains returns whether Region c is contained by Region r. It returns true if c == r.
func (Region) ISO3 ¶
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 ¶
IsCountry returns whether this region is a country or autonomous area. This includes non-standard definitions from CLDR.
func (Region) IsGroup ¶
IsGroup returns whether this region defines a collection of regions. This includes non-standard definitions from CLDR.
func (Region) IsPrivateUse ¶
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 ¶
M49 returns the UN M.49 encoding of r, or 0 if this encoding is not defined for r.
func (Region) String ¶
String returns the BCP 47 representation for the region. It returns "ZZ" for an unspecified region.
func (Region) TLD ¶
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 uint16
func MustParseScript ¶
MustParseScript is like ParseScript, but panics if the given script cannot be parsed. It simplifies safe initialization of Script values.
func ParseScript ¶
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 ¶
IsPrivateUse reports whether this script code is reserved for private use.
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 ¶
Make is a convenience wrapper for Parse that omits the error. In case of an error, a sensible default is returned.
func MustParse ¶
MustParse is like Parse, but panics if the given BCP 47 tag cannot be parsed. It simplifies safe initialization of Tag values.
func Parse ¶
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 ¶
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) HasExtensions ¶
HasExtensions reports whether t has extensions.
func (Tag) HasString ¶
HasString reports whether this tag defines more than just the raw components.
func (Tag) HasVariants ¶
HasVariants reports whether t has variants.
func (Tag) IsPrivateUse ¶
IsPrivateUse reports whether the Tag consists solely of an IsPrivateUse use tag.
func (Tag) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (Tag) Parent ¶
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 ¶
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 ¶
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) TypeForKey ¶
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.
If there are multiple types associated with a key, only the first will be returned. If there is no type associated with a key, it returns the empty string.
func (*Tag) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
func (Tag) VariantOrPrivateUseTags ¶
VariantOrPrivateUseTags returns variants or private use tags.
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) 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 ¶
ParseVariant parses and returns a Variant. An error is returned if s is not a valid variant.