1
1
package locale
2
2
3
3
import (
4
+ "bytes"
5
+ "crypto/sha1"
4
6
"fmt"
7
+ "io"
5
8
"io/ioutil"
6
- "log"
9
+
7
10
"math/rand"
8
11
"os"
9
12
"regexp"
10
13
"strings"
14
+
15
+ "github.com/BurntSushi/toml"
16
+ "github.com/nicksnyder/go-i18n/v2/i18n"
11
17
)
12
18
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
+ }
14
22
15
23
func Owoify (input string ) string {
16
24
pieces := strings .Split (input , "{{" )
17
25
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 ])
22
31
}
32
+
23
33
return full
24
34
}
25
35
@@ -44,42 +54,92 @@ func owoifyString(input string) string {
44
54
return output
45
55
}
46
56
47
- func OwoToml (path , output string ) {
57
+ func OwoToml (path , output string ) error {
48
58
f , err := os .Open (path )
49
59
if err != nil {
50
- log .Println (err )
51
- return
60
+ return err
52
61
}
53
62
defer f .Close ()
54
63
55
64
bytes , err := ioutil .ReadAll (f )
56
65
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
59
93
}
60
94
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
+
62
110
if err != nil {
63
- log .Println (err )
64
- return
111
+ return nil , fmt .Errorf ("failed to marshal strings: %s" , err )
65
112
}
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 {
77
126
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 ])
79
128
} else {
80
- outputfile . WriteString ( fmt .Sprintf ("%s = \" %s \" \n " , arr [ 0 ], Owoify (text ) ))
129
+ text = fmt .Sprintf ("%s" , Owoify (text ))
81
130
}
131
+
132
+ m [string (pluralForm )] = text
82
133
}
134
+ val [id ] = m
83
135
}
136
+ return val
137
+ }
84
138
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 ))
85
145
}
0 commit comments