Skip to content

Commit

Permalink
Do not encode HTML entities in JSON message files (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksnyder authored Sep 29, 2020
1 parent e2aedcf commit 703c3bd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
7 changes: 6 additions & 1 deletion v2/goi18n/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ func marshalValue(messageTemplates map[string]*i18n.MessageTemplate, sourceLangu
func marshal(v interface{}, format string) ([]byte, error) {
switch format {
case "json":
return json.MarshalIndent(v, "", " ")
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
err := enc.Encode(v)
return buf.Bytes(), err
case "toml":
var buf bytes.Buffer
enc := toml.NewEncoder(&buf)
Expand Down
21 changes: 21 additions & 0 deletions v2/goi18n/marshal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import "testing"

func TestMarshal(t *testing.T) {
actual, err := marshal(map[string]string{
"&<key>": "&<val>",
}, "json")

if err != nil {
t.Fatal(err)
}

expected := `{
"&<key>": "&<val>"
}
`
if a := string(actual); a != expected {
t.Fatalf("\nexpected:\n%s\n\ngot\n%s", expected, a)
}
}

0 comments on commit 703c3bd

Please sign in to comment.