...
1
2
3
4
5
6
7 package main
8
9
10
11 import (
12 "fmt"
13 "log"
14 "sort"
15 "strings"
16
17 "golang.org/x/text/internal/language"
18 )
19
20
21
22
23
24
25 func (b *builder) writeCompactIndex() {
26
27 m := map[language.Tag]bool{}
28 for _, lang := range b.data.Locales() {
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 tag := language.Make(strings.Replace(lang, "_POSIX", "-u-va-posix", 1))
52 m[tag] = true
53
54 }
55
56
57
58
59
60
61 for _, plurals := range b.supp.Plurals {
62 for _, rules := range plurals.PluralRules {
63 for _, lang := range strings.Split(rules.Locales, " ") {
64 m[language.Make(lang)] = true
65 }
66 }
67 }
68
69 var coreTags []language.CompactCoreInfo
70 var special []string
71
72 for t := range m {
73 if x := t.Extensions(); len(x) != 0 && fmt.Sprint(x) != "[u-va-posix]" {
74 log.Fatalf("Unexpected extension %v in %v", x, t)
75 }
76 if len(t.Variants()) == 0 && len(t.Extensions()) == 0 {
77 cci, ok := language.GetCompactCore(t)
78 if !ok {
79 log.Fatalf("Locale for non-basic language %q", t)
80 }
81 coreTags = append(coreTags, cci)
82 } else {
83 special = append(special, t.String())
84 }
85 }
86
87 w := b.w
88
89 sort.Slice(coreTags, func(i, j int) bool { return coreTags[i] < coreTags[j] })
90 sort.Strings(special)
91
92 w.WriteComment(`
93 NumCompactTags is the number of common tags. The maximum tag is
94 NumCompactTags-1.`)
95 w.WriteConst("NumCompactTags", len(m))
96
97 fmt.Fprintln(w, "const (")
98 for i, t := range coreTags {
99 fmt.Fprintf(w, "%s ID = %d\n", ident(t.Tag().String()), i)
100 }
101 for i, t := range special {
102 fmt.Fprintf(w, "%s ID = %d\n", ident(t), i+len(coreTags))
103 }
104 fmt.Fprintln(w, ")")
105
106 w.WriteVar("coreTags", coreTags)
107
108 w.WriteConst("specialTagsStr", strings.Join(special, " "))
109 }
110
111 func ident(s string) string {
112 return strings.Replace(s, "-", "", -1) + "Index"
113 }
114
View as plain text