1 // Copyright 2015 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.
9 // This file generates derivative tables based on the language package itself.
21 "golang.org/x/text/internal/gen"
22 "golang.org/x/text/language"
23 "golang.org/x/text/unicode/cldr"
27 test = flag.Bool("test", false,
28 "test existing tables; can be used to compare web data with package data.")
30 draft = flag.String("draft",
32 `Minimal draft requirements (approved, contributed, provisional, unconfirmed).`)
38 // Read the CLDR zip file.
39 r := gen.OpenCLDRCoreZip()
43 data, err := d.DecodeZip(r)
45 log.Fatalf("DecodeZip: %v", err)
48 w := gen.NewCodeWriter()
50 buf := &bytes.Buffer{}
52 if _, err = w.WriteGo(buf, "language"); err != nil {
53 log.Fatalf("Error formatting file index.go: %v", err)
56 // Since we're generating a table for our own package we need to rewrite
57 // doing the equivalent of go fmt -r 'language.b -> b'. Using
58 // bytes.Replace will do.
59 out := bytes.Replace(buf.Bytes(), []byte("language."), nil, -1)
60 if err := ioutil.WriteFile("index.go", out, 0600); err != nil {
61 log.Fatalf("Could not create file index.go: %v", err)
65 m := map[language.Tag]bool{}
66 for _, lang := range data.Locales() {
67 // We include all locales unconditionally to be consistent with en_US.
68 // We want en_US, even though it has no data associated with it.
70 // TODO: put any of the languages for which no data exists at the end
71 // of the index. This allows all components based on ICU to use that
72 // as the cutoff point.
73 // if x := data.RawLDML(lang); false ||
74 // x.LocaleDisplayNames != nil ||
75 // x.Characters != nil ||
76 // x.Delimiters != nil ||
77 // x.Measurement != nil ||
79 // x.Numbers != nil ||
81 // x.ListPatterns != nil ||
82 // x.Collations != nil ||
83 // x.Segmentations != nil ||
85 // x.Annotations != nil ||
86 // x.Metadata != nil {
88 // TODO: support POSIX natively, albeit non-standard.
89 tag := language.Make(strings.Replace(lang, "_POSIX", "-u-va-posix", 1))
93 // Include locales for plural rules, which uses a different structure.
94 for _, plurals := range data.Supplemental().Plurals {
95 for _, rules := range plurals.PluralRules {
96 for _, lang := range strings.Split(rules.Locales, " ") {
97 m[language.Make(lang)] = true
102 var core, special []language.Tag
105 if x := t.Extensions(); len(x) != 0 && fmt.Sprint(x) != "[u-va-posix]" {
106 log.Fatalf("Unexpected extension %v in %v", x, t)
108 if len(t.Variants()) == 0 && len(t.Extensions()) == 0 {
109 core = append(core, t)
111 special = append(special, t)
116 NumCompactTags is the number of common tags. The maximum tag is
118 w.WriteConst("NumCompactTags", len(core)+len(special))
120 sort.Sort(byAlpha(special))
121 w.WriteVar("specialTags", special)
123 // TODO: order by frequency?
124 sort.Sort(byAlpha(core))
126 // Size computations are just an estimate.
127 w.Size += int(reflect.TypeOf(map[uint32]uint16{}).Size())
128 w.Size += len(core) * 6 // size of uint32 and uint16
131 fmt.Fprintln(w, "var coreTags = map[uint32]uint16{")
132 fmt.Fprintln(w, "0x0: 0, // und")
133 i := len(special) + 1 // Und and special tags already written.
134 for _, t := range core {
135 if t == language.Und {
138 fmt.Fprint(w.Hash, t, i)
140 fmt.Fprintf(w, "0x%s%s%s: %d, // %s\n",
141 getIndex(b, 3), // 3 is enough as it is guaranteed to be a compact number
150 // getIndex prints the subtag type and extracts its index of size nibble.
151 // If the index is less than n nibbles, the result is prefixed with 0s.
152 func getIndex(x interface{}, n int) string {
153 s := fmt.Sprintf("%#v", x) // s is of form Type{typeID: 0x00}
154 s = s[strings.Index(s, "0x")+2 : len(s)-1]
155 return strings.Repeat("0", n-len(s)) + s
158 type byAlpha []language.Tag
160 func (a byAlpha) Len() int { return len(a) }
161 func (a byAlpha) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
162 func (a byAlpha) Less(i, j int) bool { return a[i].String() < a[j].String() }