cldr.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright 2013 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. //go:generate go run makexml.go -output xml.go
  5. // Package cldr provides a parser for LDML and related XML formats.
  6. //
  7. // This package is intended to be used by the table generation tools for the
  8. // various packages in x/text and is not internal for historical reasons.
  9. //
  10. // As the XML types are generated from the CLDR DTD, and as the CLDR standard is
  11. // periodically amended, this package may change considerably over time. This
  12. // mostly means that data may appear and disappear between versions. That is,
  13. // old code should keep compiling for newer versions, but data may have moved or
  14. // changed. CLDR version 22 is the first version supported by this package.
  15. // Older versions may not work.
  16. package cldr // import "golang.org/x/text/unicode/cldr"
  17. import (
  18. "fmt"
  19. "sort"
  20. )
  21. // CLDR provides access to parsed data of the Unicode Common Locale Data Repository.
  22. type CLDR struct {
  23. parent map[string][]string
  24. locale map[string]*LDML
  25. resolved map[string]*LDML
  26. bcp47 *LDMLBCP47
  27. supp *SupplementalData
  28. }
  29. func makeCLDR() *CLDR {
  30. return &CLDR{
  31. parent: make(map[string][]string),
  32. locale: make(map[string]*LDML),
  33. resolved: make(map[string]*LDML),
  34. bcp47: &LDMLBCP47{},
  35. supp: &SupplementalData{},
  36. }
  37. }
  38. // BCP47 returns the parsed BCP47 LDML data. If no such data was parsed, nil is returned.
  39. func (cldr *CLDR) BCP47() *LDMLBCP47 {
  40. return nil
  41. }
  42. // Draft indicates the draft level of an element.
  43. type Draft int
  44. const (
  45. Approved Draft = iota
  46. Contributed
  47. Provisional
  48. Unconfirmed
  49. )
  50. var drafts = []string{"unconfirmed", "provisional", "contributed", "approved", ""}
  51. // ParseDraft returns the Draft value corresponding to the given string. The
  52. // empty string corresponds to Approved.
  53. func ParseDraft(level string) (Draft, error) {
  54. if level == "" {
  55. return Approved, nil
  56. }
  57. for i, s := range drafts {
  58. if level == s {
  59. return Unconfirmed - Draft(i), nil
  60. }
  61. }
  62. return Approved, fmt.Errorf("cldr: unknown draft level %q", level)
  63. }
  64. func (d Draft) String() string {
  65. return drafts[len(drafts)-1-int(d)]
  66. }
  67. // SetDraftLevel sets which draft levels to include in the evaluated LDML.
  68. // Any draft element for which the draft level is higher than lev will be excluded.
  69. // If multiple draft levels are available for a single element, the one with the
  70. // lowest draft level will be selected, unless preferDraft is true, in which case
  71. // the highest draft will be chosen.
  72. // It is assumed that the underlying LDML is canonicalized.
  73. func (cldr *CLDR) SetDraftLevel(lev Draft, preferDraft bool) {
  74. // TODO: implement
  75. cldr.resolved = make(map[string]*LDML)
  76. }
  77. // RawLDML returns the LDML XML for id in unresolved form.
  78. // id must be one of the strings returned by Locales.
  79. func (cldr *CLDR) RawLDML(loc string) *LDML {
  80. return cldr.locale[loc]
  81. }
  82. // LDML returns the fully resolved LDML XML for loc, which must be one of
  83. // the strings returned by Locales.
  84. //
  85. // Deprecated: Use RawLDML and implement inheritance manually or using the
  86. // internal cldrtree package.
  87. // Inheritance has changed quite a bit since the onset of this package and in
  88. // practice data often represented in a way where knowledge of how it was
  89. // inherited is relevant.
  90. func (cldr *CLDR) LDML(loc string) (*LDML, error) {
  91. return cldr.resolve(loc)
  92. }
  93. // Supplemental returns the parsed supplemental data. If no such data was parsed,
  94. // nil is returned.
  95. func (cldr *CLDR) Supplemental() *SupplementalData {
  96. return cldr.supp
  97. }
  98. // Locales returns the locales for which there exist files.
  99. // Valid sublocales for which there is no file are not included.
  100. // The root locale is always sorted first.
  101. func (cldr *CLDR) Locales() []string {
  102. loc := []string{"root"}
  103. hasRoot := false
  104. for l, _ := range cldr.locale {
  105. if l == "root" {
  106. hasRoot = true
  107. continue
  108. }
  109. loc = append(loc, l)
  110. }
  111. sort.Strings(loc[1:])
  112. if !hasRoot {
  113. return loc[1:]
  114. }
  115. return loc
  116. }
  117. // Get fills in the fields of x based on the XPath path.
  118. func Get(e Elem, path string) (res Elem, err error) {
  119. return walkXPath(e, path)
  120. }