Skip to content

Commit 703c3bd

Browse files
authored
Do not encode HTML entities in JSON message files (nicksnyder#228)
1 parent e2aedcf commit 703c3bd

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

‎v2/goi18n/marshal.go‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@ func marshalValue(messageTemplates map[string]*i18n.MessageTemplate, sourceLangu
4949
func marshal(v interface{}, format string) ([]byte, error) {
5050
switch format {
5151
case "json":
52-
return json.MarshalIndent(v, "", " ")
52+
var buf bytes.Buffer
53+
enc := json.NewEncoder(&buf)
54+
enc.SetEscapeHTML(false)
55+
enc.SetIndent("", " ")
56+
err := enc.Encode(v)
57+
return buf.Bytes(), err
5358
case "toml":
5459
var buf bytes.Buffer
5560
enc := toml.NewEncoder(&buf)

‎v2/goi18n/marshal_test.go‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import "testing"
4+
5+
func TestMarshal(t *testing.T) {
6+
actual, err := marshal(map[string]string{
7+
"&<key>": "&<val>",
8+
}, "json")
9+
10+
if err != nil {
11+
t.Fatal(err)
12+
}
13+
14+
expected := `{
15+
"&<key>": "&<val>"
16+
}
17+
`
18+
if a := string(actual); a != expected {
19+
t.Fatalf("\nexpected:\n%s\n\ngot\n%s", expected, a)
20+
}
21+
}

0 commit comments

Comments
 (0)