language

Imports

Imports #

"fmt"
"sort"
"golang.org/x/text/internal/language"
"strings"
"golang.org/x/text/internal/language"
"golang.org/x/text/internal/language/compact"
"errors"
"strings"
"golang.org/x/text/internal/language"
"errors"
"sort"
"strconv"
"strings"
"golang.org/x/text/internal/language"
"golang.org/x/text/internal/language/compact"

Constants & Variables

Afrikaans var #

var Afrikaans Tag = *ast.CallExpr

Albanian var #

var Albanian Tag = *ast.CallExpr

All const #

All canonicalizations.

const All = *ast.BinaryExpr

AmericanEnglish var #

var AmericanEnglish Tag = *ast.CallExpr

Amharic var #

var Amharic Tag = *ast.CallExpr

Arabic var #

var Arabic Tag = *ast.CallExpr

Armenian var #

var Armenian Tag = *ast.CallExpr

Azerbaijani var #

var Azerbaijani Tag = *ast.CallExpr

BCP47 const #

All canonicalizations recommended by BCP 47.

const BCP47 = *ast.BinaryExpr

Bengali var #

var Bengali Tag = *ast.CallExpr

BrazilianPortuguese var #

var BrazilianPortuguese Tag = *ast.CallExpr

BritishEnglish var #

var BritishEnglish Tag = *ast.CallExpr

Bulgarian var #

var Bulgarian Tag = *ast.CallExpr

Burmese var #

var Burmese Tag = *ast.CallExpr

CLDR const #

The CLDR flag should be used if full compatibility with CLDR is required. There are a few cases where language.Tag may differ from CLDR. To follow all of CLDR's suggestions, use All|CLDR.

const CLDR

CLDRVersion const #

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

const CLDRVersion = "32"

CanadianFrench var #

var CanadianFrench Tag = *ast.CallExpr

Catalan var #

var Catalan Tag = *ast.CallExpr

Chinese var #

var Chinese Tag = *ast.CallExpr

Croatian var #

var Croatian Tag = *ast.CallExpr

Czech var #

var Czech Tag = *ast.CallExpr

Danish var #

var Danish Tag = *ast.CallExpr

Default const #

Default is the canonicalization used by Parse, Make and Compose. To preserve as much information as possible, canonicalizations that remove potentially valuable information are not included. The Matcher is designed to recognize similar tags that would be the same if they were canonicalized using All.

const Default = *ast.BinaryExpr

Deprecated const #

Replace all deprecated tags with their preferred replacements.

const Deprecated = *ast.BinaryExpr

DeprecatedBase const #

Replace deprecated base languages with their preferred replacements.

const DeprecatedBase CanonType = *ast.BinaryExpr

DeprecatedRegion const #

Replace deprecated regions with their preferred replacements.

const DeprecatedRegion

DeprecatedScript const #

Replace deprecated scripts with their preferred replacements.

const DeprecatedScript

Dutch var #

var Dutch Tag = *ast.CallExpr

English var #

var English Tag = *ast.CallExpr

ErrMissingLikelyTagsData var #

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

var ErrMissingLikelyTagsData = *ast.CallExpr

Estonian var #

var Estonian Tag = *ast.CallExpr

EuropeanPortuguese var #

var EuropeanPortuguese Tag = *ast.CallExpr

EuropeanSpanish var #

var EuropeanSpanish Tag = *ast.CallExpr

Exact const #

const Exact

Filipino var #

var Filipino Tag = *ast.CallExpr

Finnish var #

var Finnish Tag = *ast.CallExpr

French var #

var French Tag = *ast.CallExpr

Georgian var #

var Georgian Tag = *ast.CallExpr

German var #

var German Tag = *ast.CallExpr

Greek var #

var Greek Tag = *ast.CallExpr

Gujarati var #

var Gujarati Tag = *ast.CallExpr

Hebrew var #

var Hebrew Tag = *ast.CallExpr

High const #

const High

Hindi var #

var Hindi Tag = *ast.CallExpr

Hungarian var #

