forked from maximilien/i18n4go
-
Notifications
You must be signed in to change notification settings - Fork 3
/
common.go
362 lines (293 loc) · 9 KB
/
common.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package common
import (
"bytes"
"errors"
"fmt"
"os"
"regexp"
"io/ioutil"
"strconv"
"strings"
"path/filepath"
"encoding/json"
)
const (
TEMPLATED_STRING_REGEXP = `\{\{\.[[:alnum:][:punct:][:print:]]+?\}\}`
INTERPOLATED_STRING_REGEXP = `%(?:[#v]|[%EGUTXbcdefgopqstvx])`
)
var templatedStringRegexp, interpolatedStringRegexp *regexp.Regexp
func ParseStringList(stringList string, delimiter string) []string {
stringArray := strings.Split(stringList, delimiter)
var parsedStrings []string
for _, aString := range stringArray {
if aString != "" {
parsedStrings = append(parsedStrings, strings.Trim(strings.Trim(aString, " "), "\""))
}
}
return parsedStrings
}
func CreateTmpFile(content string) (*os.File, error) {
tmpFile, err := ioutil.TempFile("", "")
if err != nil {
return nil, err
}
ioutil.WriteFile(tmpFile.Name(), []byte(content), 0666)
return tmpFile, nil
}
func CheckFile(fileName string) (string, string, error) {
fileInfo, err := os.Stat(fileName)
if err != nil {
return "", "", err
}
if !fileInfo.Mode().IsRegular() {
return "", "", fmt.Errorf("i18n4go: Non-regular source file %s (%s)\n", fileInfo.Name(), fileInfo.Mode().String())
}
return filepath.Base(fileName), filepath.Dir(fileName), nil
}
func CopyFileContents(src, dst string) error {
err := CreateOutputDirsIfNeeded(filepath.Dir(dst))
if err != nil {
return err
}
byteArray, err := ioutil.ReadFile(src)
if err != nil {
return err
}
return ioutil.WriteFile(dst, byteArray, 0644)
}
func GetAbsFileInfo(fileNamePath string) (os.FileInfo, error) {
var absFilePath = fileNamePath
if !filepath.IsAbs(absFilePath) {
absFilePath = filepath.Join(os.Getenv("PWD"), absFilePath)
}
file, err := os.OpenFile(absFilePath, os.O_RDONLY, 0)
defer file.Close()
if err != nil {
return nil, err
}
return file.Stat()
}
func FindFilePath(filename string) (string, error) {
fileInfo, err := os.Stat(filename)
if err != nil {
return "", err
}
path := filename[0 : len(filename)-len(fileInfo.Name())]
return path, nil
}
func CreateOutputDirsIfNeeded(outputDirname string) error {
_, err := os.Stat(outputDirname)
if os.IsNotExist(err) {
err = os.MkdirAll(outputDirname, 0755)
if err != nil {
return err
}
}
return nil
}
func UnescapeHTML(byteArray []byte) []byte {
byteArray = bytes.Replace(byteArray, []byte("\\u003c"), []byte("<"), -1)
byteArray = bytes.Replace(byteArray, []byte("\\u003e"), []byte(">"), -1)
byteArray = bytes.Replace(byteArray, []byte("\\u0026"), []byte("&"), -1)
return byteArray
}
func SaveStrings(printer PrinterInterface, options Options, stringInfos map[string]StringInfo, outputDirname string, fileName string) error {
if !options.DryRunFlag {
err := CreateOutputDirsIfNeeded(outputDirname)
if err != nil {
printer.Println(err)
return err
}
}
i18nStringInfos := make([]I18nStringInfo, len(stringInfos))
i := 0
for _, stringInfo := range stringInfos {
i18nStringInfos[i] = I18nStringInfo{ID: stringInfo.Value, Translation: stringInfo.Value}
i++
}
jsonData, err := json.MarshalIndent(i18nStringInfos, "", " ")
if err != nil {
printer.Println(err)
return err
}
jsonData = UnescapeHTML(jsonData)
outputFilename := filepath.Join(outputDirname, fileName[strings.LastIndex(fileName, string(os.PathSeparator))+1:len(fileName)])
if len(stringInfos) != 0 {
printer.Println("Saving extracted i18n strings to file:", outputFilename)
}
if !options.DryRunFlag && len(i18nStringInfos) != 0 {
file, err := os.Create(outputFilename)
defer file.Close()
if err != nil {
printer.Println(err)
return err
}
file.Write(jsonData)
}
return nil
}
func SaveStringsInPo(printer PrinterInterface, options Options, stringInfos map[string]StringInfo, outputDirname string, fileName string) error {
if len(stringInfos) != 0 {
printer.Println("Creating and saving i18n strings to .po file:", fileName)
}
if !options.DryRunFlag && len(stringInfos) != 0 {
err := CreateOutputDirsIfNeeded(outputDirname)
if err != nil {
printer.Println(err)
return err
}
file, err := os.Create(filepath.Join(outputDirname, fileName[strings.LastIndex(fileName, string(os.PathSeparator))+1:len(fileName)]))
defer file.Close()
if err != nil {
printer.Println(err)
return err
}
for _, stringInfo := range stringInfos {
file.Write([]byte("# filename: " + strings.Split(fileName, ".en.po")[0] +
", offset: " + strconv.Itoa(stringInfo.Offset) +
", line: " + strconv.Itoa(stringInfo.Line) +
", column: " + strconv.Itoa(stringInfo.Column) + "\n"))
file.Write([]byte("msgid " + strconv.Quote(stringInfo.Value) + "\n"))
file.Write([]byte("msgstr " + strconv.Quote(stringInfo.Value) + "\n"))
file.Write([]byte("\n"))
}
}
return nil
}
func SaveI18nStringsInPo(printer PrinterInterface, options Options, i18nStrings []I18nStringInfo, fileName string) error {
printer.Println("i18n4go: creating and saving i18n strings to .po file:", fileName)
if !options.DryRunFlag && len(i18nStrings) != 0 {
file, err := os.Create(fileName)
defer file.Close()
if err != nil {
printer.Println(err)
return err
}
for _, stringInfo := range i18nStrings {
file.Write([]byte("msgid " + strconv.Quote(stringInfo.ID) + "\n"))
file.Write([]byte("msgstr " + strconv.Quote(stringInfo.Translation) + "\n"))
file.Write([]byte("\n"))
}
}
return nil
}
func SaveI18nStringInfos(printer PrinterInterface, options Options, i18nStringInfos []I18nStringInfo, fileName string) error {
jsonData, err := json.MarshalIndent(i18nStringInfos, "", " ")
if err != nil {
printer.Println(err)
return err
}
jsonData = UnescapeHTML(jsonData)
if !options.DryRunFlag && len(i18nStringInfos) != 0 {
err := ioutil.WriteFile(fileName, jsonData, 0644)
if err != nil {
printer.Println(err)
return err
}
}
return nil
}
func LoadI18nStringInfos(fileName string) ([]I18nStringInfo, error) {
_, err := os.Stat(fileName)
if os.IsNotExist(err) {
return nil, err
}
content, err := ioutil.ReadFile(fileName)
if err != nil {
return nil, err
}
var i18nStringInfos []I18nStringInfo
err = json.Unmarshal(content, &i18nStringInfos)
if err != nil {
return nil, err
}
return i18nStringInfos, nil
}
func CreateI18nStringInfoMap(i18nStringInfos []I18nStringInfo) (map[string]I18nStringInfo, error) {
inputMap := make(map[string]I18nStringInfo, len(i18nStringInfos))
for _, i18nStringInfo := range i18nStringInfos {
if _, ok := inputMap[i18nStringInfo.ID]; !ok {
inputMap[i18nStringInfo.ID] = i18nStringInfo
} else {
return nil, errors.New("Duplicated key found: " + i18nStringInfo.ID)
}
}
return inputMap, nil
}
func CopyI18nStringInfoMap(i18nStringInfoMap map[string]I18nStringInfo) map[string]I18nStringInfo {
copyMap := make(map[string]I18nStringInfo, len(i18nStringInfoMap))
for key, value := range i18nStringInfoMap {
copyMap[key] = value
}
return copyMap
}
func GetTemplatedStringArgs(aString string) []string {
re, err := getTemplatedStringRegexp()
if err != nil {
fmt.Printf("i18n4go: Error compiling templated string Regexp: %s\n", err.Error())
return []string{}
}
matches := re.FindAllStringSubmatch(aString, -1)
var stringMatches []string
for _, match := range matches {
stringMatch := match[0]
stringMatch = stringMatch[3 : len(stringMatch)-2]
stringMatches = append(stringMatches, stringMatch)
}
return stringMatches
}
func IsTemplatedString(aString string) bool {
re, err := getTemplatedStringRegexp()
if err != nil {
fmt.Printf("i18n4go: Error compiling templated string Regexp: %s\n", err.Error())
return false
}
return re.Match([]byte(aString))
}
func IsInterpolatedString(aString string) bool {
re, err := getInterpolatedStringRegexp()
if err != nil {
fmt.Printf("i18n4go: Error compiling interpolated string Regexp: %s\n", err.Error())
return false
}
return re.Match([]byte(aString))
}
func ConvertToTemplatedString(aString string) string {
if !IsInterpolatedString(aString) {
return aString
}
re, err := getInterpolatedStringRegexp()
if err != nil {
fmt.Printf("i18n4go: Error compiling interpolated string Regexp: %s\n", err.Error())
return ""
}
matches := re.FindAllStringSubmatch(aString, -1)
templatedString := aString
for i, match := range matches {
argName := "{{.Arg" + strconv.Itoa(i) + "}}"
templatedString = strings.Replace(templatedString, match[0], argName, 1)
}
return templatedString
}
func I18nStringInfoMapValues2Array(i18nStringInfosMap map[string]I18nStringInfo) []I18nStringInfo {
var i18nStringInfos []I18nStringInfo
for _, i18nStringInfo := range i18nStringInfosMap {
i18nStringInfos = append(i18nStringInfos, i18nStringInfo)
}
return i18nStringInfos
}
// Private
func getTemplatedStringRegexp() (*regexp.Regexp, error) {
var err error
if templatedStringRegexp == nil {
templatedStringRegexp, err = regexp.Compile(TEMPLATED_STRING_REGEXP)
}
return templatedStringRegexp, err
}
func getInterpolatedStringRegexp() (*regexp.Regexp, error) {
var err error
if interpolatedStringRegexp == nil {
interpolatedStringRegexp, err = regexp.Compile(INTERPOLATED_STRING_REGEXP)
}
return interpolatedStringRegexp, err
}