-
Notifications
You must be signed in to change notification settings - Fork 1
/
chili-tradutor-go.go
411 lines (359 loc) · 13.1 KB
/
chili-tradutor-go.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
chili-tradutor-go - Command-line JSON processor, similar to jq
go get github.com/go-ini/ini
Chili GNU/Linux - https://github.com/vcatafesta/chili/go
Chili GNU/Linux - https://chililinux.com
Chili GNU/Linux - https://chilios.com.br
Created: 2023/10/01
Altered: 2023/10/16
Copyright (c) 2023-2023, Vilmar Catafesta <vcatafesta@gmail.com>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"github.com/fatih/color"
"github.com/spf13/pflag"
// "github.com/ogier/pflag"
"log"
"os"
"os/exec"
"strings"
"sync"
)
const (
_APP_ = "chili-tradutor-go"
_VERSION_ = "1.2.2-20231014"
_COPY_ = "Copyright (C) 2023 Vilmar Catafesta, <vcatafesta@gmail.com>"
)
// Constantes para cores ANSI
const (
Reset = "\x1b[0m"
Red = "\x1b[31m"
Green = "\x1b[32m"
Yellow = "\x1b[33m"
Blue = "\x1b[34m"
Magenta = "\x1b[35m"
Cyan = "\x1b[36m"
White = "\x1b[37m"
)
var (
cyan = color.New(color.Bold, color.FgCyan).SprintFunc()
orange = color.New(color.FgYellow).SprintFunc()
yellow = color.New(color.Bold, color.FgYellow).SprintFunc()
green = color.New(color.FgGreen).SprintFunc()
magenta = color.New(color.FgMagenta).SprintFunc()
red = color.New(color.FgRed).SprintFunc()
boldred = color.New(color.Bold, color.FgRed).SprintFunc()
blue = color.New(color.FgBlue).SprintFunc()
white = color.New(color.FgWhite).SprintFunc()
black = color.New(color.Bold, color.FgBlack).SprintFunc()
success = color.New(color.Bold, color.FgGreen).FprintlnFunc()
notice = color.New(color.Bold, color.FgGreen).PrintlnFunc()
)
var (
inputFile string
showVersion bool
showHelp bool
forceFlag bool
quietFlag bool
verboseFlag bool
nocolorFlag bool
languageCode string
languages []string
)
var supportedLanguages = []string{
"ar",
"bg",
"cs",
"da", "de",
"el", "en", "es", "et",
"fa", "fi", "fr",
"he", "hi", "hr", "hu",
"is", "it",
"ja",
"ko",
"nl", "no",
"pl", "pt-PT", "pt-BR",
"ro", "ru",
"sk", "sv",
"tr",
"uk",
"zh",
}
var (
IsForce bool
logger *log.Logger
loggerMu sync.Mutex
)
func init() {
}
func confLog() {
fileLog := "/tmp/" + _APP_ + ".log"
logFile, err := os.OpenFile(fileLog, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
if err != nil {
log.Fatalf("Erro ao abrir o arquivo de log: %v", err)
}
// defer logFile.Close()
// Configurar o logger para escrever no arquivo
// logger = log.New(logFile, "", log.LstdFlags)
// logger = log.New(io.MultiWriter(os.Stdout, logFile), "[INFO] ", log.LstdFlags)
// logger = log.New(io.MultiWriter(os.Stdout, logFile), "", log.LstdFlags)
if quietFlag {
logger = log.New(logFile, "", 0)
} else {
logger = log.New(io.MultiWriter(os.Stdout, logFile), "", 0) // O 0 desabilita o timestamp
}
logger.Printf("%s Iniciando log: %s", black("[LOG]"), magenta(fileLog))
}
func main() {
// Use pflag em vez de flag para criar opções curtas e longas
pflag.StringVarP(&inputFile, "inputfile", "i", "", "Arquivo de entrada")
pflag.BoolVarP(&showVersion, "version", "V", false, "Mostar versão")
pflag.BoolVarP(&showHelp, "help", "h", false, "Mostrar help")
pflag.BoolVarP(&forceFlag, "force", "f", false, "Forçar")
pflag.BoolVarP(&quietFlag, "quiet", "q", false, "Modo quieto")
pflag.BoolVarP(&verboseFlag, "verbose", "v", false, "Modo verbose")
pflag.BoolVarP(&nocolorFlag, "nocolor", "n", false, "Desliga saida em cores")
// pflag.StringVarP(&languageCode, "language", "l", "", "Language code")
pflag.StringSliceVarP(&languages, "language", "l", nil, "Idiomas")
// pflag.Lookup("language").NoOptDefVal = "4321"
pflag.Parse()
for _, arg := range os.Args[1:] {
switch arg {
case "-V", "--version":
fmt.Printf("Version: %s\n", _VERSION_)
os.Exit(0)
case "-h", "--help":
usage()
os.Exit(0)
case "-f", "--force":
IsForce = true
}
}
confLog()
if nocolorFlag {
color.NoColor = true // disables colorized output
}
if inputFile == "" || len(os.Args) < 2 {
usage()
os.Exit(1)
}
_, err := os.Stat(inputFile)
if err != nil {
if os.IsNotExist(err) {
logger.Printf("%s Arquivo informado não existe: %v\n", red("[ERROR]"), err)
} else {
logger.Printf("%s Erro ao verificar o arquivo: %v\n", red("[ERROR]"), err)
}
}
// if languageCode != "" {
// supportedLanguages = []string{languageCode}
// }
if len(languages) != 0 {
supportedLanguages = languages
}
if len(supportedLanguages) > 0 {
prepareGettext(inputFile)
var wg sync.WaitGroup
for _, lang := range supportedLanguages {
wg.Add(1)
go func(lang string) {
defer wg.Done()
if languageCode == "" || languageCode == lang {
prepareMsginit(inputFile, lang)
translateFile(inputFile, lang)
writeMsgfmtToMo(inputFile, lang)
os.Remove(fmt.Sprintf("%s-temp-%s.po", inputFile, lang))
}
}(lang)
}
wg.Wait()
}
}
func prepareMsginit(inputFile, lang string) {
potFile := inputFile + ".pot"
poFile := fmt.Sprintf("%s-%s.po", inputFile, lang)
poFileTmp := fmt.Sprintf("%s-temp-%s.po", inputFile, lang)
if quietFlag {
fmt.Printf("%s Inicializando catalogo de mensagens para o idioma '%s'\n", green("[MSGINIT]"), cyan(lang))
}
if _, err := os.Stat(poFile); os.IsNotExist(err) || forceFlag {
logger.Printf("%s Rodando msginit para o idioma: '%s'\n", black("[MSGINIT]"), cyan(lang))
cmd := exec.Command("msginit", "--no-translator", "--locale="+lang, "--input="+potFile, "--output="+poFileTmp)
if err := cmd.Run(); err != nil {
logger.Printf("%s Erro ao executar 'msginit': %v\n", red("[MSGINIT]"), err)
}
logger.Printf("%s Catalogo de mensagens para o idioma '%s' inicializado: %s\n", green("[MSGINIT]"), cyan(lang), magenta(poFileTmp))
logger.Printf("%s Rodando '%s' em: %s\n", black("[MSGINIT]"), black("sed"), magenta(poFileTmp))
cmd = exec.Command("sed", "-i", "s|Content-Type: text/plain; charset=ASCII|Content-Type: text/plain; charset=utf-8|g", poFileTmp)
if err := cmd.Run(); err != nil {
logger.Printf("%s Erro ao executar '%s': %v\n", red("[SED]"), black("sed"), err)
}
}
}
func translateFile(inputFile, lang string) {
translatedFile := fmt.Sprintf("%s-%s.po", inputFile, lang)
poFileTemp := fmt.Sprintf("%s-temp-%s.po", inputFile, lang)
if quietFlag {
fmt.Printf("%s Traduzindo mensagens para o idioma: '%s'\n", black("[TRANS]"), cyan(lang))
}
if _, err := os.Stat(translatedFile); !os.IsNotExist(err) && !forceFlag {
logger.Printf("%s Pulando %s, arquivo existe.\n", cyan("[TRANS]"), translatedFile)
return
}
file, err := os.Open(poFileTemp)
if err != nil {
logger.Printf("%s Erro ao abrir arquivo: %v\n", red("[ERROR]"), err)
}
defer file.Close()
outputFile, err := os.Create(translatedFile)
if err != nil {
logger.Printf("%s Erro ao criar arquivo: %v\n", red("[ERROR]"), err)
}
defer outputFile.Close()
scanner := bufio.NewScanner(file)
var msgidLines []string
var isMsgid bool
logger.Printf("%s Traduzindo mensagens para o idioma: '%s'\n", black("[TRANS]"), cyan(lang))
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "msgid ") {
isMsgid = true
msgidLines = []string{strings.TrimPrefix(line, "msgid ")}
} else if strings.HasPrefix(line, "msgstr ") && isMsgid {
msgstr := translateMessage(strings.Join(msgidLines, "\n"), lang)
fmt.Fprintf(outputFile, "msgid %s\nmsgstr %s\n\n", strings.Join(msgidLines, "\n"), msgstr)
isMsgid = false
msgidLines = nil
} else if isMsgid {
msgidLines = append(msgidLines, line)
} else {
fmt.Fprintln(outputFile, line)
}
}
if err := scanner.Err(); err != nil {
logger.Printf("%s Erro ao ler o arquivo: [%v]\n", red("[ERROR]"), err)
}
logger.Printf("%s Traduzido para o idioma: '%s' [%s]\n", green("[TRANS]"), cyan(lang), translatedFile)
}
func translateMessage(msgid, languageCode string) string {
var msgstr string
msgid = strings.TrimSpace(msgid)
if len(msgid) > 2 {
msgid = strings.Trim(msgid, `"`)
cmd := exec.Command("trans", "-no-autocorrect", "-b", ":"+languageCode, msgid)
// output, err := cmd.CombinedOutput()
output, err := cmd.Output()
if err != nil {
logger.Printf("%s Erro (%v) ao traduzir mensagem para o idioma: '%s', tentando novamente...\n", red("[TRANS]"), red(err), cyan(languageCode))
cmd := exec.Command("trans", "-no-autocorrect", "-b", ":"+languageCode, msgid)
// output, err := cmd.CombinedOutput()
output, err := cmd.Output()
if err != nil {
logger.Printf("%s Erro ao traduzir a mensagem, usando original: %v\n", red("[TRANS]"), err)
msgstr = msgid
} else {
output = bytes.TrimSpace(output)
msgstr = `"` + string(output) + `"`
}
} else {
output = bytes.TrimSpace(output)
msgstr = `"` + string(output) + `"`
}
} else {
msgstr = msgid
}
if verboseFlag {
logger.Printf("%s (%s)\t%s [len=%-3d] %s %s => %s\n", orange("[TRANS]"), cyan(languageCode), yellow("Traduzindo msgid:"), len(msgid), msgid, green("to"), msgstr)
}
return msgstr
}
func writeMsgfmtToMo(inputFile, lang string) {
directoryPath := "usr/share/locale/" + lang + "/LC_MESSAGES"
translatedFile := fmt.Sprintf("%s-%s.po", inputFile, lang)
moFile := fmt.Sprintf("%s/%s.mo", directoryPath, inputFile)
var cmd *exec.Cmd
if err := os.MkdirAll(directoryPath, os.ModePerm); err != nil {
logger.Printf("%s Ocorreu erro ao criar o diretório: %v\n", red("[MSGFMT]"), err)
os.Exit(1)
}
logger.Printf("%s Diretório criado com sucesso: %s\n", green("[MSGFMT]"), directoryPath)
cmd = exec.Command("msgfmt", translatedFile, "-o", moFile)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
logger.Printf("%s Erro ao executar 'msgfmt': %v\n", red("[MSGFMT]"), err)
os.Exit(1)
}
if quietFlag {
fmt.Printf("%s Traduzido para o idioma '%s':\t%s\n", green("[MSGFMT]"), cyan(lang), magenta(moFile))
} else {
logger.Printf("%s Traduzido para o idioma '%s':\t%s\n", green("[MSGFMT]"), cyan(lang), magenta(moFile))
}
}
func prepareGettext(inputFile string) {
potFile := inputFile + ".pot"
logger.Printf("%s Preparando arquivo: %s\n", black("[XGETTEXT]"), magenta(potFile))
// Verificar se o arquivo .pot já existe
if _, err := os.Stat(potFile); os.IsNotExist(err) || forceFlag {
logger.Printf("%s Rodando xgettext em: %s\n", black("[XGETTEXT]"), magenta(inputFile))
cmd := exec.Command("xgettext", "--verbose", "--from-code=UTF-8", "--language=shell", "--keyword=gettext", "--output="+potFile, inputFile)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
logger.Printf("%s => executando 'xgettext': %v\n", red("[XGETTEXT]"), err)
os.Exit(1)
}
if _, err := os.Stat(potFile); os.IsNotExist(err) {
logger.Printf("%s => xgettext não gerou '%s': (%s)\n", red("[XGETTEXT]"), potFile, err)
os.Exit(1)
}
} else {
logger.Printf("%s Pulando xgettext, %s já existe!\n", cyan("[XGETTEXT]"), potFile)
}
logger.Printf("%s Rodando '%s' passo #1\n", black("[XGETTEXT]"), cyan("sed"))
cmd := exec.Command("sed", "-i", "s/Content-Type: text\\/plain; charset=CHARSET\\\\n/Content-Type: text\\/plain; charset=UTF-8\\\\n/", potFile)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
logger.Printf("%s Erro (%v) ao executar '%s' passo #1\n", red("[SED]"), red(err), cyan("sed"))
}
logger.Printf("%s Rodando '%s' passo #2\n", black("[XGETTEXT]"), cyan("sed"))
cmd = exec.Command("sed", "-i", "s/Language: \\n/Language: pt_BR\\n/", potFile)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
logger.Printf("%s Erro (%v) ao executar '%s' passo #2\n", red("[SED]"), red(err), cyan("sed"))
}
}
func usage() {
fmt.Println("Uso: chili-tradutor-go -i <input_file> [-q] [-V] [-h] [-f] [-l <en,fr,es,...>]")
fmt.Fprintf(os.Stderr, " --i, --inputfile Input file\n")
fmt.Fprintf(os.Stderr, " --V, --version Show version\n")
fmt.Fprintf(os.Stderr, " --h, --help Show help\n")
fmt.Fprintf(os.Stderr, " --f, --force Force flag\n")
fmt.Fprintf(os.Stderr, " --q, --quiet Quiet flag\n")
fmt.Fprintf(os.Stderr, " --l, --language Language code\n")
}