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
|
package main
import (
"fmt"
"os"
"path/filepath"
"slices"
"strings"
"text/template/parse"
"golang.org/x/text/language"
"golang.org/x/text/message/pipeline"
"golang.org/x/tools/go/packages"
)
const (
pkgbase = "git.thomasvoss.com/euro-cash.eu"
srclang = "en"
srcdir = "./src"
transdir = srcdir + "/rosetta"
outfile = "catalog.gen.go"
transfn = "T"
)
func main() {
/* cd to the project root directory */
try(os.Chdir(filepath.Dir(os.Args[0])))
pkgnames := packageList(".")
var paths []string
pkgs := try2(packages.Load(&packages.Config{
Mode: packages.NeedFiles | packages.NeedEmbedFiles,
}, pkgnames...))
for _, pkg := range pkgs {
if len(pkg.Errors) != 0 {
for _, err := range pkg.Errors {
warn(err.Msg)
}
os.Exit(1)
}
for _, f := range pkg.EmbedFiles {
if filepath.Ext(f) == ".tmpl" {
paths = append(paths, f)
}
}
}
msgs := make([]pipeline.Message, 0, 1024)
for _, path := range paths {
f := try2(os.ReadFile(path))
trees := make(map[string]*parse.Tree)
t := parse.New("name")
t.Mode |= parse.SkipFuncCheck
try2(t.Parse(string(f), "", "", trees))
for _, t := range trees {
process(&msgs, t.Root)
}
}
pconf := &pipeline.Config{
Supported: languages(),
SourceLanguage: language.Make(srclang),
Packages: pkgnames,
Dir: transdir,
GenFile: outfile,
GenPackage: srcdir,
}
state := try2(pipeline.Extract(pconf))
state.Extracted.Messages = append(state.Extracted.Messages, msgs...)
try(state.Import())
try(state.Merge())
try(state.Export())
try(state.Generate())
}
func process(tmplMsgs *[]pipeline.Message, node parse.Node) {
switch node.Type() {
case parse.NodeList:
if ln, ok := node.(*parse.ListNode); ok {
for _, n := range ln.Nodes {
process(tmplMsgs, n)
}
}
case parse.NodeIf:
if in, ok := node.(*parse.IfNode); ok {
process(tmplMsgs, in.List)
if in.ElseList != nil {
process(tmplMsgs, in.ElseList)
}
}
case parse.NodeWith:
if wn, ok := node.(*parse.WithNode); ok {
process(tmplMsgs, wn.List)
if wn.ElseList != nil {
process(tmplMsgs, wn.ElseList)
}
}
case parse.NodeRange:
if rn, ok := node.(*parse.RangeNode); ok {
process(tmplMsgs, rn.List)
if rn.ElseList != nil {
process(tmplMsgs, rn.ElseList)
}
}
case parse.NodeAction:
an, ok := node.(*parse.ActionNode)
if !ok {
break
}
for _, cmd := range an.Pipe.Cmds {
if !hasIndent(cmd, transfn) {
continue
}
for _, arg := range cmd.Args {
if arg.Type() != parse.NodeString {
continue
}
if sn, ok := arg.(*parse.StringNode); ok {
txt := collapse(sn.Text)
*tmplMsgs = append(*tmplMsgs, pipeline.Message{
ID: pipeline.IDList{txt},
Key: txt,
Message: pipeline.Text{Msg: txt},
})
}
}
}
}
}
func hasIndent(cmd *parse.CommandNode, s string) bool {
if len(cmd.Args) == 0 {
return false
}
arg := cmd.Args[0]
var idents []string
switch arg.Type() {
case parse.NodeField:
idents = arg.(*parse.FieldNode).Ident
case parse.NodeVariable:
idents = arg.(*parse.VariableNode).Ident
}
return slices.Contains(idents, s)
}
func packageList(path string) []string {
ents := try2(os.ReadDir(path))
xs := make([]string, 0, len(ents))
foundOne := false
for _, ent := range ents {
switch {
case filepath.Ext(ent.Name()) == ".go":
if !foundOne {
xs = append(xs, pkgbase+"/"+path)
foundOne = true
}
case !ent.IsDir(), ent.Name() == "cmd", ent.Name() == "vendor":
continue
default:
xs = append(xs, packageList(path+"/"+ent.Name())...)
}
}
return xs
}
func languages() []language.Tag {
ents := try2(os.ReadDir(transdir))
tags := make([]language.Tag, len(ents))
for i, e := range ents {
tags[i] = language.MustParse(e.Name())
}
return tags
}
func collapse(s string) string {
var (
sb strings.Builder
prev bool
)
const spc = " \t\n"
for _, ch := range strings.Trim(s, spc) {
if strings.ContainsRune(spc, ch) {
if prev {
continue
}
ch = ' '
prev = true
} else {
prev = false
}
sb.WriteRune(ch)
}
return sb.String()
}
func try(err error) {
if err != nil {
die(err)
}
}
func try2[T any](val T, err error) T {
if err != nil {
die(err)
}
return val
}
func warn(err any) {
fmt.Fprintf(os.Stderr, "%s: %s\n", filepath.Base(os.Args[0]), err)
}
func die(err any) {
warn(err)
os.Exit(1)
}
|