-
Notifications
You must be signed in to change notification settings - Fork 8
/
make_plurals.go
72 lines (66 loc) · 1.42 KB
/
make_plurals.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
// +build ignore
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"go/format"
"gopkg.in/yaml.v2"
)
// download github.com/vube/i18n first
func main() {
rules := filepath.Join(strings.Split(os.Getenv("GOPATH"), ":")[0], "src/github.com/vube/i18n/data/rules")
err := filepath.Walk(rules, func(path string, info os.FileInfo, err error) error {
if err != nil {
panic(err)
}
if info.IsDir() {
return nil
}
in, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
var out yaml.MapSlice
if err = yaml.Unmarshal(in, &out); err != nil {
panic(err)
}
var plural string
for _, item := range out {
if item.Key == "plural" {
plural = item.Value.(string)
}
}
if plural == "" {
fmt.Println("can't find plurals in", path)
return nil
}
locale := strings.Replace(info.Name(), filepath.Ext(info.Name()), "", 1)
locale = strings.Replace(locale, "-", "_", -1)
dir := "resources/locales/" + locale
if _, err := os.Stat(dir); err != nil {
fmt.Println(dir, "doesn't exist")
return nil
}
file, err := os.Create(dir + "/plural.go")
if err != nil {
panic(err)
}
rawcodes := fmt.Sprintf(`package %s
var pluralRule = %q
`, locale, plural)
var codes []byte
if codes, err = format.Source([]byte(rawcodes)); err != nil {
panic(err)
}
if _, err := file.Write(codes); err != nil {
panic(err)
}
return nil
})
if err != nil {
panic(err)
}
}