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