-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
229 lines (200 loc) · 5.38 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
)
// Package contains the details about the source cldr
type Package struct {
Name string `json:"name"`
Version string `json:"version"`
PeerDependencies struct {
CldrCore string `json:"cldr-core"`
} `json:"peerDependencies"`
Homepage string `json:"homepage"`
Author string `json:"author"`
Maintainers []struct {
Name string `json:"name"`
Email string `json:"email"`
URL string `json:"url"`
} `json:"maintainers"`
Repository struct {
Type string `json:"type"`
URL string `json:"url"`
} `json:"repository"`
Licenses []struct {
Type string `json:"type"`
URL string `json:"url"`
} `json:"licenses"`
Bugs string `json:"bugs"`
}
// Symbols contains the infor we want to extract for each locale
type Symbols struct {
Decimal string `json:"decimal,omitempty"`
Group string `json:"group,omitempty"`
PercentSign string `json:"percentSign,omitempty"`
}
// Locale has the details of the settings
type Locale struct {
Identity struct {
Version struct {
CldrVersion string `json:"_cldrVersion"`
} `json:"version"`
} `json:"identity"`
Numbers struct {
MinimumGroupingDigits string `json:"minimumGroupingDigits"`
Symbols Symbols `json:"symbols-numberSystem-latn"`
DecimalFormats struct {
Standard string `json:"standard"`
} `json:"decimalFormats-numberSystem-latn"`
ScientificFormats struct {
Standard string `json:"standard"`
} `json:"scientificFormats-numberSystem-latn"`
PercentFormats struct {
Standard string `json:"standard"`
} `json:"percentFormats-numberSystem-latn"`
} `json:"numbers"`
}
// Numbers holds the info we want to generate a single file with details about each locale
type Numbers struct {
Main map[string]Locale `json:"main"`
}
// Result is the format that is written to disk
type Result struct {
Package Package `json:"package"`
Symbols map[string]Symbols `json:"symbols"`
}
func main() {
dir := flag.String("dir", "cldr-numbers-full", "Directory when cldr-numbers-full is located")
type Defaults struct {
Decimal string
Group string
PercentSign string
}
defaults := Defaults{
Decimal: ".",
Group: ",",
PercentSign: "%",
}
flag.Parse()
full := Result{}
full.Symbols = map[string]Symbols{}
condensed := Result{}
condensed.Symbols = map[string]Symbols{}
err := filepath.Walk(*dir, func(filePath string, file os.FileInfo, err error) error {
if file != nil {
if file.Name() == "numbers.json" {
bytes, err := ioutil.ReadFile(filePath)
if err != nil {
fmt.Printf("Unable to read file %s: %s", filePath, err)
}
numbers := &Numbers{}
err = json.Unmarshal(bytes, numbers)
if err != nil {
fmt.Printf("Unable to parse file %s: %s", filePath, err)
}
for key, val := range numbers.Main {
full.Symbols[key] = val.Numbers.Symbols
count := 0
if val.Numbers.Symbols.Decimal == defaults.Decimal {
val.Numbers.Symbols.Decimal = ""
count++
}
if val.Numbers.Symbols.Group == defaults.Group {
val.Numbers.Symbols.Group = ""
count++
}
if val.Numbers.Symbols.PercentSign == defaults.PercentSign {
val.Numbers.Symbols.PercentSign = ""
count++
}
if count != 3 {
// Don't add if everything was omited
condensed.Symbols[key] = val.Numbers.Symbols
}
}
} else if file.Name() == "package.json" {
bytes, err := ioutil.ReadFile(filePath)
if err != nil {
fmt.Printf("Unable to read file %s: %s", filePath, err)
}
pack := &Package{}
err = json.Unmarshal(bytes, pack)
if err != nil {
fmt.Printf("Unable to parse file %s: %s", filePath, err)
}
full.Package = *pack
condensed.Package = *pack
}
}
return nil
})
if err != nil {
fmt.Printf("Unable to iterate file: %s", err)
return
}
if len(full.Symbols) == 0 {
fmt.Print("No locales found, check path to cldr-numbers-full")
return
}
err = writeToFile("cldr-numbers", full, false)
if err != nil {
fmt.Print(err)
return
}
err = writeToFile("cldr-numbers-condensed", condensed, false)
if err != nil {
fmt.Print(err)
return
}
err = writeToFile("cldr-numbers", full, true)
if err != nil {
fmt.Print(err)
return
}
err = writeToFile("cldr-numbers-condensed", condensed, true)
if err != nil {
fmt.Print(err)
return
}
fmt.Println("Complete")
}
func writeToFile(name string, result Result, javaScript bool) error {
if _, err := os.Stat("dist"); os.IsNotExist(err) {
os.Mkdir("dist", 0644)
}
extension := "json"
if javaScript {
extension = "js"
}
pretty, err := json.MarshalIndent(result, "", " ")
if err != nil {
return fmt.Errorf("Unable to marshal file: %s", err)
}
min, err := json.Marshal(result)
if err != nil {
return fmt.Errorf("Unable to marshal file: %s", err)
}
if javaScript {
re := regexp.MustCompile(`("([A-Za-z_]+)"):`)
sPretty := string(pretty)
sPretty = re.ReplaceAllString(sPretty, `$2:`)
pretty = []byte(sPretty)
sMin := string(min)
sMin = re.ReplaceAllString(sMin, `$2:`)
min = []byte(sMin)
}
err = ioutil.WriteFile("dist/"+name+"."+extension, pretty, 0644)
if err != nil {
return fmt.Errorf("Unable to write file: %s", err)
}
err = ioutil.WriteFile("dist/"+name+".min."+extension, min, 0644)
if err != nil {
return fmt.Errorf("Unable to write file: %s", err)
}
return nil
}