You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

130 lines
2.8 KiB

  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 catalog
  5. import (
  6. "sync"
  7. "golang.org/x/text/internal"
  8. "golang.org/x/text/internal/catmsg"
  9. "golang.org/x/text/language"
  10. )
  11. // TODO:
  12. // Dictionary returns a Dictionary that returns the first Message, using the
  13. // given language tag, that matches:
  14. // 1. the last one registered by one of the Set methods
  15. // 2. returned by one of the Loaders
  16. // 3. repeat from 1. using the parent language
  17. // This approach allows messages to be underspecified.
  18. // func (c *Catalog) Dictionary(tag language.Tag) (Dictionary, error) {
  19. // // TODO: verify dictionary exists.
  20. // return &dict{&c.index, tag}, nil
  21. // }
  22. type dict struct {
  23. s *store
  24. tag language.Tag // TODO: make compact tag.
  25. }
  26. func (d *dict) Lookup(key string) (data string, ok bool) {
  27. return d.s.lookup(d.tag, key)
  28. }
  29. func (b *Builder) lookup(tag language.Tag, key string) (data string, ok bool) {
  30. return b.index.lookup(tag, key)
  31. }
  32. func (c *Builder) set(tag language.Tag, key string, s *store, msg ...Message) error {
  33. data, err := catmsg.Compile(tag, &dict{&c.macros, tag}, firstInSequence(msg))
  34. s.mutex.Lock()
  35. defer s.mutex.Unlock()
  36. m := s.index[tag]
  37. if m == nil {
  38. m = msgMap{}
  39. if s.index == nil {
  40. s.index = map[language.Tag]msgMap{}
  41. }
  42. c.matcher = nil
  43. s.index[tag] = m
  44. }
  45. m[key] = data
  46. return err
  47. }
  48. func (c *Builder) Matcher() language.Matcher {
  49. c.index.mutex.RLock()
  50. m := c.matcher
  51. c.index.mutex.RUnlock()
  52. if m != nil {
  53. return m
  54. }
  55. c.index.mutex.Lock()
  56. if c.matcher == nil {
  57. c.matcher = language.NewMatcher(c.unlockedLanguages())
  58. }
  59. m = c.matcher
  60. c.index.mutex.Unlock()
  61. return m
  62. }
  63. type store struct {
  64. mutex sync.RWMutex
  65. index map[language.Tag]msgMap
  66. }
  67. type msgMap map[string]string
  68. func (s *store) lookup(tag language.Tag, key string) (data string, ok bool) {
  69. s.mutex.RLock()
  70. defer s.mutex.RUnlock()
  71. for ; ; tag = tag.Parent() {
  72. if msgs, ok := s.index[tag]; ok {
  73. if msg, ok := msgs[key]; ok {
  74. return msg, true
  75. }
  76. }
  77. if tag == language.Und {
  78. break
  79. }
  80. }
  81. return "", false
  82. }
  83. // Languages returns all languages for which the Catalog contains variants.
  84. func (b *Builder) Languages() []language.Tag {
  85. s := &b.index
  86. s.mutex.RLock()
  87. defer s.mutex.RUnlock()
  88. return b.unlockedLanguages()
  89. }
  90. func (b *Builder) unlockedLanguages() []language.Tag {
  91. s := &b.index
  92. if len(s.index) == 0 {
  93. return nil
  94. }
  95. tags := make([]language.Tag, 0, len(s.index))
  96. _, hasFallback := s.index[b.options.fallback]
  97. offset := 0
  98. if hasFallback {
  99. tags = append(tags, b.options.fallback)
  100. offset = 1
  101. }
  102. for t := range s.index {
  103. if t != b.options.fallback {
  104. tags = append(tags, t)
  105. }
  106. }
  107. internal.SortTags(tags[offset:])
  108. return tags
  109. }