var Hungarian Tag = *ast.CallExpr

Icelandic var #

var Icelandic Tag = *ast.CallExpr

Indonesian var #

var Indonesian Tag = *ast.CallExpr

Italian var #

var Italian Tag = *ast.CallExpr

Japanese var #

var Japanese Tag = *ast.CallExpr

Kannada var #

var Kannada Tag = *ast.CallExpr

Kazakh var #

var Kazakh Tag = *ast.CallExpr

Khmer var #

var Khmer Tag = *ast.CallExpr

Kirghiz var #

var Kirghiz Tag = *ast.CallExpr

Korean var #

var Korean Tag = *ast.CallExpr

Lao var #

var Lao Tag = *ast.CallExpr

LatinAmericanSpanish var #

var LatinAmericanSpanish Tag = *ast.CallExpr

Latvian var #

var Latvian Tag = *ast.CallExpr

Legacy const #

Normalize legacy encodings. This includes legacy languages defined in CLDR as well as bibliographic codes defined in ISO-639.

const Legacy

Lithuanian var #

var Lithuanian Tag = *ast.CallExpr

Low const #

const Low

Macedonian var #

var Macedonian Tag = *ast.CallExpr

Macro const #

Map the dominant language of a macro language group to the macro language subtag. For example cmn -> zh.

const Macro

Malay var #

var Malay Tag = *ast.CallExpr

Malayalam var #

var Malayalam Tag = *ast.CallExpr

Marathi var #

var Marathi Tag = *ast.CallExpr

ModernStandardArabic var #

var ModernStandardArabic Tag = *ast.CallExpr

Mongolian var #

var Mongolian Tag = *ast.CallExpr

Nepali var #

var Nepali Tag = *ast.CallExpr

No const #

const No Confidence = iota

Norwegian var #

var Norwegian Tag = *ast.CallExpr

NumCompactTags const #

NumCompactTags is the number of compact tags. The maximum tag is NumCompactTags-1.

const NumCompactTags = compact.NumCompactTags

Persian var #

var Persian Tag = *ast.CallExpr

Polish var #

var Polish Tag = *ast.CallExpr

Portuguese var #

var Portuguese Tag = *ast.CallExpr

Punjabi var #

var Punjabi Tag = *ast.CallExpr

Raw const #

Raw can be used to Compose or Parse without Canonicalization.

const Raw CanonType = 0

Romanian var #

var Romanian Tag = *ast.CallExpr

Russian var #

var Russian Tag = *ast.CallExpr

Serbian var #

var Serbian Tag = *ast.CallExpr

SerbianLatin var #

var SerbianLatin Tag = *ast.CallExpr

SimplifiedChinese var #

var SimplifiedChinese Tag = *ast.CallExpr

Sinhala var #

var Sinhala Tag = *ast.CallExpr

Slovak var #

var Slovak Tag = *ast.CallExpr

Slovenian var #

var Slovenian Tag = *ast.CallExpr

Spanish var #

var Spanish Tag = *ast.CallExpr

Supported var #

Supported defines a Coverage that lists all supported subtags. Tags always returns nil.

var Supported Coverage = allSubtags{...}

SuppressScript const #

Remove redundant scripts.

const SuppressScript

Swahili var #

var Swahili Tag = *ast.CallExpr

Swedish var #

var Swedish Tag = *ast.CallExpr

Tamil var #

var Tamil Tag = *ast.CallExpr

Telugu var #

var Telugu Tag = *ast.CallExpr

Thai var #

var Thai Tag = *ast.CallExpr

TraditionalChinese var #

var TraditionalChinese Tag = *ast.CallExpr

Turkish var #

var Turkish Tag = *ast.CallExpr

Ukrainian var #

var Ukrainian Tag = *ast.CallExpr

Und var #

var Und Tag = Tag{...}

Urdu var #

var Urdu Tag = *ast.CallExpr

Uzbek var #

var Uzbek Tag = *ast.CallExpr

Vietnamese var #

var Vietnamese Tag = *ast.CallExpr

Zulu var #

