khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 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 | |
| 5 | //go:generate go run makexml.go -output xml.go |
| 6 | |
| 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" |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "sort" |
| 22 | ) |
| 23 | |
| 24 | // CLDR provides access to parsed data of the Unicode Common Locale Data Repository. |
| 25 | type CLDR struct { |
| 26 | parent map[string][]string |
| 27 | locale map[string]*LDML |
| 28 | resolved map[string]*LDML |
| 29 | bcp47 *LDMLBCP47 |
| 30 | supp *SupplementalData |
| 31 | } |
| 32 | |
| 33 | func makeCLDR() *CLDR { |
| 34 | return &CLDR{ |
| 35 | parent: make(map[string][]string), |
| 36 | locale: make(map[string]*LDML), |
| 37 | resolved: make(map[string]*LDML), |
| 38 | bcp47: &LDMLBCP47{}, |
| 39 | supp: &SupplementalData{}, |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // BCP47 returns the parsed BCP47 LDML data. If no such data was parsed, nil is returned. |
| 44 | func (cldr *CLDR) BCP47() *LDMLBCP47 { |
| 45 | return nil |
| 46 | } |
| 47 | |
| 48 | // Draft indicates the draft level of an element. |
| 49 | type Draft int |
| 50 | |
| 51 | const ( |
| 52 | Approved Draft = iota |
| 53 | Contributed |
| 54 | Provisional |
| 55 | Unconfirmed |
| 56 | ) |
| 57 | |
| 58 | var drafts = []string{"unconfirmed", "provisional", "contributed", "approved", ""} |
| 59 | |
| 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) { |
| 63 | if level == "" { |
| 64 | return Approved, nil |
| 65 | } |
| 66 | for i, s := range drafts { |
| 67 | if level == s { |
| 68 | return Unconfirmed - Draft(i), nil |
| 69 | } |
| 70 | } |
| 71 | return Approved, fmt.Errorf("cldr: unknown draft level %q", level) |
| 72 | } |
| 73 | |
| 74 | func (d Draft) String() string { |
| 75 | return drafts[len(drafts)-1-int(d)] |
| 76 | } |
| 77 | |
| 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) { |
| 85 | // TODO: implement |
| 86 | cldr.resolved = make(map[string]*LDML) |
| 87 | } |
| 88 | |
| 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] |
| 93 | } |
| 94 | |
| 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) |
| 99 | } |
| 100 | |
| 101 | // Supplemental returns the parsed supplemental data. If no such data was parsed, |
| 102 | // nil is returned. |
| 103 | func (cldr *CLDR) Supplemental() *SupplementalData { |
| 104 | return cldr.supp |
| 105 | } |
| 106 | |
| 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"} |
| 112 | hasRoot := false |
| 113 | for l, _ := range cldr.locale { |
| 114 | if l == "root" { |
| 115 | hasRoot = true |
| 116 | continue |
| 117 | } |
| 118 | loc = append(loc, l) |
| 119 | } |
| 120 | sort.Strings(loc[1:]) |
| 121 | if !hasRoot { |
| 122 | return loc[1:] |
| 123 | } |
| 124 | return loc |
| 125 | } |
| 126 | |
| 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) |
| 130 | } |