| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/xml" |
| 5 |
"flag" |
| 6 |
"fmt" |
| 7 |
"os" |
| 8 |
"text/template" |
| 9 |
) |
| 10 |
|
| 11 |
var usage = `%[1]s generates Go code to support CLDR plural rules. |
| 12 |
|
| 13 |
Usage: %[1]s [options] |
| 14 |
|
| 15 |
Options: |
| 16 |
|
| 17 |
` |
| 18 |
|
| 19 |
func main() { |
| 20 |
flag.Usage = func() { |
| 21 |
fmt.Fprintf(os.Stderr, usage, os.Args[0]) |
| 22 |
flag.PrintDefaults() |
| 23 |
} |
| 24 |
var in, cout, tout string |
| 25 |
flag.StringVar(&in, "i", "plurals.xml", "the input XML file containing CLDR plural rules") |
| 26 |
flag.StringVar(&cout, "cout", "", "the code output file") |
| 27 |
flag.StringVar(&tout, "tout", "", "the test output file") |
| 28 |
flag.BoolVar(&verbose, "v", false, "verbose output") |
| 29 |
flag.Parse() |
| 30 |
|
| 31 |
buf, err := os.ReadFile(in) |
| 32 |
if err != nil { |
| 33 |
fatalf("failed to read file: %s", err) |
| 34 |
} |
| 35 |
|
| 36 |
var data SupplementalData |
| 37 |
if err := xml.Unmarshal(buf, &data); err != nil { |
| 38 |
fatalf("failed to unmarshal xml: %s", err) |
| 39 |
} |
| 40 |
|
| 41 |
count := 0 |
| 42 |
for _, pg := range data.PluralGroups { |
| 43 |
count += len(pg.SplitLocales()) |
| 44 |
} |
| 45 |
infof("parsed %d locales", count) |
| 46 |
|
| 47 |
if cout != "" { |
| 48 |
file := openWritableFile(cout) |
| 49 |
if err := codeTemplate.Execute(file, data); err != nil { |
| 50 |
fatalf("unable to execute code template because %s", err) |
| 51 |
} else { |
| 52 |
infof("generated %s", cout) |
| 53 |
} |
| 54 |
} else { |
| 55 |
infof("not generating code file (use -cout)") |
| 56 |
} |
| 57 |
|
| 58 |
if tout != "" { |
| 59 |
file := openWritableFile(tout) |
| 60 |
if err := testTemplate.Execute(file, data); err != nil { |
| 61 |
fatalf("unable to execute test template because %s", err) |
| 62 |
} else { |
| 63 |
infof("generated %s", tout) |
| 64 |
} |
| 65 |
} else { |
| 66 |
infof("not generating test file (use -tout)") |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
func openWritableFile(name string) *os.File { |
| 71 |
file, err := os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) |
| 72 |
if err != nil { |
| 73 |
fatalf("failed to write file %s because %s", name, err) |
| 74 |
} |
| 75 |
return file |
| 76 |
} |
| 77 |
|
| 78 |
var codeTemplate = template.Must(template.New("rule").Parse(`// This file is generated by i18n/plural/codegen/generate.sh; DO NOT EDIT |
| 79 |
|
| 80 |
package plural |
| 81 |
|
| 82 |
// DefaultRules returns a map of Rules generated from CLDR language data. |
| 83 |
func DefaultRules() Rules { |
| 84 |
rules := Rules{} |
| 85 |
|
| 86 |
{{range .PluralGroups}} |
| 87 |
addPluralRules(rules, {{printf "%#v" .SplitLocales}}, &Rule{ |
| 88 |
PluralForms: newPluralFormSet({{range $i, $e := .PluralRules}}{{if $i}}, {{end}}{{$e.CountTitle}}{{end}}), |
| 89 |
PluralFormFunc: func(ops *Operands) Form { {{range .PluralRules}}{{if .GoCondition}} |
| 90 |
// {{.Condition}} |
| 91 |
if {{.GoCondition}} { |
| 92 |
return {{.CountTitle}} |
| 93 |
}{{end}}{{end}} |
| 94 |
return Other |
| 95 |
}, |
| 96 |
}){{end}} |
| 97 |
|
| 98 |
return rules |
| 99 |
} |
| 100 |
`)) |
| 101 |
|
| 102 |
var testTemplate = template.Must(template.New("rule").Parse(`// This file is generated by i18n/plural/codegen/generate.sh; DO NOT EDIT |
| 103 |
|
| 104 |
package plural |
| 105 |
|
| 106 |
import "testing" |
| 107 |
|
| 108 |
{{range .PluralGroups}} |
| 109 |
func Test{{.Name}}(t *testing.T) { |
| 110 |
var tests []pluralFormTest |
| 111 |
{{range .PluralRules}} |
| 112 |
{{if .IntegerExamples}}tests = appendIntegerTests(tests, {{.CountTitle}}, {{printf "%#v" .IntegerExamples}}){{end}} |
| 113 |
{{if .DecimalExamples}}tests = appendDecimalTests(tests, {{.CountTitle}}, {{printf "%#v" .DecimalExamples}}){{end}} |
| 114 |
{{end}} |
| 115 |
locales := {{printf "%#v" .SplitLocales}} |
| 116 |
for _, locale := range locales { |
| 117 |
runTests(t, locale, tests) |
| 118 |
} |
| 119 |
} |
| 120 |
{{end}} |
| 121 |
`)) |
| 122 |
|
| 123 |
func infof(format string, args ...interface{}) { |
| 124 |
fmt.Fprintf(os.Stderr, format+"\n", args...) |
| 125 |
} |
| 126 |
|
| 127 |
var verbose bool |
| 128 |
|
| 129 |
func fatalf(format string, args ...interface{}) { |
| 130 |
infof("fatal: "+format+"\n", args...) |
| 131 |
os.Exit(1) |
| 132 |
} |