pkg.go.dev Open in urlscan Pro
2600:1901:0:f535::  Public Scan

Submitted URL: http://golang.org/x/text/language
Effective URL: https://pkg.go.dev/golang.org/x/text/language
Submission: On September 09 via api from US — Scanned from DE

Form analysis 3 forms found in the DOM

/search

<form class="go-InputGroup go-ShortcutKey go-SearchForm-form" action="/search" data-shortcut="/" data-shortcut-alt="search" data-gtmc="search form" aria-label="Search for a package" role="search">
  <input name="q" class="go-Input js-searchFocus" aria-label="Search for a package" type="search" autocapitalize="off" autocomplete="off" autocorrect="off" spellcheck="false" placeholder="Search packages or symbols" value="">
  <input name="m" value="" hidden="">
  <button class="go-Button go-Button--inverted" aria-label="Submit search">
    <img class="go-Icon" height="24" width="24" src="/static/shared/icon/search_gm_grey_24dp.svg" alt="">
  </button>
</form>

DIALOG

<form method="dialog" data-gmtc="jump to form" aria-label="Jump to Identifier">
  <div class="Dialog-title go-Modal-header">
    <h2>Jump to</h2>
    <button class="go-Button go-Button--inline" type="button" data-modal-close="" data-gtmc="modal button" aria-label="Close">
      <img class="go-Icon" height="24" width="24" src="/static/shared/icon/close_gm_grey_24dp.svg" alt="">
    </button>
  </div>
  <div class="JumpDialog-filter">
    <input class="JumpDialog-input go-Input" autocomplete="off" type="text">
  </div>
  <div class="JumpDialog-body go-Modal-body">
    <div class="JumpDialog-list"></div>
  </div>
  <div class="go-Modal-actions">
    <button class="go-Button" data-test-id="close-dialog">Close</button>
  </div>
</form>

DIALOG

<form method="dialog">
  <div class="go-Modal-header">
    <h2>Keyboard shortcuts</h2>
    <button class="go-Button go-Button--inline" type="button" data-modal-close="" data-gtmc="modal button" aria-label="Close">
      <img class="go-Icon" height="24" width="24" src="/static/shared/icon/close_gm_grey_24dp.svg" alt="">
    </button>
  </div>
  <div class="go-Modal-body">
    <table>
      <tbody>
        <tr>
          <td class="ShortcutsDialog-key">
            <strong>?</strong>
          </td>
          <td> : This menu</td>
        </tr>
        <tr>
          <td class="ShortcutsDialog-key">
            <strong>/</strong>
          </td>
          <td> : Search site</td>
        </tr>
        <tr>
          <td class="ShortcutsDialog-key">
            <strong>f</strong> or <strong>F</strong>
          </td>
          <td> : Jump to</td>
        </tr>
        <tr>
          <td class="ShortcutsDialog-key"><strong>y</strong> or <strong>Y</strong></td>
          <td> : Canonical URL</td>
        </tr>
      </tbody>
    </table>
  </div>
  <div class="go-Modal-actions">
    <button class="go-Button" data-test-id="close-dialog">Close</button>
  </div>
</form>

Text Content

Skip to Main Content
 * Why Go
   * Case Studies
     
     Common problems companies solve with Go
   
   * Use Cases
     
     Stories about how and why companies use Go
   
   * Security Policy
     
     How Go can help keep you secure by default
 * Learn
 * Docs
   * Effective Go
     
     Tips for writing clear, performant, and idiomatic Go code
   
   * Go User Manual
     
     A complete introduction to building software with Go
   
   * Standard library
     
     Reference documentation for Go's standard library
   
   * Release Notes
     
     Learn what's new in each Go release
 * Packages
 * Community
   * Recorded Talks
     
     Videos from prior events
   
   * Meetups
     
     Meet other local Go developers
   
   * Conferences
     
     Learn and network with Go developers from around the world
   
   * Go blog
     
     The Go project's official blog.
   
   * Go project
     
     Get help and stay informed from Go
   
   * Get connected
     
     
     
     

 * Why Go
   Why Go
    * Case Studies
    * Use Cases
    * Security Policy

 * Learn
 * Docs
   Docs
    * Effective Go
    * Go User Manual
    * Standard library
    * Release Notes

 * Packages
 * Community
   Community
    * Recorded Talks
    * Meetups
    * Conferences
    * Go blog
    * Go project
    * Get connected
      


 1. Discover Packages
 2. golang.org/x/text
 3. language


