Path: blob/main/vendor/golang.org/x/text/language/doc.go
2880 views
// Copyright 2017 The Go Authors. All rights reserved.1// Use of this source code is governed by a BSD-style2// license that can be found in the LICENSE file.34// Package language implements BCP 47 language tags and related functionality.5//6// The most important function of package language is to match a list of7// user-preferred languages to a list of supported languages.8// It alleviates the developer of dealing with the complexity of this process9// and provides the user with the best experience10// (see https://blog.golang.org/matchlang).11//12// # Matching preferred against supported languages13//14// A Matcher for an application that supports English, Australian English,15// Danish, and standard Mandarin can be created as follows:16//17// var matcher = language.NewMatcher([]language.Tag{18// language.English, // The first language is used as fallback.19// language.MustParse("en-AU"),20// language.Danish,21// language.Chinese,22// })23//24// This list of supported languages is typically implied by the languages for25// which there exists translations of the user interface.26//27// User-preferred languages usually come as a comma-separated list of BCP 4728// language tags.29// The MatchString finds best matches for such strings:30//31// handler(w http.ResponseWriter, r *http.Request) {32// lang, _ := r.Cookie("lang")33// accept := r.Header.Get("Accept-Language")34// tag, _ := language.MatchStrings(matcher, lang.String(), accept)35//36// // tag should now be used for the initialization of any37// // locale-specific service.38// }39//40// The Matcher's Match method can be used to match Tags directly.41//42// Matchers are aware of the intricacies of equivalence between languages, such43// as deprecated subtags, legacy tags, macro languages, mutual44// intelligibility between scripts and languages, and transparently passing45// BCP 47 user configuration.46// For instance, it will know that a reader of Bokmål Danish can read Norwegian47// and will know that Cantonese ("yue") is a good match for "zh-HK".48//49// # Using match results50//51// To guarantee a consistent user experience to the user it is important to52// use the same language tag for the selection of any locale-specific services.53// For example, it is utterly confusing to substitute spelled-out numbers54// or dates in one language in text of another language.55// More subtly confusing is using the wrong sorting order or casing56// algorithm for a certain language.57//58// All the packages in x/text that provide locale-specific services59// (e.g. collate, cases) should be initialized with the tag that was60// obtained at the start of an interaction with the user.61//62// Note that Tag that is returned by Match and MatchString may differ from any63// of the supported languages, as it may contain carried over settings from64// the user tags.65// This may be inconvenient when your application has some additional66// locale-specific data for your supported languages.67// Match and MatchString both return the index of the matched supported tag68// to simplify associating such data with the matched tag.69//70// # Canonicalization71//72// If one uses the Matcher to compare languages one does not need to73// worry about canonicalization.74//75// The meaning of a Tag varies per application. The language package76// therefore delays canonicalization and preserves information as much77// as possible. The Matcher, however, will always take into account that78// two different tags may represent the same language.79//80// By default, only legacy and deprecated tags are converted into their81// canonical equivalent. All other information is preserved. This approach makes82// the confidence scores more accurate and allows matchers to distinguish83// between variants that are otherwise lost.84//85// As a consequence, two tags that should be treated as identical according to86// BCP 47 or CLDR, like "en-Latn" and "en", will be represented differently. The87// Matcher handles such distinctions, though, and is aware of the88// equivalence relations. The CanonType type can be used to alter the89// canonicalization form.90//91// # References92//93// BCP 47 - Tags for Identifying Languages http://tools.ietf.org/html/bcp4794package language // import "golang.org/x/text/language"9596// TODO: explanation on how to match languages for your own locale-specific97// service.9899100