From 703c3bde3ea09b5f449581f4cb30f681e11351a3 Mon Sep 17 00:00:00 2001 From: Nick Snyder Date: Mon, 28 Sep 2020 21:14:03 -0700 Subject: [PATCH] Do not encode HTML entities in JSON message files (#228) --- v2/goi18n/marshal.go | 7 ++++++- v2/goi18n/marshal_test.go | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 v2/goi18n/marshal_test.go diff --git a/v2/goi18n/marshal.go b/v2/goi18n/marshal.go index 751b698a..a6cc762f 100644 --- a/v2/goi18n/marshal.go +++ b/v2/goi18n/marshal.go @@ -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) diff --git a/v2/goi18n/marshal_test.go b/v2/goi18n/marshal_test.go new file mode 100644 index 00000000..de6b392f --- /dev/null +++ b/v2/goi18n/marshal_test.go @@ -0,0 +1,21 @@ +package main + +import "testing" + +func TestMarshal(t *testing.T) { + actual, err := marshal(map[string]string{ + "&": "&", + }, "json") + + if err != nil { + t.Fatal(err) + } + + expected := `{ + "&": "&" +} +` + if a := string(actual); a != expected { + t.Fatalf("\nexpected:\n%s\n\ngot\n%s", expected, a) + } +}