LANGUAGE

package
Version: v0.18.0
Opens a new window with list of versions in this module.
Latest Latest

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

Go to latest Published: Aug 20, 2024 License: BSD-3-Clause
Opens a new window with license information.
Imports: 7
Opens a new window with list of imports.
Imported by: 8,702
Opens a new window with list of known importers.
Main Versions Licenses Imports Imported By


DETAILS

 * Valid go.mod file
   
   The Go module system was introduced in Go 1.11 and is the official dependency
   management solution for Go.

 * Redistributable license
   
   Redistributable licenses place minimal restrictions on how software can be
   used, modified, and redistributed.

 * Tagged version
   
   Modules with tagged versions give importers more predictable builds.

 * Stable version
   
   When a project reaches major version v1 it is considered stable.

 * Learn more about best practices


REPOSITORY

cs.opensource.google/go/x/text


LINKS

 * Report a Vulnerability
 * Open Source Insights

Jump to ...
 * Documentation
   * Overview
   * Index
     * Examples
   * Constants
   * Variables
   * Functions
     * CompactIndex(t)
   * Types
     * type Base
       * MustParseBase(s)
       * ParseBase(s)
       * (b) ISO3()
       * (b) IsPrivateUse()
       * (b) String()
     * type CanonType
       * (c) Canonicalize(t)
       * (c) Compose(part)
       * (c) Make(s)
       * (c) MustParse(s)
       * (c) Parse(s)
     * type Confidence
       * Comprehends(speaker, alternative)
       * (c) String()
     * type Coverage
       * NewCoverage(list)
     * type Extension
       * ParseExtension(s)
       * (e) String()
       * (e) Tokens()
       * (e) Type()
     * type MatchOption
       * PreferSameScript(preferSame)
     * type Matcher
       * NewMatcher(t, options)
     * type Region
       * EncodeM49(r)
       * MustParseRegion(s)
       * ParseRegion(s)
       * (r) Canonicalize()
       * (r) Contains(c)
       * (r) ISO3()
       * (r) IsCountry()
       * (r) IsGroup()
       * (r) IsPrivateUse()
       * (r) M49()
       * (r) String()
       * (r) TLD()
     * type Script
       * MustParseScript(s)
       * ParseScript(s)
       * (s) IsPrivateUse()
       * (s) String()
     * type Tag
       * Compose(part)
       * Make(s)
       * MatchStrings(m, lang)
       * MustParse(s)
       * Parse(s)
       * ParseAcceptLanguage(s)
       * (t) Base()
       * (t) Extension(x)
       * (t) Extensions()
       * (t) IsRoot()
       * (t) MarshalText()
       * (t) Parent()
       * (t) Raw()
       * (t) Region()
       * (t) Script()
       * (t) SetTypeForKey(key, value)
       * (t) String()
       * (t) TypeForKey(key)
       * (t) UnmarshalText(text)
       * (t) Variants()
     * type ValueError
     * type Variant
       * ParseVariant(s)
       * (v) String()
 * Source Files
 * Directories

DocumentationSource
FilesDirectoriesOverviewIndexConstantsVariablesFunctionsTypesExamplesCompactIndex(t)type
Basetype CanonTypetype Confidencetype Coveragetype Extensiontype MatchOptiontype
Matchertype Regiontype Scripttype Tagtype ValueErrortype
VariantMustParseBase(s)ParseBase(s)(b) ISO3()(b) IsPrivateUse()(b) String()(c)
Canonicalize(t)(c) Compose(part)(c) Make(s)(c) MustParse(s)(c)
Parse(s)Comprehends(speaker, alternative)(c)
String()NewCoverage(list)ParseExtension(s)(e) String()(e) Tokens()(e)
Type()PreferSameScript(preferSame)NewMatcher(t,
options)EncodeM49(r)MustParseRegion(s)ParseRegion(s)(r) Canonicalize()(r)
Contains(c)(r) ISO3()(r) IsCountry()(r) IsGroup()(r) IsPrivateUse()(r) M49()(r)
String()(r) TLD()MustParseScript(s)ParseScript(s)(s) IsPrivateUse()(s)
String()Compose(part)Make(s)MatchStrings(m,
lang)MustParse(s)Parse(s)ParseAcceptLanguage(s)(t) Base()(t) Extension(x)(t)
Extensions()(t) IsRoot()(t) MarshalText()(t) Parent()(t) Raw()(t) Region()(t)
Script()(t) SetTypeForKey(key, value)(t) String()(t) TypeForKey(key)(t)
UnmarshalText(text)(t) Variants()ParseVariant(s)(v) String()


