Skip to content

Commit 9f5ef2d

Browse files
committed
i18n/xgettext-go: preserve already escaped quotes
1 parent 9799106 commit 9f5ef2d

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

i18n/xgettext-go/main.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,9 @@ func inspectNodeForTranslations(fset *token.FileSet, f *ast.File, n ast.Node) bo
123123
}
124124
// the "`" is special
125125
if s[0] == '`' {
126-
// replace inner " with \"
127-
s = strings.Replace(s, "\"", "\\\"", -1)
128-
// replace \n with \\n
129-
s = strings.Replace(s, "\n", "\\n", -1)
126+
// keep escaped ", replace inner " with \", replace \n with \\n
127+
rep := strings.NewReplacer(`\"`, `\"`, `"`, `\"`, "\n", "\\n")
128+
s = rep.Replace(s)
130129
}
131130
// strip leading and trailing " (or `)
132131
s = s[1 : len(s)-1]

i18n/xgettext-go/main_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -513,3 +513,26 @@ msgstr ""
513513
c.Check(out.String(), Equals, expected)
514514

515515
}
516+
517+
func (s *xgettextTestSuite) TestDontEscapeAlreadyEscapedQuoteInBacktick(c *C) {
518+
fname := makeGoSourceFile(c, []byte(`package main
519+
520+
func main() {
521+
i18n.G(`+"`"+`Some text: "{\"key\":\"value\"}"`+"`"+`)
522+
}
523+
`))
524+
525+
err := processFiles([]string{fname})
526+
c.Assert(err, IsNil)
527+
528+
out := bytes.NewBuffer([]byte(""))
529+
writePotFile(out)
530+
531+
expected := fmt.Sprintf(`%s
532+
#: %[2]s:4
533+
msgid "Some text: \"{\"key\":\"value\"}\""
534+
msgstr ""
535+
536+
`, header, fname)
537+
c.Check(out.String(), Equals, expected)
538+
}

0 commit comments

Comments
 (0)