forked from bradrydzewski/togo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction_i18n.go
90 lines (79 loc) · 1.44 KB
/
action_i18n.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
package main
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/urfave/cli"
"github.com/bradrydzewski/togo/template"
)
type (
i18nParams struct {
Package string
Files []*i18nFile
}
i18nFile struct {
Base string
Name string
Path string
Ext string
Data string
}
)
var i18nCommand = cli.Command{
Name: "i18n",
Usage: "embed i18n files",
Action: i18nAction,
Flags: []cli.Flag{
cli.StringFlag{
Name: "package",
Value: "i18n",
},
cli.StringFlag{
Name: "input",
Value: "files/*.all.json",
},
cli.StringFlag{
Name: "output",
Value: "i18n_gen.go",
},
cli.BoolFlag{
Name: "encode",
},
},
}
func i18nAction(c *cli.Context) error {
pattern := c.Args().First()
if pattern == "" {
pattern = c.String("input")
}
matches, err := filepath.Glob(pattern)
if err != nil {
return err
}
params := i18nParams{
Package: c.String("package"),
}
for _, match := range matches {
raw, ioerr := ioutil.ReadFile(match)
if ioerr != nil {
return ioerr
}
params.Files = append(params.Files, &i18nFile{
Path: match,
Name: filepath.Base(match),
Base: strings.TrimSuffix(filepath.Base(match), filepath.Ext(match)),
Ext: filepath.Ext(match),
Data: string(raw),
})
}
wr := os.Stdout
if output := c.String("output"); output != "" {
wr, err = os.Create(output)
if err != nil {
return err
}
defer wr.Close()
}
return template.Execute(wr, "i18n.tmpl", params)
}