DOCUMENTATION ¶


OVERVIEW ¶

 * Matching preferred against supported languages
 * Using match results
 * Canonicalization
 * References

Package language implements BCP 47 language tags and related functionality.

The most important function of package language is to match a list of
user-preferred languages to a list of supported languages. It alleviates the
developer of dealing with the complexity of this process and provides the user
with the best experience (see https://blog.golang.org/matchlang).

MATCHING PREFERRED AGAINST SUPPORTED LANGUAGES ¶

A Matcher for an application that supports English, Australian English, Danish,
and standard Mandarin can be created as follows:

var matcher = language.NewMatcher([]language.Tag{
    language.English,   // The first language is used as fallback.
    language.MustParse("en-AU"),
    language.Danish,
    language.Chinese,
})


This list of supported languages is typically implied by the languages for which
there exists translations of the user interface.

User-preferred languages usually come as a comma-separated list of BCP 47
language tags. The MatchString finds best matches for such strings:

handler(w http.ResponseWriter, r *http.Request) {
    lang, _ := r.Cookie("lang")
    accept := r.Header.Get("Accept-Language")
    tag, _ := language.MatchStrings(matcher, lang.String(), accept)

    // tag should now be used for the initialization of any
    // locale-specific service.
}


The Matcher's Match method can be used to match Tags directly.

Matchers are aware of the intricacies of equivalence between languages, such as
deprecated subtags, legacy tags, macro languages, mutual intelligibility between
scripts and languages, and transparently passing BCP 47 user configuration. For
instance, it will know that a reader of Bokmål Danish can read Norwegian and
will know that Cantonese ("yue") is a good match for "zh-HK".

USING MATCH RESULTS ¶

To guarantee a consistent user experience to the user it is important to use the
same language tag for the selection of any locale-specific services. For
example, it is utterly confusing to substitute spelled-out numbers or dates in
one language in text of another language. More subtly confusing is using the
wrong sorting order or casing algorithm for a certain language.

All the packages in x/text that provide locale-specific services (e.g. collate,
cases) should be initialized with the tag that was obtained at the start of an
interaction with the user.

Note that Tag that is returned by Match and MatchString may differ from any of
the supported languages, as it may contain carried over settings from the user
tags. This may be inconvenient when your application has some additional
locale-specific data for your supported languages. Match and MatchString both
return the index of the matched supported tag to simplify associating such data
with the matched tag.

CANONICALIZATION ¶

If one uses the Matcher to compare languages one does not need to worry about
canonicalization.

The meaning of a Tag varies per application. The language package therefore
delays canonicalization and preserves information as much as possible. The
Matcher, however, will always take into account that two different tags may
represent the same language.

By default, only legacy and deprecated tags are converted into their canonical
equivalent. All other information is preserved. This approach makes the
confidence scores more accurate and allows matchers to distinguish between
variants that are otherwise lost.

As a consequence, two tags that should be treated as identical according to BCP
47 or CLDR, like "en-Latn" and "en", will be represented differently. The
Matcher handles such distinctions, though, and is aware of the equivalence
relations. The CanonType type can be used to alter the canonicalization form.

REFERENCES ¶

BCP 47 - Tags for Identifying Languages http://tools.ietf.org/html/bcp47


INDEX ¶

 * Constants
 * Variables
 * func CompactIndex(t Tag) (index int, exact bool)
 * type Base
 * * func MustParseBase(s string) Base
   * func ParseBase(s string) (Base, error)
 * * func (b Base) ISO3() string
   * func (b Base) IsPrivateUse() bool
   * func (b Base) String() string
 * type CanonType
 * * func (c CanonType) Canonicalize(t Tag) (Tag, error)
   * func (c CanonType) Compose(part ...interface{}) (t Tag, err error)
   * func (c CanonType) Make(s string) Tag
   * func (c CanonType) MustParse(s string) Tag
   * func (c CanonType) Parse(s string) (t Tag, err error)
 * type Confidence
 * * func Comprehends(speaker, alternative Tag) Confidence
 * * func (c Confidence) String() string
 * type Coverage
 * * func NewCoverage(list ...interface{}) Coverage
 * type Extension
 * * func ParseExtension(s string) (e Extension, err error)
 * * func (e Extension) String() string
   * func (e Extension) Tokens() []string
   * func (e Extension) Type() byte
 * type MatchOption
 * * func PreferSameScript(preferSame bool) MatchOption
 * type Matcher
 * * func NewMatcher(t []Tag, options ...MatchOption) Matcher
 * type Region
 * * func EncodeM49(r int) (Region, error)
   * func MustParseRegion(s string) Region
   * func ParseRegion(s string) (Region, error)
 * * 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
 * * func MustParseScript(s string) Script
   * func ParseScript(s string) (Script, error)
 * * func (s Script) IsPrivateUse() bool
   * func (s Script) String() string
 * type Tag
 * * func Compose(part ...interface{}) (t Tag, err error)
   * func Make(s string) Tag
   * func MatchStrings(m Matcher, lang ...string) (tag Tag, index int)
   * func MustParse(s string) Tag
   * func Parse(s string) (t Tag, err error)
   * func ParseAcceptLanguage(s string) (tag []Tag, q []float32, err error)
 * * func (t Tag) Base() (Base, Confidence)
   * func (t Tag) Extension(x byte) (ext Extension, ok bool)
   * func (t Tag) Extensions() []Extension
   * func (t Tag) IsRoot() bool
   * func (t Tag) MarshalText() (text []byte, err error)
   * func (t Tag) Parent() Tag
   * func (t Tag) Raw() (b Base, s Script, r Region)
   * func (t Tag) Region() (Region, Confidence)
   * func (t Tag) Script() (Script, Confidence)
   * 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) Variants() []Variant
 * type ValueError
 * type Variant
 * * func ParseVariant(s string) (Variant, error)
 * * func (v Variant) String() string