var Zulu Tag = *ast.CallExpr

_001 const #

const _001 = 1

_419 const #

const _419 = 31

_BR const #

const _BR = 65

_CA const #

const _CA = 73

_ES const #

const _ES = 111

_GB const #

const _GB = 124

_Hani const #

const _Hani = 57

_Hans const #

const _Hans = 59

_Hant const #

const _Hant = 60

_Latn const #

const _Latn = 91

_MD const #

const _MD = 189

_PT const #

const _PT = 239

_Qaaa const #

const _Qaaa = 149

_Qaai const #

const _Qaai = 157

_Qabx const #

const _Qabx = 198

_UK const #

const _UK = 307

_US const #

const _US = 310

_XA const #

const _XA = 324

_XC const #

const _XC = 326

_XK const #

const _XK = 334

_ZZ const #

const _ZZ = 358

_Zinh const #

const _Zinh = 255

_Zyyy const #

const _Zyyy = 260

_Zzzz const #

const _Zzzz = 261

_de const #

const _de = 269

_en const #

const _en = 313

_fr const #

const _fr = 350

_it const #

const _it = 505

_mo const #

const _mo = 784

_mul const #

const _mul = 806

_nb const #

const _nb = 839

_no const #

const _no = 879

_pt const #

const _pt = 960

_sh const #

const _sh = 1031

_und const #

const _und = 0

acceptFallback var #

Add hack mapping to deal with a small number of cases that occur in Accept-Language (with reasonable frequency).

var acceptFallback = map[string]language.Language{...}

canonLang const #

const canonLang = *ast.BinaryExpr

confName var #

var confName = []string{...}

errInvalidArgument var #

var errInvalidArgument = *ast.CallExpr

errInvalidWeight var #

var errInvalidWeight = *ast.CallExpr

errTagListTooLarge var #

var errTagListTooLarge = *ast.CallExpr

matchLang var #

matchLang holds pairs of langIDs of base languages that are typically mutually intelligible. Each pair is associated with a confidence and whether the intelligibility goes one or both ways.

var matchLang = []mutualIntelligibility{...}

matchRegion var #

var matchRegion = []regionIntelligibility{...}

matchScript var #

matchScript holds pairs of scriptIDs where readers of one script can typically also read the other. Each is associated with a confidence.

var matchScript = []scriptIntelligibility{...}

notEquivalent var #

var notEquivalent []language.Language

paradigmLocales var #

var paradigmLocales = [][3]uint16{...}

regionToGroups var #

var regionToGroups = []uint8{...}

root var #

var root = language.Tag{...}

und var #

var und = Tag{...}

Type Aliases

CanonType type #

CanonType can be used to enable or disable various types of canonicalization.

type CanonType int

Confidence type #

Confidence indicates the level of certainty for a given return value. For example, Serbian may be written in Cyrillic or Latin script. The confidence level indicates whether a value was explicitly specified, whether it is typically the only possible value, or whether there is an ambiguity.

type Confidence int

MatchOption type #

A MatchOption configures a Matcher.

type MatchOption func(*matcher)

Tag type #

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.

type Tag compact.Tag

bases type #

bases implements sort.Interface and is used to sort base languages.

type bases []Base

Interfaces

Coverage interface #

The Coverage interface is used to define the level of coverage of an internationalization service. Note that not all types are supported by all services. As lists may be generated on the fly, it is recommended that users of a Coverage cache the results.

type Coverage interface {
Tags() []Tag
BaseLanguages() []Base
Scripts() []Script
Regions() []Region
}

Matcher interface #

Matcher is the interface that wraps the Match method. Match returns the best match for any of the given tags, along with a unique index associated with the returned tag and a confidence score.

type Matcher interface {
Match(t ...Tag) (tag Tag, index int, c Confidence)
}

ValueError interface #

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.

type ValueError interface {
error
Subtag() string
}

Structs

Base struct #

Base is an ISO 639 language code, used for encoding the base language of a language tag.

type Base struct {
langID language.Language
}

Extension struct #

Extension is a single BCP 47 extension.

