forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_translation.go
379 lines (345 loc) · 10.4 KB
/
generate_translation.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package uadmin
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"reflect"
"strings"
)
/*
// Field appears in JSON as key "myName".
Field int `json:"myName"`
// Field appears in JSON as key "myName" and
// the field is omitted from the object if its value is empty,
// as defined above.
Field int `json:"myName,omitempty"`
// Field appears in JSON as key "Field" (the default), but
// the field is skipped if empty.
// Note the leading comma.
Field int `json:",omitempty"`
// Field is ignored by this package.
Field int `json:"-"`
// Field appears in JSON as key "-".
Field int `json:"-,"`
*/
type fieldLanguage struct {
DisplayName string `json:"display_name"`
updateDisplayName bool
Help string `json:"help"`
updateHelp bool
PatternMsg string `json:"pattern_msg"`
updatePatternMsg bool
ErrMsg map[string]string `json:"err_msg"`
Choices map[int]string
updateChoice []bool
}
type structLanguage struct {
DisplayName string `json:"display_name"`
updateDisplayName bool
Fields map[string]fieldLanguage `json:"fields"`
}
const translateMe = "Translate me ---> "
// syncCustomTranslation is a function for creating and updating custom translation files
func syncCustomTranslation(path string) map[string]int {
var err error
var buf []byte
stat := map[string]int{}
pathParts := strings.Split(path, "/")
if len(pathParts) != 2 {
Trail(ERROR, "Custom translation file path is incorrect (%s)", path)
return stat
}
group := pathParts[0]
name := pathParts[1]
os.MkdirAll("./static/i18n/"+group+"/", 0744)
fileName := "./static/i18n/" + group + "/" + name + ".en.json"
langMap := map[string]string{}
if _, err = os.Stat(fileName); os.IsNotExist(err) {
ioutil.WriteFile(fileName, []byte{'{', '}'}, 0644)
stat["en"] = 0
} else {
buf, err = ioutil.ReadFile(fileName)
if err != nil {
Trail(ERROR, "Unable to read system translation file (%s)", fileName)
return stat
}
err = json.Unmarshal(buf, &langMap)
if err != nil {
Trail(ERROR, "Invalid format of system translation file (%s). %s", fileName, err)
}
for _, lang := range activeLangs {
if lang.Code == "en" {
stat["en"] = len(langMap)
continue
}
stat[lang.Code] = 0
updateRequired := false
langFileName := "./static/i18n/" + group + "/" + name + "." + lang.Code + ".json"
langSystemMap := map[string]string{}
if _, err = os.Stat(langFileName); os.IsNotExist(err) {
ioutil.WriteFile(langFileName, []byte{'{', '}'}, 0644)
}
buf, err = ioutil.ReadFile(langFileName)
if err != nil {
Trail(ERROR, "Unable to read system translation file (%s)", langFileName)
return stat
}
err = json.Unmarshal(buf, &langSystemMap)
if err != nil {
Trail(ERROR, "Invalid format of system translation file (%s). %s", langFileName, err)
}
for k, v := range langMap {
if val, ok := langSystemMap[k]; !ok {
updateRequired = true
langSystemMap[k] = translateMe + v
} else {
if !strings.HasPrefix(val, translateMe) {
stat[lang.Code]++
}
}
}
if updateRequired {
saveLangFile(langSystemMap, langFileName)
}
}
}
return stat
}
func syncModelTranslation(m ModelSchema) map[string]int {
var err error
var buf []byte
stat := map[string]int{
"en": 1,
}
// Generate/Sync original language file
// First parse the schema into a structLanguage
structLang := structLanguage{}
structLang.DisplayName = m.DisplayName
structLang.Fields = map[string]fieldLanguage{}
modelCount := 1
fileCount := 1
for _, f := range m.Fields {
structLang.Fields[f.Name] = fieldLanguage{
DisplayName: f.DisplayName,
Help: f.Help,
PatternMsg: f.PatternMsg,
ErrMsg: map[string]string{},
Choices: map[int]string{},
updateChoice: []bool{},
}
if f.DisplayName != "" {
modelCount++
}
if f.Help != "" {
modelCount++
}
if f.Help != "" {
modelCount++
}
}
// Get information about the model
pkgName := fmt.Sprint(reflect.TypeOf(models[m.ModelName]))
pkgName = strings.Split(pkgName, ".")[0]
// Get the model's original language file
err = os.MkdirAll("./static/i18n/"+pkgName+"/", 0744)
if err != nil {
Trail(ERROR, "generateTranslation error creating folder (./static/i18n/"+pkgName+"/). %v", err)
return stat
}
fileName := "./static/i18n/" + pkgName + "/" + m.ModelName + ".en.json"
// Check if the fist doesn't exist and create it
if _, err = os.Stat(fileName); os.IsNotExist(err) {
buf, _ = json.MarshalIndent(structLang, "", " ")
err = ioutil.WriteFile(fileName, buf, 0644)
if err != nil {
Trail(ERROR, "generateTranslation error writing a file. %v", err)
}
fileCount = modelCount
} else {
// It is exists, read it
buf, err = ioutil.ReadFile(fileName)
if err != nil {
Trail(ERROR, "Unable to read language file (%s)", fileName)
}
// Parse the on disk file to a structLanguage
langOnFile := structLanguage{}
err = json.Unmarshal(buf, &langOnFile)
if err != nil {
Trail(ERROR, "Invalid format for language file (%s)", fileName)
}
// Check if there are any changes that required updating the language file
requiresUpdate := false
if langOnFile.DisplayName != structLang.DisplayName {
requiresUpdate = true
langOnFile.updateDisplayName = true
langOnFile.DisplayName = structLang.DisplayName
}
if langOnFile.DisplayName != "" {
fileCount++
}
for k, v := range structLang.Fields {
// Check if the field is not on file
if onFileV, ok := langOnFile.Fields[k]; !ok {
requiresUpdate = true
langOnFile.Fields[k] = v
fileCount++
} else {
// If the field is on disk, then verify all variables
if v.DisplayName != onFileV.DisplayName {
requiresUpdate = true
onFileV.updateDisplayName = true
onFileV.DisplayName = v.DisplayName
}
if v.PatternMsg != onFileV.PatternMsg {
requiresUpdate = true
onFileV.updatePatternMsg = true
onFileV.PatternMsg = v.PatternMsg
}
if v.Help != onFileV.Help {
requiresUpdate = true
onFileV.updateHelp = true
onFileV.Help = v.Help
}
if onFileV.DisplayName != "" {
fileCount++
}
if onFileV.Help != "" {
fileCount++
}
if onFileV.PatternMsg != "" {
fileCount++
}
fileCount += len(onFileV.ErrMsg)
// Assign back the onFileV
langOnFile.Fields[k] = onFileV
// Assign back the fieldLang
structLang.Fields[k] = onFileV
}
}
// If the file was changed, write it back to disk
if requiresUpdate {
buf, _ = json.MarshalIndent(langOnFile, "", " ")
err = ioutil.WriteFile(fileName, buf, 0644)
if err != nil {
Trail(ERROR, "Unable to write language file (%s)", fileName)
return stat
}
}
// Finally update the model's structLanguage with the updated one from disk
structLang = langOnFile
}
stat["en"] = fileCount
// Sync active languages
for _, lang := range activeLangs {
if lang.Code == "en" {
continue
}
updateRequired := false
stat[lang.Code] = 0
// The active language file name
langFileName := "./static/i18n/" + pkgName + "/" + m.ModelName + "." + lang.Code + ".json"
structLangOnFile := structLanguage{}
// Check if the language file exists
if _, err = os.Stat(langFileName); os.IsNotExist(err) {
buf, _ = json.MarshalIndent(structLangOnFile, "", " ")
ioutil.WriteFile(langFileName, buf, 0644)
}
// Read/Parse language file from disk
buf, err = ioutil.ReadFile(langFileName)
if err != nil {
Trail(ERROR, "Unable to read system translation file (%s)", langFileName)
continue
}
err = json.Unmarshal(buf, &structLangOnFile)
if err != nil {
Trail(ERROR, "Invalid format of system translation file (%s). %s", langFileName, err)
continue
}
// If language file is empty then initialize it
if structLangOnFile.DisplayName == "" {
updateRequired = true
structLangOnFile.DisplayName = translateMe + m.DisplayName
structLangOnFile.Fields = map[string]fieldLanguage{}
} else {
// if the file exists then verify its content
if structLang.updateDisplayName {
updateRequired = true
structLangOnFile.DisplayName = translateMe + m.DisplayName
}
if structLangOnFile.DisplayName != "" && !strings.HasPrefix(structLangOnFile.DisplayName, translateMe) {
stat[lang.Code]++
}
}
for k, v := range structLang.Fields {
// Check if the field is not on file and add it
if langV, ok := structLangOnFile.Fields[k]; !ok {
updateRequired = true
langV = fieldLanguage{
ErrMsg: map[string]string{},
}
langV.DisplayName = translateMe + v.DisplayName
if v.Help != "" {
langV.Help = translateMe + v.Help
}
if v.PatternMsg != "" {
langV.PatternMsg = translateMe + v.PatternMsg
}
for errK, errV := range v.ErrMsg {
langV.ErrMsg[errK] = translateMe + errV
}
structLangOnFile.Fields[k] = langV
} else {
// If the field exists, then verify it's contents
if v.updateDisplayName {
updateRequired = true
langV.DisplayName = translateMe + v.DisplayName
}
if langV.DisplayName != "" && !strings.HasPrefix(langV.DisplayName, translateMe) {
stat[lang.Code]++
}
if v.updateHelp {
updateRequired = true
langV.Help = translateMe + v.Help
}
if langV.Help != "" && !strings.HasPrefix(langV.Help, translateMe) {
stat[lang.Code]++
}
if v.updatePatternMsg {
updateRequired = true
langV.PatternMsg = translateMe + v.PatternMsg
}
if langV.PatternMsg != "" && !strings.HasPrefix(langV.PatternMsg, translateMe) {
stat[lang.Code]++
}
for errK, errV := range v.ErrMsg {
if _, ok = langV.ErrMsg[errK]; !ok {
updateRequired = true
langV.ErrMsg[errK] = translateMe + errV
}
if langV.ErrMsg[errK] != "" && !strings.HasPrefix(langV.ErrMsg[errK], translateMe) {
stat[lang.Code]++
}
}
// Assign the field back into the structLanguage
structLangOnFile.Fields[k] = langV
}
}
// If there are any changes, write the file back to disk
if updateRequired {
saveLangFile(structLangOnFile, langFileName)
if err != nil {
Trail(ERROR, "Unable to write language file language file (%s)", langFileName)
}
}
}
return stat
}
func saveLangFile(v interface{}, fileName string) {
buf, _ := json.MarshalIndent(v, "", " ")
buf = bytes.Replace(buf, []byte("\\u003c"), []byte("<"), -1)
buf = bytes.Replace(buf, []byte("\\u003e"), []byte(">"), -1)
langMapCache[fileName] = buf
ioutil.WriteFile(fileName, buf, 0644)
}