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