type Extension struct {
s string
}

Region struct #

Region is an ISO 3166-1 or UN M.49 code for representing countries and regions.

type Region struct {
regionID language.Region
}

Script struct #

Script is a 4-letter ISO 15924 code for representing scripts. It is idiomatically represented in title case.

type Script struct {
scriptID language.Script
}

Variant struct #

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

type Variant struct {
variant string
}

allSubtags struct #

type allSubtags struct {

}

bestMatch struct #

bestMatch accumulates the best match so far.

type bestMatch struct {
have *haveTag
want language.Tag
conf Confidence
pinnedRegion language.Region
pinLanguage bool
sameRegionGroup bool
origLang bool
origReg bool
paradigmReg bool
regGroupDist uint8
origScript bool
}

coverage struct #

coverage is used by NewCoverage which is used as a convenient way for creating Coverage implementations for partially defined data. Very often a package will only need to define a subset of slices. coverage provides a convenient way to do this. Moreover, packages using NewCoverage, instead of their own implementation, will not break if later new slice types are added.

type coverage struct {
tags func() []Tag
bases func() []Base
scripts func() []Script
regions func() []Region
}

haveTag struct #

haveTag holds a supported Tag and its maximized script and region. The maximized or canonicalized language is not stored as it is not needed during matching.

type haveTag struct {
tag language.Tag
index int
conf Confidence
maxRegion language.Region
maxScript language.Script
altScript language.Script
nextMax uint16
}

matchHeader struct #

matchHeader has the lists of tags for exact matches and matches based on maximized and canonicalized tags for a given language.

type matchHeader struct {
haveTags []*haveTag
original bool
}

matcher struct #

matcher keeps a set of supported language tags, indexed by language.

type matcher struct {
default_ *haveTag
supported []*haveTag
index map[language.Language]*matchHeader
passSettings bool
preferSameScript bool
}

mutualIntelligibility struct #

type mutualIntelligibility struct {
want uint16
have uint16
distance uint8
oneway bool
}

regionIntelligibility struct #

type regionIntelligibility struct {
lang uint16
script uint8
group uint8
distance uint8
}

scriptIntelligibility struct #

type scriptIntelligibility struct {
wantLang uint16
haveLang uint16
wantScript uint8
haveScript uint8
distance uint8
}

tagSort struct #

type tagSort struct {
tag []Tag
q []float32
}

Functions

Base method #

Base returns the base language of the language tag. If the base language is unspecified, an attempt will be made to infer it from the context. It uses a variant of CLDR's Add Likely Subtags algorithm. This is subject to change.

func (t Tag) Base() (Base, Confidence)

BaseLanguages method #

BaseLanguages returns the result from calling s.bases if it is specified or otherwise derives the set of supported base languages from tags.

func (s *coverage) BaseLanguages() []Base

BaseLanguages method #

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

func (s allSubtags) BaseLanguages() []Base

Canonicalize method #

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 (r Region) Canonicalize() Region

Canonicalize method #

Canonicalize returns the canonicalized equivalent of the tag.

func (c CanonType) Canonicalize(t Tag) (Tag, error)

CompactIndex function #

CompactIndex returns an index, where 0 <= index < NumCompactTags, for tags for which data exists in the text repository.The index will change over time and should not be stored in persistent storage. If t does not match a compact index, exact will be false and the compact index will be returned for the first match after repeatedly taking the Parent of t.

func CompactIndex(t Tag) (index int, exact bool)

Compose method #

Compose creates a Tag from individual parts, which may be of type Tag, Base, Script, Region, Variant, []Variant, Extension, []Extension or error. If a Base, Script or Region or slice of type Variant or Extension is passed more than once, the latter will overwrite the former. Variants and Extensions are accumulated, but if two extensions of the same type are passed, the latter will replace the former. For -u extensions, though, the key-type pairs are added, where later values overwrite older ones. A Tag overwrites all former values and typically only makes sense as the first argument. The resulting tag is returned after canonicalizing using CanonType c. If one or more errors are encountered, one of the errors is returned.