EXAMPLES ¶

 * CanonType
 * Compose
 * Comprehends
 * MatchStrings
 * Matcher
 * Parse (Errors)
 * ParseAcceptLanguage
 * Region.TLD
 * Tag (Values)
 * Tag.Base
 * Tag.Parent
 * Tag.Region
 * Tag.Script


CONSTANTS ¶

View Source

const (
	// Replace deprecated base languages with their preferred replacements.
	DeprecatedBase CanonType = 1 << iota
	// Replace deprecated scripts with their preferred replacements.
	DeprecatedScript
	// Replace deprecated regions with their preferred replacements.
	DeprecatedRegion
	// Remove redundant scripts.
	SuppressScript
	// Normalize legacy encodings. This includes legacy languages defined in
	// CLDR as well as bibliographic codes defined in ISO-639.
	Legacy
	// Map the dominant language of a macro language group to the macro language
	// subtag. For example cmn -> zh.
	Macro
	// 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.
	CLDR

	// Raw can be used to Compose or Parse without Canonicalization.
	Raw CanonType = 0

	// Replace all deprecated tags with their preferred replacements.
	Deprecated = DeprecatedBase | DeprecatedScript | DeprecatedRegion

	// All canonicalizations recommended by BCP 47.
	BCP47 = Deprecated | SuppressScript

	// All canonicalizations.
	All = BCP47 | Legacy | Macro

	// 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.
	Default = Deprecated | Legacy
)

View Source

const CLDRVersion = "32"

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

View Source

const NumCompactTags = compact.NumCompactTags

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


VARIABLES ¶

View Source

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

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


FUNCTIONS ¶

FUNC COMPACTINDEX ¶

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

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.


TYPES ¶

TYPE BASE ¶

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

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

FUNC MUSTPARSEBASE ¶

