Skip to content

Commit 7c40490

Browse files
committed
chore(locale): updated de ÓwÓ genyewation medod
1 parent ded5d6f commit 7c40490

File tree

3 files changed

+846
-219
lines changed

3 files changed

+846
-219
lines changed

locale/owo.go

+87-27
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
11
package locale
22

33
import (
4+
"bytes"
5+
"crypto/sha1"
46
"fmt"
7+
"io"
58
"io/ioutil"
6-
"log"
9+
710
"math/rand"
811
"os"
912
"regexp"
1013
"strings"
14+
15+
"github.com/BurntSushi/toml"
16+
"github.com/nicksnyder/go-i18n/v2/i18n"
1117
)
1218

13-
var OwoFaces = []string{"OwO", "Owo", "owO", "ÓwÓ", "ÕwÕ", "@w@", "ØwØ", "øwø", "uwu", "☆w☆", "✧w✧", "♥w♥", "゜w゜", "◕w◕", "ᅌwᅌ", "◔w◔", "ʘwʘ", "⓪w⓪", "(owo)"}
19+
var OwoFaces = []string{
20+
"OwO", "Owo", "owO", "ÓwÓ", "ÕwÕ", "@w@", "ØwØ", "øwø", "uwu", "☆w☆", "✧w✧", "♥w♥", "゜w゜", "◕w◕", "ᅌwᅌ", "◔w◔", "ʘwʘ", "⓪w⓪", "(owo)",
21+
}
1422

1523
func Owoify(input string) string {
1624
pieces := strings.Split(input, "{{")
1725
full := owoifyString(pieces[0])
18-
if len(pieces) > 1 {
19-
//NOTE will fail for strings with {{ but no matching }}
20-
sub := strings.Split(pieces[1], "}}")
21-
full += "{{" + sub[0] + "}}" + Owoify(sub[1])
26+
27+
for _, str := range pieces[1:] {
28+
// NOTE will fail for strings with {{ but no matching }}
29+
sub := strings.Split(str, "}}")
30+
full += "{{" + sub[0] + "}}" + owoifyString(sub[1])
2231
}
32+
2333
return full
2434
}
2535

@@ -44,42 +54,92 @@ func owoifyString(input string) string {
4454
return output
4555
}
4656

47-
func OwoToml(path, output string) {
57+
func OwoToml(path, output string) error {
4858
f, err := os.Open(path)
4959
if err != nil {
50-
log.Println(err)
51-
return
60+
return err
5261
}
5362
defer f.Close()
5463

5564
bytes, err := ioutil.ReadAll(f)
5665
if err != nil {
57-
log.Println(err)
58-
return
66+
return err
67+
}
68+
69+
unmarshalFuncs := map[string]i18n.UnmarshalFunc{
70+
"toml": toml.Unmarshal,
71+
}
72+
mf, err := i18n.ParseMessageFileBytes(bytes, path, unmarshalFuncs)
73+
74+
if err != nil {
75+
return fmt.Errorf("failed to load message file %s: %s", path, err)
76+
}
77+
78+
messageTemplates := map[string]*i18n.MessageTemplate{}
79+
for _, m := range mf.Messages {
80+
template := i18n.NewMessageTemplate(m)
81+
if template == nil {
82+
continue
83+
}
84+
85+
template.Hash = hash(template)
86+
messageTemplates[m.ID] = template
87+
}
88+
89+
val := marshalOwoValue(messageTemplates)
90+
content, err := encodeToml(val)
91+
if err != nil {
92+
return err
5993
}
6094

61-
outputfile, err := os.Create(output)
95+
if err := ioutil.WriteFile(output, content, 0666); err != nil {
96+
return err
97+
}
98+
99+
return nil
100+
}
101+
102+
func encodeToml(v interface{}) (content []byte, err error) {
103+
// by toml
104+
var buf bytes.Buffer
105+
enc := toml.NewEncoder(&buf)
106+
enc.Indent = ""
107+
err = enc.Encode(v)
108+
content = buf.Bytes()
109+
62110
if err != nil {
63-
log.Println(err)
64-
return
111+
return nil, fmt.Errorf("failed to marshal strings: %s", err)
65112
}
66-
defer outputfile.Close()
67-
68-
lines := strings.Split(string(bytes), "\n")
69-
for _, line := range lines {
70-
arr := strings.Split(line, " = ")
71-
if len(arr) > 1 {
72-
text := arr[1][1 : len(arr[1])-2]
73-
text = strings.ReplaceAll(text, "\n", "")
74-
text = strings.ReplaceAll(text, "\r", "")
75-
genFace := rand.Intn(2)
76-
if genFace == 1 {
113+
return
114+
}
115+
116+
func marshalOwoValue(messageTemplates map[string]*i18n.MessageTemplate) interface{} {
117+
val := make(map[string]interface{}, len(messageTemplates))
118+
for id, template := range messageTemplates {
119+
m := map[string]string{}
120+
121+
m["hash"] = template.Hash
122+
123+
for pluralForm, template := range template.PluralTemplates {
124+
text := template.Src
125+
if rand.Intn(2) == 1 {
77126
faceIdx := rand.Intn(len(OwoFaces))
78-
outputfile.WriteString(fmt.Sprintf("%s = \"%s %s\"\n", arr[0], Owoify(text), OwoFaces[faceIdx]))
127+
text = fmt.Sprintf("%s %s", Owoify(text), OwoFaces[faceIdx])
79128
} else {
80-
outputfile.WriteString(fmt.Sprintf("%s = \"%s\"\n", arr[0], Owoify(text)))
129+
text = fmt.Sprintf("%s", Owoify(text))
81130
}
131+
132+
m[string(pluralForm)] = text
82133
}
134+
val[id] = m
83135
}
136+
return val
137+
}
84138

139+
// Source: https://github.com/nicksnyder/go-i18n/blob/603af13488ca751833928c45f7ada0eed720a392/v2/goi18n/merge_command.go#L294
140+
func hash(t *i18n.MessageTemplate) string {
141+
h := sha1.New()
142+
_, _ = io.WriteString(h, t.Description)
143+
_, _ = io.WriteString(h, t.PluralTemplates["other"].Src)
144+
return fmt.Sprintf("sha1-%x", h.Sum(nil))
85145
}

locale/owo_test.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
package locale
22

3-
import "testing"
3+
import (
4+
"log"
5+
"testing"
6+
)
47

58
func TestOwoToml(t *testing.T) {
6-
OwoToml("../locales/active.en.toml", "../locales/active.zu.toml")
9+
err := OwoToml("../locales/active.en.toml", "../locales/active.zu.toml")
10+
if err != nil {
11+
log.Println(err)
12+
return
13+
}
714
}

0 commit comments

Comments
 (0)