-
Notifications
You must be signed in to change notification settings - Fork 84
/
main.go
134 lines (118 loc) · 3.61 KB
/
main.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
package main
import (
"bytes"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"decred.org/dcrdex/client/webserver/locales"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
var (
workingDirectory, _ = os.Getwd()
titler cases.Caser
)
func main() {
write := flag.Bool("write", false, "write out translated html templates, otherwise just check (default false)")
siteDir := workingDirectory
if filepath.Base(workingDirectory) == "template-builder" {
siteDir = filepath.Dir(workingDirectory)
}
templateDir := filepath.Join(siteDir, "src", "html")
outputDirectory := filepath.Join(siteDir, "src", "localized_html")
if *write {
fmt.Println("Creating output directory:", outputDirectory)
err := os.MkdirAll(outputDirectory, 0755)
if err != nil {
fmt.Printf("MkdirAll %q error: %v \n", outputDirectory, err)
os.Exit(1)
}
for lang := range locales.Locales {
langDir := filepath.Join(outputDirectory, lang)
err := os.MkdirAll(langDir, 0755)
if err != nil {
fmt.Printf("MkdirAll %q error: %v \n", langDir, err)
os.Exit(2)
}
}
}
err := filepath.Walk(templateDir, func(_ string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
if fi.IsDir() {
return nil
}
baseName := fi.Name()
if filepath.Ext(baseName) != ".tmpl" {
return nil
}
fmt.Println(baseName)
rawTmplPath := filepath.Join(templateDir, baseName)
rawTmpl, err := os.ReadFile(rawTmplPath)
if err != nil {
return fmt.Errorf("ReadFile error: %w", err)
}
localizedTemplates := make(map[string][]byte, len(locales.Locales))
enDict := locales.Locales["en-US"]
for lang := range locales.Locales {
fmt.Println("Prepping", lang)
tmpl := make([]byte, len(rawTmpl))
copy(tmpl, rawTmpl)
localizedTemplates[lang] = tmpl
}
for _, matchGroup := range locales.Tokens(rawTmpl) {
if len(matchGroup) != 2 {
return fmt.Errorf("can't parse match group: %v", matchGroup)
}
token, key := matchGroup[0], string(matchGroup[1])
for lang, tmpl := range localizedTemplates {
titler = cases.Title(language.MustParse(lang))
dict := locales.Locales[lang]
var toTitle bool
var found bool
var replacement string
if titleKey := strings.TrimPrefix(key, ":title:"); titleKey != key {
// Check if there's a value for :title:key. Especially for languages
// that do not work well with cases.Caser, e.g zh-cn.
if replacement, found = dict[key]; !found {
toTitle = true
key = titleKey
}
}
if !found {
if replacement, found = dict[key]; !found {
if replacement, found = enDict[key]; !found {
return fmt.Errorf("no %s translation in %q and no default replacement for %s", lang, baseName, key)
}
fmt.Printf("Warning: no %s replacement text for key %q, using 'en' value %s \n", lang, key, replacement)
}
}
if toTitle {
replacement = titler.String(replacement)
}
localizedTemplates[lang] = bytes.Replace(tmpl, token, []byte(replacement), -1) // Could just do 1
}
}
if *write {
for lang, tmpl := range localizedTemplates {
langDir := filepath.Join(outputDirectory, lang)
localizedName := filepath.Join(langDir, baseName)
// ext := filepath.Ext(d.Name())
// name := baseName[:len(baseName)-len(ext)]
// localizedName := filepath.Join(outputDirectory, name+"_"+lang+ext)
fmt.Println("Writing", localizedName)
if err := os.WriteFile(localizedName, tmpl, 0644); err != nil {
return fmt.Errorf("error writing localized template %s: %v", localizedName, err)
}
}
}
return nil
})
if err != nil {
fmt.Println("WalkDir error:", err)
os.Exit(3)
}
}