func MustParseBase(s string) Base

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) (Base, 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 (BASE) ISO3 ¶

func (b Base) ISO3() string

ISO3 returns the ISO 639-3 language code.

FUNC (BASE) ISPRIVATEUSE ¶

func (b Base) IsPrivateUse() bool

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

FUNC (BASE) STRING ¶

func (b Base) String() string

String returns the BCP 47 representation of the base language.

TYPE CANONTYPE ¶

type CanonType int

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

Example ¶

Output:

Default(en-Latn) -> en-Latn
BCP47(en-Latn) -> en
Macro(en-Latn) -> en-Latn
All(en-Latn) -> en
Default(sh) -> sr-Latn
BCP47(sh) -> sh
Macro(sh) -> sh
All(sh) -> sr-Latn
Default(zh-cmn) -> cmn
BCP47(zh-cmn) -> cmn
Macro(zh-cmn) -> zh
All(zh-cmn) -> zh
Default(bjd) -> drl
BCP47(bjd) -> drl
Macro(bjd) -> bjd
All(bjd) -> drl
Default(iw-Latn-fonipa-u-cu-usd) -> he-Latn-fonipa-u-cu-usd
BCP47(iw-Latn-fonipa-u-cu-usd) -> he-Latn-fonipa-u-cu-usd
Macro(iw-Latn-fonipa-u-cu-usd) -> iw-Latn-fonipa-u-cu-usd
All(iw-Latn-fonipa-u-cu-usd) -> he-Latn-fonipa-u-cu-usd




Share Format Run

FUNC (CANONTYPE) CANONICALIZE ¶

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

Canonicalize returns the canonicalized equivalent of the tag.

FUNC (CANONTYPE) COMPOSE ¶

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

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 (CANONTYPE) MAKE ¶

func (c CanonType) Make(s string) Tag

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

FUNC (CANONTYPE) MUSTPARSE ¶

func (c CanonType) 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 (CANONTYPE) PARSE ¶

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

TYPE CONFIDENCE ¶

type Confidence int

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.

const (
	No    Confidence = iota // full confidence that there was no match
	Low                     // most likely value picked out of a set of alternatives
	High                    // value is generally assumed to be the correct match
	Exact                   // exact match or explicitly specified value
)

FUNC COMPREHENDS ¶

func Comprehends(speaker, alternative Tag) Confidence

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

Example ¶

Output:

Exact
High
No
----
No
Low
----
High
No




Share Format Run

FUNC (CONFIDENCE) STRING ¶

func (c Confidence) String() string

TYPE COVERAGE ¶

type Coverage interface {
	// Tags returns the list of supported tags.
	Tags() []Tag

	// BaseLanguages returns the list of supported base languages.
	BaseLanguages() []Base

	// Scripts returns the list of supported scripts.
	Scripts() []Script

	// Regions returns the list of supported regions.
	Regions() []Region
}

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.

var (
	// Supported defines a Coverage that lists all supported subtags. Tags
	// always returns nil.
	Supported Coverage = allSubtags{}
)

FUNC NEWCOVERAGE ¶

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

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.

TYPE EXTENSION ¶

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

Extension is a single BCP 47 extension.

FUNC PARSEEXTENSION ¶

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

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

FUNC (EXTENSION) STRING ¶

func (e Extension) String() string

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

FUNC (EXTENSION) TOKENS ¶

func (e Extension) Tokens() []string

Tokens returns the list of tokens of e.

FUNC (EXTENSION) TYPE ¶

func (e Extension) Type() byte

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

TYPE MATCHOPTION ¶

type MatchOption func(*matcher)

A MatchOption configures a Matcher.

FUNC PREFERSAMESCRIPT ¶

func PreferSameScript(preferSame bool) MatchOption

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.

TYPE MATCHER ¶

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

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.

Example ¶

ExampleMatcher_bestMatch gives some examples of getting the best match of a set
of tags to any of the tags of given set.

Output:

fr 2 Exact
en-GB-u-rg-auzzzz 1 High
en 0 No
en 0 No
----
zh-Hans 6 Low
en-GB-u-rg-auzzzz 1 High
----
pt-BR 4 Exact
fr-u-rg-bezzzz 2 High
af-u-rg-nazzzz 3 High
----
iw-IL 7 Exact
he-u-rg-ilzzzz 9 Exact
----
fr-u-cu-frf 2 Exact
fr-u-cu-frf 2 High
en-u-co-phonebk 0 No




Share Format Run

FUNC NEWMATCHER ¶

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

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.

TYPE REGION ¶

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

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

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.

Example ¶

Output:

US <nil>
UK <nil>
UK <nil>
ZZ language: region is not a valid ccTLD
US <nil>
UK <nil>
UK <nil>
MM <nil>




Share Format Run

TYPE SCRIPT ¶

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

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

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 compact.Tag

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.

Example (Values) ¶

Output:

true true
true false
true




Share Format Run

var (
	Und Tag = Tag{}

	Afrikaans            Tag = Tag(compact.Afrikaans)
	Amharic              Tag = Tag(compact.Amharic)
	Arabic               Tag = Tag(compact.Arabic)
	ModernStandardArabic Tag = Tag(compact.ModernStandardArabic)
	Azerbaijani          Tag = Tag(compact.Azerbaijani)
	Bulgarian            Tag = Tag(compact.Bulgarian)
	Bengali              Tag = Tag(compact.Bengali)
	Catalan              Tag = Tag(compact.Catalan)
	Czech                Tag = Tag(compact.Czech)
	Danish               Tag = Tag(compact.Danish)
	German               Tag = Tag(compact.German)
	Greek                Tag = Tag(compact.Greek)
	English              Tag = Tag(compact.English)
	AmericanEnglish      Tag = Tag(compact.AmericanEnglish)
	BritishEnglish       Tag = Tag(compact.BritishEnglish)
	Spanish              Tag = Tag(compact.Spanish)
	EuropeanSpanish      Tag = Tag(compact.EuropeanSpanish)
	LatinAmericanSpanish Tag = Tag(compact.LatinAmericanSpanish)
	Estonian             Tag = Tag(compact.Estonian)
	Persian              Tag = Tag(compact.Persian)
	Finnish              Tag = Tag(compact.Finnish)
	Filipino             Tag = Tag(compact.Filipino)
	French               Tag = Tag(compact.French)
	CanadianFrench       Tag = Tag(compact.CanadianFrench)
	Gujarati             Tag = Tag(compact.Gujarati)
	Hebrew               Tag = Tag(compact.Hebrew)
	Hindi                Tag = Tag(compact.Hindi)
	Croatian             Tag = Tag(compact.Croatian)
	Hungarian            Tag = Tag(compact.Hungarian)
	Armenian             Tag = Tag(compact.Armenian)
	Indonesian           Tag = Tag(compact.Indonesian)
	Icelandic            Tag = Tag(compact.Icelandic)
	Italian              Tag = Tag(compact.Italian)
	Japanese             Tag = Tag(compact.Japanese)
	Georgian             Tag = Tag(compact.Georgian)
	Kazakh               Tag = Tag(compact.Kazakh)
	Khmer                Tag = Tag(compact.Khmer)
	Kannada              Tag = Tag(compact.Kannada)
	Korean               Tag = Tag(compact.Korean)
	Kirghiz              Tag = Tag(compact.Kirghiz)
	Lao                  Tag = Tag(compact.Lao)
	Lithuanian           Tag = Tag(compact.Lithuanian)
	Latvian              Tag = Tag(compact.Latvian)
	Macedonian           Tag = Tag(compact.Macedonian)
	Malayalam            Tag = Tag(compact.Malayalam)
	Mongolian            Tag = Tag(compact.Mongolian)
	Marathi              Tag = Tag(compact.Marathi)
	Malay                Tag = Tag(compact.Malay)
	Burmese              Tag = Tag(compact.Burmese)
	Nepali               Tag = Tag(compact.Nepali)
	Dutch                Tag = Tag(compact.Dutch)
	Norwegian            Tag = Tag(compact.Norwegian)
	Punjabi              Tag = Tag(compact.Punjabi)
	Polish               Tag = Tag(compact.Polish)
	Portuguese           Tag = Tag(compact.Portuguese)
	BrazilianPortuguese  Tag = Tag(compact.BrazilianPortuguese)
	EuropeanPortuguese   Tag = Tag(compact.EuropeanPortuguese)
	Romanian             Tag = Tag(compact.Romanian)
	Russian              Tag = Tag(compact.Russian)
	Sinhala              Tag = Tag(compact.Sinhala)
	Slovak               Tag = Tag(compact.Slovak)
	Slovenian            Tag = Tag(compact.Slovenian)
	Albanian             Tag = Tag(compact.Albanian)
	Serbian              Tag = Tag(compact.Serbian)
	SerbianLatin         Tag = Tag(compact.SerbianLatin)
	Swedish              Tag = Tag(compact.Swedish)
	Swahili              Tag = Tag(compact.Swahili)
	Tamil                Tag = Tag(compact.Tamil)
	Telugu               Tag = Tag(compact.Telugu)
	Thai                 Tag = Tag(compact.Thai)
	Turkish              Tag = Tag(compact.Turkish)
	Ukrainian            Tag = Tag(compact.Ukrainian)
	Urdu                 Tag = Tag(compact.Urdu)
	Uzbek                Tag = Tag(compact.Uzbek)
	Vietnamese           Tag = Tag(compact.Vietnamese)
	Chinese              Tag = Tag(compact.Chinese)
	SimplifiedChinese    Tag = Tag(compact.SimplifiedChinese)
	TraditionalChinese   Tag = Tag(compact.TraditionalChinese)
	Zulu                 Tag = Tag(compact.Zulu)
)

FUNC COMPOSE ¶

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

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.

Example ¶

Output:

nl-US <nil>
nl-x-piglatin <nil>
ja-US <nil>
nl-US-u-nu-arabic <nil>
nl-1901-u-co-phonebk <nil>
nl-1901-u-co-phonebk-nu-arabic <nil>
und-1901-u-co-phonebk <nil>
de-u-co-phonebk <nil>
de-1901 <nil>
fi <nil>
de <nil>
und-ZA <nil>
und language: subtag "HH" is well-formed but unknown
en-Latn-GB <nil>
en-GB <nil>




Share Format Run

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 MATCHSTRINGS ¶

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

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.

Example ¶

Output:





Share Format Run

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

Example (Errors) ¶

Output:

foo
Foobar
aa-Uuuu: culprit "Uuuu"
AC: culprit "ac"
ac-u: ill-formed




Share Format Run

FUNC PARSEACCEPTLANGUAGE ¶

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

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.

Example ¶

Output:

            en-GB (t: [    en  en-US     nn]; q: [  1 0.8 0.3]; err: <nil>)
en-GB-u-rg-uszzzz (t: [   gsw  en-US     en]; q: [  1 0.8 0.7]; err: <nil>)
               de (t: [   gsw     nl     da]; q: [  1   1   1]; err: <nil>)
            en-GB (t: []; q: []; err: language: tag is not well-formed)




Share Format Run

FUNC (TAG) BASE ¶

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

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.

Example ¶

Output:

en Low
en High
nl High
es Low
en Low




Share Format Run

FUNC (TAG) EXTENSION ¶

func (t Tag) Extension(x byte) (ext Extension, 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() []Extension

Extensions returns all extensions of t.

FUNC (TAG) ISROOT ¶

func (t Tag) IsRoot() bool

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

FUNC (TAG) MARSHALTEXT ¶ ADDED IN V0.2.0

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

MarshalText implements encoding.TextMarshaler.

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.

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".

Example ¶

Output:

parent(zh-CN): zh
parent(en-AU): en-001
parent(zh-HK): zh-Hant
parent(zh-Hant): und
parent(de-1994-u-co-phonebk): de
parent(de-Latn-LU): de




Share Format Run

FUNC (TAG) RAW ¶

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

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

FUNC (TAG) REGION ¶

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

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.

Example ¶

Output:

RU Low
US Low




Share Format Run

FUNC (TAG) SCRIPT ¶

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

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.

Example ¶

Output:

Latn High
Cyrl Low
false
true




Share Format Run

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.

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 ¶ ADDED IN V0.2.0

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

UnmarshalText implements encoding.TextUnmarshaler.

FUNC (TAG) VARIANTS ¶

func (t Tag) Variants() []Variant

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

TYPE VALUEERROR ¶

type ValueError interface {
	error

	// Subtag returns the subtag for which the error occurred.
	Subtag() string
}

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 VARIANT ¶

type Variant struct {
	// 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.


SOURCE FILES ¶

View all Source files
 * coverage.go
 * doc.go
 * language.go
 * match.go
 * parse.go
 * tables.go
 * tags.go


DIRECTORIES ¶

Show internal Collapse all

Path Synopsis
display
Package display provides display names for languages, scripts and regions in a
requested language.
Package display provides display names for languages, scripts and regions in a
requested language.

Click to show internal directories.
Click to hide internal directories.

Why Go Use Cases Case Studies
Get Started Playground Tour Stack Overflow Help
Packages Standard Library Sub-repositories About Go Packages
About Download Blog Issue Tracker Release Notes Brand Guidelines Code of Conduct
Connect Twitter GitHub Slack r/golang Meetup Golang Weekly
 * Copyright
 * Terms of Service
 * Privacy Policy
 * Report an Issue

 * Theme Toggle

 * Shortcuts Modal


JUMP TO



Close


KEYBOARD SHORTCUTS

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

Close
go.dev uses cookies from Google to deliver and enhance the quality of its
services and to analyze traffic. Learn more.
Okay