-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtranslate.go
269 lines (255 loc) · 8.26 KB
/
translate.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
package main
import (
"bufio"
"context"
"fmt"
"log"
"os"
"regexp"
"strings"
"cloud.google.com/go/translate"
readingtime "github.com/begmaroman/reading-time"
"golang.org/x/text/language"
"google.golang.org/api/option"
)
func AuthTranslate(jsonPath, projectID string) (*translate.Client, context.Context, error) {
ctx := context.Background()
client, err := translate.NewClient(ctx, option.WithCredentialsFile(jsonPath))
if err != nil {
log.Fatal(err)
return client, ctx, err
}
return client, ctx, nil
}
// this is directly copy/pasted from Google example
func translateTextWithModel(targetLanguage, text, model string) (string, error) {
lang, err := language.Parse(targetLanguage)
if err != nil {
return "", fmt.Errorf("language.Parse: %v", err)
}
client, ctx, err := AuthTranslate("google-secret.json", "103373479946395174633")
if err != nil {
return "", fmt.Errorf("translate.NewClient: %v", err)
}
defer client.Close()
resp, err := client.Translate(ctx, []string{text}, lang, &translate.Options{
Model: model, // Either "nmt" or "base".
})
if err != nil {
return "", fmt.Errorf("Translate: %v", err)
}
if len(resp) == 0 {
return "", nil
}
return resp[0].Text, nil
}
// I get tired of typing this all the time
func checkError(err error) {
if err != nil {
log.Fatal(err)
}
}
func xl(fromLang string, toLang string, xlate string) string {
// fix URLs because google translate changes [link](http://you.link) to
// [link] (http://your.link) and it *also* will translate any path
// components, thus breaking your URLs.
reg := regexp.MustCompile(`]\([-a-zA-Z0-9@:%._\+~#=\/]{1,256}\)`)
// get all the URLs with a single RegEx, keep them for later.
var foundUrls [][]byte = reg.FindAll([]byte(xlate), -1)
translated, err := translateTextWithModel(toLang, xlate, "nmt")
checkError(err)
// a bunch of regexs to fix other broken stuff
reg = regexp.MustCompile(` (\*\*) ([A-za-z0-9]+) (\*\*)`) // fix bolds (**foo**)
translated = string(reg.ReplaceAll([]byte(translated), []byte(" $1$2$3")))
reg = regexp.MustCompile(`"`) // fix escaped quotes
translated = string(reg.ReplaceAll([]byte(translated), []byte("\"")))
reg = regexp.MustCompile(`>`) //fix >
translated = string(reg.ReplaceAll([]byte(translated), []byte(">")))
reg = regexp.MustCompile(`<`) // fix <
translated = string(reg.ReplaceAll([]byte(translated), []byte("<")))
reg = regexp.MustCompile(`'`) // fix '
translated = string(reg.ReplaceAll([]byte(translated), []byte("'")))
reg = regexp.MustCompile(` (\*) ([A-za-z0-9]+) (\*)`) // fix underline (*foo*)
translated = string(reg.ReplaceAll([]byte(translated), []byte("$1$2$3")))
reg = regexp.MustCompile(`({{)(<)[ ]{1,3}([vV]ideo)`) // fix video shortcodes
translated = string(reg.ReplaceAll([]byte(translated), []byte("$1$2 video")))
reg = regexp.MustCompile(`({{)(<)[ ]{1,3}([yY]outube)`) // fix youtube shortcodes
translated = string(reg.ReplaceAll([]byte(translated), []byte("$1$2 youtube")))
// Now it's time to go back and replace all the fucked up urls ...
reg = regexp.MustCompile(`] \([-a-zA-Z0-9@:%._\+~#=\/ ]{1,256}\)`)
for x := 0; x < len(foundUrls); x++ {
// fmt.Println("FoundURL: ", string(foundUrls[x]))
tmp := reg.FindIndex([]byte(translated))
if tmp == nil {
break
}
t := []byte(translated)
translated = fmt.Sprintf("%s(%s%s", string(t[0:tmp[0]+1]), string(foundUrls[x][2:]), (string(t[tmp[1]:])))
}
return translated
}
// walk through the front matter, etc. and translate stuff
func doXlate(from string, lang string, readFile string, writeFile string) {
file, err := os.Open(readFile)
checkError(err)
defer file.Close()
xfile, err := os.Create(writeFile)
checkError(err)
defer xfile.Close()
head := false
code := false
scanner := bufio.NewScanner(file)
for scanner.Scan() {
ln := scanner.Text()
if strings.HasPrefix(ln, "{{") {
xfile.WriteString(ln + "\n")
continue
}
if strings.HasPrefix(ln, "```") { // deal with in-line code
xfile.WriteString(ln + "\n")
code = !code
continue
}
if code { // I don't translate code!
xfile.WriteString(ln + "\n")
continue
}
if string(ln) == "---" { // start and end of front matter
xfile.WriteString(ln + "\n")
head = !head
} else if !head {
if strings.HasPrefix(ln, "!") { // translate the ALT-TEXT not the image path
bar := strings.Split(ln, "]")
desc := strings.Split(bar[0], "[")
translated := xl(from, lang, desc[1])
xfile.WriteString("![" + translated + "]" + bar[1] + "\n")
} else { // blank lines and everything else
if ln == "" { // handle blank lines.
xfile.WriteString("\n")
} else { // everything else
translated := xl(from, lang, ln)
xfile.WriteString(translated + "\n")
}
}
} else { // handle header fields
headString := strings.Split(ln, ":")
if headString[0] == "title" { // title
translated := xl(from, lang, headString[1])
xfile.WriteString(headString[0] + ": " + translated + "\n")
} else if headString[0] == "description" { // description
translated := xl(from, lang, headString[1])
xfile.WriteString(headString[0] + ": " + translated + "\n")
} else { // all other header fields left as-is
xfile.WriteString(ln + "\n")
}
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
xfile.Close()
file.Close()
}
// is a value in the array?
func isValueInList(value string, list []string) bool { // Test Written
for _, v := range list {
if v == value {
return true
}
}
return false
}
// future work for automagically translating all files.
func getFile(from string, path string, lang string) {
thisDir, err := os.ReadDir(path)
checkError(err)
for _, f := range thisDir {
if f.IsDir() {
if f.Name() == "images" {
continue
}
//fmt.Println("going into ", path + "/" + f.Name())
getFile(from, path+"/"+f.Name(), lang) // fucking hell, recursion!
} else {
if strings.Split(f.Name(), ".")[0] == "_index" || strings.Split(f.Name(), ".")[0] == "index" {
fromFile := fmt.Sprintf("%s/%s.%s.md", path, strings.Split(f.Name(), ".")[0], from)
toFile := fmt.Sprintf("%s/%s.%s.md", path, strings.Split(f.Name(), ".")[0], lang)
// fmt.Println("From: ", fromFile)
// fmt.Println(toFile)
_, err := os.Stat(toFile)
if !os.IsNotExist(err) {
if !(strings.Split(f.Name(), ".")[0] == "_index") {
addReadingTime(fromFile)
addReadingTime(toFile)
}
// fmt.Printf("Already translated:\t %s/index.%s.md\n", path, lang)
continue
}
addReadingTime(fromFile) // get the reading time first.
// fmt.Printf("Found a file to translate:\t %s/%s\n", path, f.Name())
fmt.Printf("Translating:\t %s\nto: \t\t%s\n", fromFile, toFile)
doXlate(from, lang, fromFile, toFile)
// }
continue
}
}
}
}
func addReadingTime(file string) {
// fmt.Println("Reading: ", file)
f, err := os.ReadFile(file)
if strings.Index(string(f), "reading_time:") > 0 {
return
}
checkError(err)
estimation := readingtime.Estimate(string(f))
fm := strings.LastIndex(string(f), "---")
newArt := f[:fm]
fw, err := os.Create(file)
checkError(err)
defer fw.Close()
fw.WriteString(string(newArt))
mins := int(estimation.Duration.Minutes())
dur := ""
if mins > 1 {
dur = fmt.Sprintf("reading_time: %d minutes\n", mins)
} else if mins == 1 {
dur = fmt.Sprintf("reading_time: %d minute\n", mins)
} else {
}
fw.WriteString(dur)
fw.WriteString(string(f[fm:]))
fw.Close()
}
func main() {
fromLang := "en"
langs := [4]string{"nl", "fr", "de", "es"} // only doing these four languages right now
dir := os.Args[1] // only doing a directory passed in
for x := 0; x < len(langs); x++ {
lang := langs[x]
// fmt.Print("Translating: \n" + dir + "\nTo: ")
// switch lang {
// case "es":
// fmt.Println("Spanish")
// case "fr":
// fmt.Println("French")
// case "de":
// fmt.Println("German")
// case "nl":
// fmt.Println("Dutch")
// }
fi, err := os.Stat(dir)
checkError(err)
switch mode := fi.Mode(); {
case mode.IsDir():
// do directory stuff
getFile(fromLang, dir, lang)
case mode.IsRegular(): // we're just doing one file
pt := strings.Split(dir, "/")
fn := strings.Split(pt[len(pt)-1], ".")
path := strings.TrimRight(dir, pt[len(pt)-1])
writeFile := fmt.Sprintf("%s%s.%s.%s", path, fn[0], lang, fn[len(fn)-1])
doXlate(fromLang, lang, dir, writeFile)
}
}
}