Skip to content

Commit

Permalink
fix escape sequences when extracting string literals (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksnyder authored Jan 18, 2021
1 parent a5b9f57 commit c587b0d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
7 changes: 4 additions & 3 deletions v2/goi18n/extract_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/nicksnyder/go-i18n/v2/i18n"
Expand Down Expand Up @@ -240,9 +241,9 @@ func extractStringLiteral(expr ast.Expr) (string, bool) {
if v.Kind != token.STRING {
return "", false
}
s := v.Value[1 : len(v.Value)-1]
if v.Value[0] == '"' {
s = strings.Replace(s, `\"`, `"`, -1)
s, err := strconv.Unquote(v.Value)
if err != nil {
return "", false
}
return s, true
case *ast.BinaryExpr:
Expand Down
15 changes: 15 additions & 0 deletions v2/goi18n/extract_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ func TestExtract(t *testing.T) {
}
`,
},
{
name: "escape newline",
fileName: "file.go",
file: `package main
import "github.com/nicksnyder/go-i18n/v2/i18n"
var hasnewline = &i18n.Message{
ID: "hasnewline",
Other: "\nfoo\nbar\\",
}
`,
activeFile: []byte(`hasnewline = "\nfoo\nbar\\"
`),
},
{
name: "escape",
fileName: "file.go",
Expand Down

0 comments on commit c587b0d

Please sign in to comment.