doc.go 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2017 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package language implements BCP 47 language tags and related functionality.
  5. //
  6. // The most important function of package language is to match a list of
  7. // user-preferred languages to a list of supported languages.
  8. // It alleviates the developer of dealing with the complexity of this process
  9. // and provides the user with the best experience
  10. // (see https://blog.golang.org/matchlang).
  11. //
  12. // # Matching preferred against supported languages
  13. //
  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 for
  25. // which there exists translations of the user interface.
  26. //
  27. // User-preferred languages usually come as a comma-separated list of BCP 47
  28. // 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 any
  37. // // 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, such
  43. // as deprecated subtags, legacy tags, macro languages, mutual
  44. // intelligibility between scripts and languages, and transparently passing
  45. // BCP 47 user configuration.
  46. // For instance, it will know that a reader of Bokmål Danish can read Norwegian
  47. // and will know that Cantonese ("yue") is a good match for "zh-HK".
  48. //
  49. // # Using match results
  50. //
  51. // To guarantee a consistent user experience to the user it is important to
  52. // use the same language tag for the selection of any locale-specific services.
  53. // For example, it is utterly confusing to substitute spelled-out numbers
  54. // or dates in one language in text of another language.
  55. // More subtly confusing is using the wrong sorting order or casing
  56. // algorithm for a certain language.
  57. //
  58. // All the packages in x/text that provide locale-specific services
  59. // (e.g. collate, cases) should be initialized with the tag that was
  60. // 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 any
  63. // of the supported languages, as it may contain carried over settings from
  64. // the user tags.
  65. // This may be inconvenient when your application has some additional
  66. // locale-specific data for your supported languages.
  67. // Match and MatchString both return the index of the matched supported tag
  68. // to simplify associating such data with the matched tag.
  69. //
  70. // # Canonicalization
  71. //
  72. // If one uses the Matcher to compare languages one does not need to
  73. // worry about canonicalization.
  74. //
  75. // The meaning of a Tag varies per application. The language package
  76. // therefore delays canonicalization and preserves information as much
  77. // as possible. The Matcher, however, will always take into account that
  78. // two different tags may represent the same language.
  79. //
  80. // By default, only legacy and deprecated tags are converted into their
  81. // canonical equivalent. All other information is preserved. This approach makes
  82. // the confidence scores more accurate and allows matchers to distinguish
  83. // between variants that are otherwise lost.
  84. //
  85. // As a consequence, two tags that should be treated as identical according to
  86. // BCP 47 or CLDR, like "en-Latn" and "en", will be represented differently. The
  87. // Matcher handles such distinctions, though, and is aware of the
  88. // equivalence relations. The CanonType type can be used to alter the
  89. // canonicalization form.
  90. //
  91. // # References
  92. //
  93. // BCP 47 - Tags for Identifying Languages http://tools.ietf.org/html/bcp47
  94. package language // import "golang.org/x/text/language"
  95. // TODO: explanation on how to match languages for your own locale-specific
  96. // service.