func (c CanonType) Compose(part ...interface{}) (t Tag, err error)

Compose function #

Compose creates a Tag from individual parts, which may be of type Tag, Base, Script, Region, Variant, []Variant, Extension, []Extension or error. If a Base, Script or Region or slice of type Variant or Extension is passed more than once, the latter will overwrite the former. Variants and Extensions are accumulated, but if two extensions of the same type are passed, the latter will replace the former. For -u extensions, though, the key-type pairs are added, where later values overwrite older ones. A Tag overwrites all former values and typically only makes sense as the first argument. The resulting tag is returned after canonicalizing using the Default CanonType. If one or more errors are encountered, one of the errors is returned.

func Compose(part ...interface{}) (t Tag, err error)

Comprehends function #

Comprehends reports the confidence score for a speaker of a given language to being able to comprehend the written form of an alternative language.

func Comprehends(speaker Tag, alternative Tag) Confidence

Contains method #

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

func (r Region) Contains(c Region) bool

EncodeM49 function #

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

func EncodeM49(r int) (Region, error)

Extension method #

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 (t Tag) Extension(x byte) (ext Extension, ok bool)

Extensions method #

Extensions returns all extensions of t.

func (t Tag) Extensions() []Extension

ISO3 method #

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 (r Region) ISO3() string

ISO3 method #

ISO3 returns the ISO 639-3 language code.

func (b Base) ISO3() string

IsCountry method #

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

func (r Region) IsCountry() bool

IsGroup method #

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

func (r Region) IsGroup() bool

IsPrivateUse method #

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 (r Region) IsPrivateUse() bool

IsPrivateUse method #

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

func (b Base) IsPrivateUse() bool

IsPrivateUse method #

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

func (s Script) IsPrivateUse() bool

IsRoot method #

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

func (t Tag) IsRoot() bool

Len method #

func (s *tagSort) Len() int

Len method #

func (b bases) Len() int

Less method #

func (s *tagSort) Less(i int, j int) bool

Less method #

func (b bases) Less(i int, j int) bool

M49 method #

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

func (r Region) M49() int

Make method #

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

func (c CanonType) Make(s string) Tag

Make function #

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

func Make(s string) Tag

MarshalText method #

MarshalText implements encoding.TextMarshaler.

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

Match method #

func (m *matcher) Match(want ...Tag) (t Tag, index int, c Confidence)

MatchStrings function #

MatchStrings parses and matches the given strings until one of them matches the language in the Matcher. A string may be an Accept-Language header as handled by ParseAcceptLanguage. The default language is returned if no other language matched.

func MatchStrings(m Matcher, lang ...string) (tag Tag, index int)

MustParse function #

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

func MustParse(s string) Tag

MustParse method #

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

func (c CanonType) MustParse(s string) Tag

MustParseBase function #

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

func MustParseBase(s string) Base

MustParseRegion function #

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

func MustParseRegion(s string) Region

MustParseScript function #

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

func MustParseScript(s string) Script

NewCoverage function #

NewCoverage returns a Coverage for the given lists. It is typically used by packages providing internationalization services to define their level of coverage. A list may be of type []T or func() []T, where T is either Tag, Base, Script or Region. The returned Coverage derives the value for Bases from Tags if no func or slice for []Base is specified. For other unspecified types the returned Coverage will return nil for the respective methods.

func NewCoverage(list ...interface{}) Coverage

NewMatcher function #

NewMatcher returns a Matcher that matches an ordered list of preferred tags against a list of supported tags based on written intelligibility, closeness of dialect, equivalence of subtags and various other rules. It is initialized with the list of supported tags. The first element is used as the default value in case no match is found. Its Match method matches the first of the given Tags to reach a certain confidence threshold. The tags passed to Match should therefore be specified in order of preference. Extensions are ignored for matching. The index returned by the Match method corresponds to the index of the matched tag in t, but is augmented with the Unicode extension ('u')of the corresponding preferred tag. This allows user locale options to be passed transparently.

func NewMatcher(t []Tag, options ...MatchOption) Matcher

