match.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2015 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 internal
  5. // This file contains matchers that implement CLDR inheritance.
  6. //
  7. // See https://unicode.org/reports/tr35/#Locale_Inheritance.
  8. //
  9. // Some of the inheritance described in this document is already handled by
  10. // the cldr package.
  11. import (
  12. "golang.org/x/text/language"
  13. )
  14. // TODO: consider if (some of the) matching algorithm needs to be public after
  15. // getting some feel about what is generic and what is specific.
  16. // NewInheritanceMatcher returns a matcher that matches based on the inheritance
  17. // chain.
  18. //
  19. // The matcher uses canonicalization and the parent relationship to find a
  20. // match. The resulting match will always be either Und or a language with the
  21. // same language and script as the requested language. It will not match
  22. // languages for which there is understood to be mutual or one-directional
  23. // intelligibility.
  24. //
  25. // A Match will indicate an Exact match if the language matches after
  26. // canonicalization and High if the matched tag is a parent.
  27. func NewInheritanceMatcher(t []language.Tag) *InheritanceMatcher {
  28. tags := &InheritanceMatcher{make(map[language.Tag]int)}
  29. for i, tag := range t {
  30. ct, err := language.All.Canonicalize(tag)
  31. if err != nil {
  32. ct = tag
  33. }
  34. tags.index[ct] = i
  35. }
  36. return tags
  37. }
  38. type InheritanceMatcher struct {
  39. index map[language.Tag]int
  40. }
  41. func (m InheritanceMatcher) Match(want ...language.Tag) (language.Tag, int, language.Confidence) {
  42. for _, t := range want {
  43. ct, err := language.All.Canonicalize(t)
  44. if err != nil {
  45. ct = t
  46. }
  47. conf := language.Exact
  48. for {
  49. if index, ok := m.index[ct]; ok {
  50. return ct, index, conf
  51. }
  52. if ct == language.Und {
  53. break
  54. }
  55. ct = ct.Parent()
  56. conf = language.High
  57. }
  58. }
  59. return language.Und, 0, language.No
  60. }