cldr.go 3.8 KB

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