-
Notifications
You must be signed in to change notification settings - Fork 3
/
i18n.go
195 lines (181 loc) · 3.73 KB
/
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
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
package i18n
import (
"bytes"
"encoding/base64"
"fmt"
"github.com/snail007/gmct/util"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"strings"
"time"
"github.com/snail007/gmct/tool"
)
type I18nArgs struct {
Dir *string
GoFilenamePrefix string
GoFilename string
Clean *bool
Extension *string
SubName *string
}
func NewI18nArgs() I18nArgs {
in := I18nArgs{
Dir: new(string),
Extension: new(string),
Clean: new(bool),
SubName: new(string),
}
*in.Extension = ".toml"
return in
}
type I18n struct {
tool.GMCTool
args I18nArgs
}
func NewI18n() *I18n {
return &I18n{}
}
func (s *I18n) init(args0 interface{}) (err error) {
s.args = args0.(I18nArgs)
if *s.args.Dir == "" {
return fmt.Errorf("i18n directory not exists")
}
//convert pack path to absoulte path of *nix style
*s.args.Dir, err = filepath.Abs(*s.args.Dir)
if err != nil {
return
}
*s.args.Dir = strings.Replace(*s.args.Dir, "\\", "/", -1)
*s.args.Dir = filepath.Join(*s.args.Dir, "/")
if *s.args.Extension == "" {
return fmt.Errorf("extension of i18n file required")
}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
s.args.GoFilenamePrefix = "gmc_i18n_bindata_"
s.args.GoFilename = s.args.GoFilenamePrefix + fmt.Sprintf("%d", r.Int63()) + ".go"
return
}
func (s *I18n) Start(args interface{}) (err error) {
err = s.init(args)
if err != nil {
return
}
if *s.args.Clean {
s.clean()
} else {
s.clean()
err = s.pack()
}
return
}
func (s *I18n) Stop() {
return
}
func (s *I18n) pack() (err error) {
names := []string{}
err = s.tree(*s.args.Dir, &names)
if err != nil {
return
}
i18nFilenames := []string{}
for _, v := range names {
filename := filepath.Base(v)
if strings.HasSuffix(filename, *s.args.Extension) {
i18nFilenames = append(i18nFilenames, v)
}
}
var buf bytes.Buffer
for _, v := range i18nFilenames {
b, err := ioutil.ReadFile(filepath.Join(*s.args.Dir, v))
if err != nil {
fmt.Println(err)
os.Exit(1)
}
str := base64.StdEncoding.EncodeToString(b)
buf.WriteString(fmt.Sprintf("\t\t\"%s\" : \"%s\",\n", strings.TrimSuffix(v, *s.args.Extension), str))
}
currentDir, _ := filepath.Abs(".")
packageName := util.GetPackageName(currentDir)
i18n := fmt.Sprintf(`package %s
import gmci18n "github.com/snail007/gmc/i18n"
func init(){
gmci18n.SetBinData(map[string]string{
{{HOLDER}}
})
}
`, packageName)
i18n = strings.Replace(i18n, "{{HOLDER}}", buf.String(), 1)
ioutil.WriteFile(s.args.GoFilename, []byte(i18n), 0755)
return
}
func (s *I18n) clean() {
files, err := filepath.Glob(s.args.GoFilenamePrefix + "*.go")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
for _, v := range files {
filename := filepath.Base(v)
if strings.HasPrefix(filename, s.args.GoFilenamePrefix) &&
strings.HasSuffix(filename, ".go") {
err = os.Remove(v)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
}
return
}
func (s *I18n) tree(folder string, names *[]string) (err error) {
f, err := os.Open(folder)
if err != nil {
return
}
defer f.Close()
finfo, err := f.Stat()
if err != nil {
return
}
if !finfo.IsDir() {
return
}
files, err := filepath.Glob(folder + "/*")
if err != nil {
return
}
var file *os.File
for _, v := range files {
if file != nil {
file.Close()
}
file, err = os.Open(v)
if err != nil {
return
}
fileInfo, err := file.Stat()
if err != nil {
return err
}
if fileInfo.IsDir() {
err = s.tree(v, names)
if err != nil {
return err
}
} else {
v, err = filepath.Abs(v)
if err != nil {
return err
}
v0 := strings.Replace(v, "\\", "/", -1)
v0 = strings.Replace(v0, *s.args.Dir+"/", "", -1)
*names = append(*names, v0)
}
}
if file != nil {
file.Close()
}
return
}