Skip to content

Commit c587b0d

Browse files
authored
fix escape sequences when extracting string literals (nicksnyder#244)
1 parent a5b9f57 commit c587b0d

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

‎v2/goi18n/extract_command.go‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"io/ioutil"
1010
"os"
1111
"path/filepath"
12+
"strconv"
1213
"strings"
1314

1415
"github.com/nicksnyder/go-i18n/v2/i18n"
@@ -240,9 +241,9 @@ func extractStringLiteral(expr ast.Expr) (string, bool) {
240241
if v.Kind != token.STRING {
241242
return "", false
242243
}
243-
s := v.Value[1 : len(v.Value)-1]
244-
if v.Value[0] == '"' {
245-
s = strings.Replace(s, `\"`, `"`, -1)
244+
s, err := strconv.Unquote(v.Value)
245+
if err != nil {
246+
return "", false
246247
}
247248
return s, true
248249
case *ast.BinaryExpr:

‎v2/goi18n/extract_command_test.go‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,21 @@ func TestExtract(t *testing.T) {
3333
}
3434
`,
3535
},
36+
{
37+
name: "escape newline",
38+
fileName: "file.go",
39+
file: `package main
40+
41+
import "github.com/nicksnyder/go-i18n/v2/i18n"
42+
43+
var hasnewline = &i18n.Message{
44+
ID: "hasnewline",
45+
Other: "\nfoo\nbar\\",
46+
}
47+
`,
48+
activeFile: []byte(`hasnewline = "\nfoo\nbar\\"
49+
`),
50+
},
3651
{
3752
name: "escape",
3853
fileName: "file.go",

0 commit comments

Comments
 (0)