forked from sumatrapdfreader/sumatrapdf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslations.go
More file actions
145 lines (129 loc) · 3.08 KB
/
translations.go
File metadata and controls
145 lines (129 loc) · 3.08 KB
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
package do
import (
"bytes"
"os"
"path/filepath"
"regexp"
"strings"
)
func verifyTranslationsMust() {
d := downloadTranslationsMust()
curr := readFileMust(translationsTxtPath)
panicIf(!bytes.Equal(d, curr), "Translations did change!!!\nRun:\n.\\doit.bat -trans-dl\nto update translations\n")
}
// Translation describes a single translated text
type Translation struct {
Text string
Lang string
Translation string
}
func trimEmptyLinesFromEnd(a []string) []string {
for len(a) > 0 {
lastIdx := len(a) - 1
s := strings.TrimSpace(a[lastIdx])
if len(s) > 0 {
return a
}
a = a[:lastIdx]
}
return a
}
func parseTranslations(s string) map[string][]*Translation {
res := map[string][]*Translation{}
lines := strings.Split(s, "\n")[2:]
// strip empty lines from the end
lines = trimEmptyLinesFromEnd(lines)
currStr := ""
var currTranslations []*Translation
for _, l := range lines {
if len(l) == 0 {
continue
}
// TODO: looks like apptranslator doesn't deal well with strings that
// have newlines in them. Newline at the end ends up as an empty line
// apptranslator should escape newlines and tabs etc. but for now
// skip those lines as harmless
if l[0] == ':' {
if currStr != "" {
panicIf(len(currTranslations) == 0)
res[currStr] = currTranslations
}
currStr = l[1:]
currTranslations = nil
} else {
parts := strings.SplitN(l, ":", 2)
panicIf(len(parts) != 2, "Invalid line: '%s'", l)
lang, trans := parts[0], parts[1]
tr := &Translation{
Text: currStr,
Lang: lang,
Translation: trans,
}
currTranslations = append(currTranslations, tr)
}
}
if currStr != "" {
panicIf(len(currTranslations) == 0)
res[currStr] = currTranslations
}
return res
}
var (
dirsToProcess = []string{"src"}
)
func getFilesToProcess() []string {
shouldTranslate := func(path string) bool {
ext := strings.ToLower(filepath.Ext(path))
return ext == ".cpp"
}
var res []string
for _, dir := range dirsToProcess {
files, err := os.ReadDir(dir)
must(err)
for _, f := range files {
path := filepath.Join(dir, f.Name())
if shouldTranslate(path) {
res = append(res, path)
}
}
}
return res
}
var (
translationPattern = regexp.MustCompile(`\b_TR[AN]?\("(.*?)"\)`)
)
func extractTranslations(s string) []string {
var res []string
a := translationPattern.FindAllStringSubmatch(s, -1)
for _, el := range a {
res = append(res, el[1])
}
return res
}
func extractStringsFromCFile(path string) []string {
d := readFileMust(path)
return extractTranslations(string(d))
}
func uniquifyStrings(a []string) []string {
m := map[string]bool{}
for _, s := range a {
m[s] = true
}
var res []string
for k := range m {
res = append(res, k)
}
return res
}
func extractStringsFromCFilesNoPaths() []string {
filesToProcess := getFilesToProcess()
logf("Files to process: %d\n", len(filesToProcess))
var res []string
for _, path := range filesToProcess {
a := extractStringsFromCFile(path)
res = append(res, a...)
}
res = uniquifyStrings(res)
logf("%d strings to translate\n", len(res))
return res
}