diff --git a/grammar.go b/grammar.go index fe85c22..15ae392 100644 --- a/grammar.go +++ b/grammar.go @@ -2,6 +2,7 @@ package i18n import ( "maps" + "strconv" "strings" "text/template" "unicode" @@ -742,7 +743,7 @@ func renderWordOrTitle(lang, word string) string { // Quote wraps a string in double quotes. func Quote(s string) string { - return `"` + s + `"` + return strconv.Quote(s) } // ArticlePhrase prefixes a noun phrase with the correct article. diff --git a/grammar_test.go b/grammar_test.go index 99e9a3e..1d995f6 100644 --- a/grammar_test.go +++ b/grammar_test.go @@ -511,6 +511,9 @@ func TestQuote(t *testing.T) { if got := Quote("hello"); got != `"hello"` { t.Errorf("Quote(%q) = %q, want %q", "hello", got, `"hello"`) } + if got := Quote(`a "quoted" path\name`); got != `"a \"quoted\" path\\name"` { + t.Errorf("Quote(%q) = %q, want %q", `a "quoted" path\name`, got, `"a \"quoted\" path\\name"`) + } } func TestCaseHelpers(t *testing.T) {