Parent method #

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. Parent returns a tag for a less specific language that is mutually intelligible or Und if there is no such language. This may not be the same as simply stripping the last BCP 47 subtag. For instance, the parent of "zh-TW" is "zh-Hant", and the parent of "zh-Hant" is "und".

func (t Tag) Parent() Tag

Parse function #

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. The resulting tag is canonicalized using the default canonicalization type.

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

Parse method #

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. The resulting tag is canonicalized using the canonicalization type c.

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

ParseAcceptLanguage function #

ParseAcceptLanguage parses the contents of an Accept-Language header as defined in http://www.ietf.org/rfc/rfc2616.txt and returns a list of Tags and a list of corresponding quality weights. It is more permissive than RFC 2616 and may return non-nil slices even if the input is not valid. The Tags will be sorted by highest weight first and then by first occurrence. Tags with a weight of zero will be dropped. An error will be returned if the input could not be parsed.

func ParseAcceptLanguage(s string) (tag []Tag, q []float32, err error)

ParseBase function #

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 ParseBase(s string) (Base, error)

ParseExtension function #

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

func ParseExtension(s string) (e Extension, err error)

ParseRegion function #

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 ParseRegion(s string) (Region, error)

ParseScript function #

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 ParseScript(s string) (Script, error)

ParseVariant function #

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

func ParseVariant(s string) (Variant, error)

PreferSameScript function #

PreferSameScript will, in the absence of a match, result in the first preferred tag with the same script as a supported tag to match this supported tag. The default is currently true, but this may change in the future.

func PreferSameScript(preferSame bool) MatchOption

Raw method #

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

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

Region method #

Region returns the region for the language tag. If it was not explicitly given, it will infer a most likely candidate from the context. It uses a variant of CLDR's Add Likely Subtags algorithm. This is subject to change.

func (t Tag) Region() (Region, Confidence)

Regions method #

Regions returns the list of supported regions. As all regions are in a consecutive range, it simply returns a slice of numbers in increasing order. The "undefined" region is not returned.

func (s allSubtags) Regions() []Region

Regions method #

func (s *coverage) Regions() []Region

Script method #

Script infers the script for the language tag. If it was not explicitly given, it will infer a most likely candidate. If more than one script is commonly used for a language, the most likely one is returned with a low confidence indication. For example, it returns (Cyrl, Low) for Serbian. If a script cannot be inferred (Zzzz, No) is returned. We do not use Zyyy (undetermined) as one would suspect from the IANA registry for BCP 47. In a Unicode context Zyyy marks common characters (like 1, 2, 3, '.', etc.) and is therefore more like multiple scripts. See https://www.unicode.org/reports/tr24/#Values for more details. Zzzz is also used for unknown value in CLDR. (Zzzz, Exact) is returned if Zzzz was explicitly specified. Note that an inferred script is never guaranteed to be the correct one. Latin is almost exclusively used for Afrikaans, but Arabic has been used for some texts in the past. Also, the script that is commonly used may change over time. It uses a variant of CLDR's Add Likely Subtags algorithm. This is subject to change.

func (t Tag) Script() (Script, Confidence)

Scripts method #

func (s *coverage) Scripts() []Script

Scripts method #

Scripts returns the list of supported scripts. As all scripts are in a consecutive range, it simply returns a slice of numbers in increasing order. The "undefined" script is not returned.

func (s allSubtags) Scripts() []Script

SetTypeForKey method #

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 (t Tag) SetTypeForKey(key string, value string) (Tag, error)

String method #

func (c Confidence) String() string

String method #

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

func (s Script) String() string

String method #

String returns the BCP 47 representation of the base language.

func (b Base) String() string

String method #

String returns the canonical string representation of the language tag.

func (t Tag) String() string

String method #

String returns the string representation of the variant.

func (v Variant) String() string

String method #

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

func (r Region) String() string

String method #

String returns the string representation of the extension, including the type tag.

func (e Extension) String() string

Swap method #

func (b bases) Swap(i int, j int)

Swap method #

func (s *tagSort) Swap(i int, j int)

TLD method #

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.

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

Tags method #

Tags always returns nil.

func (s allSubtags) Tags() []Tag

Tags method #

func (s *coverage) Tags() []Tag

Tokens method #

Tokens returns the list of tokens of e.

func (e Extension) Tokens() []string

Type method #

Type returns the one-byte extension type of e. It returns 0 for the zero exception.

func (e Extension) Type() byte

TypeForKey method #

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 (t Tag) TypeForKey(key string) string

UnmarshalText method #

UnmarshalText implements encoding.TextUnmarshaler.

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

Variants method #

Variants returns the variants specified explicitly for this language tag. or nil if no variant was specified.

func (t Tag) Variants() []Variant

addIfNew method #

addIfNew adds a haveTag to the list of tags only if it is a unique tag. Tags that have the same maximized values are linked by index.

func (h *matchHeader) addIfNew(n haveTag, exact bool)

altScript function #

altScript returns an alternative script that may match the given script with a low confidence. At the moment, the langMatch data allows for at most one script to map to another and we rely on this to keep the code simple.

func altScript(l language.Language, s language.Script) language.Script

canonicalize function #

canonicalize returns the canonicalized equivalent of the tag and whether there was any change.

func canonicalize(c CanonType, t language.Tag) (language.Tag, bool)

consume function #

consume removes a leading token c from s and returns the result or the empty string if there is no such token.

func consume(s string, c byte) string

equalsRest function #

equalsRest compares everything except the language.

func equalsRest(a language.Tag, b language.Tag) bool

getBest method #

getBest gets the best matching tag in m for any of the given tags, taking into account the order of preference of the given tags.

func (m *matcher) getBest(want ...Tag) (got *haveTag, orig language.Tag, c Confidence)

init function #

func init()

isCompact method #

func (t *Tag) isCompact() bool

isExactEquivalent function #

isExactEquivalent returns true if canonicalizing the language will not alter the script or region of a tag.

func isExactEquivalent(l language.Language) bool

isParadigmLocale function #

func isParadigmLocale(lang language.Language, r language.Region) bool

lang method #

TODO: improve performance.

func (t *Tag) lang() language.Language

makeHaveTag function #

func makeHaveTag(tag language.Tag, index int) (haveTag, language.Language)

makeTag function #

func makeTag(t language.Tag) (tag Tag)

newMatcher function #

newMatcher builds an index for the given supported tags and returns it as a matcher. It also expands the index by considering various equivalence classes for a given tag.

func newMatcher(supported []Tag, options []MatchOption) *matcher

nextToken function #

nextToken returns token t and the rest of the string.

func nextToken(s string) (t string, tail string)

region method #

func (t *Tag) region() language.Region

regionGroupDist function #

regionGroupDist computes the distance between two regions based on their CLDR grouping.

func regionGroupDist(a language.Region, b language.Region, script language.Script, lang language.Language) (dist uint8, same bool)

script method #

func (t *Tag) script() language.Script

split function #

func split(s string, c byte) (head string, tail string)

tag method #

func (t *Tag) tag() language.Tag

toConf function #

func toConf(d uint8) Confidence

update function #

func update(b *language.Builder, part ...interface{}) (err error)

update method #

update updates the existing best match if the new pair is considered to be a better match. To determine if the given pair is a better match, it first computes the rough confidence level. If this surpasses the current match, it will replace it and update the tie-breaker rule cache. If there is a tie, it proceeds with applying a series of tie-breaker rules. If there is no conclusive winner after applying the tie-breaker rules, it leaves the current match as the preferred match. If pin is true and have and tag are a strong match, it will henceforth only consider matches for this language. This corresponds to the idea that most users have a strong preference for the first defined language. A user can still prefer a second language over a dialect of the preferred language by explicitly specifying dialects, e.g. "en, nl, en-GB". In this case pin should be false.

func (m *bestMatch) update(have *haveTag, tag language.Tag, maxScript language.Script, maxRegion language.Region, pin bool)

Generated with Arrow