diff options
author | Nick Snyder <nickdsnyder@gmail.com> | 2021-01-18 22:54:34 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-18 22:54:34 +0300 |
commit | c587b0dfc11a01d42c0c5f9cea07d040f7a10788 (patch) | |
tree | 7f8b6981e49cff59f1d417b8621f15b5d1bb2dde | |
parent | a5b9f57ef0d4f3ea2277b1c9c3462c3e312e7a15 (diff) |
fix escape sequences when extracting string literals (#244)v2.1.2
-rw-r--r-- | v2/goi18n/extract_command.go | 7 | ||||
-rw-r--r-- | v2/goi18n/extract_command_test.go | 15 |
2 files changed, 19 insertions, 3 deletions
diff --git a/v2/goi18n/extract_command.go b/v2/goi18n/extract_command.go index 528d617..67afab4 100644 --- a/v2/goi18n/extract_command.go +++ b/v2/goi18n/extract_command.go @@ -9,6 +9,7 @@ import ( "io/ioutil" "os" "path/filepath" + "strconv" "strings" "github.com/nicksnyder/go-i18n/v2/i18n" @@ -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: diff --git a/v2/goi18n/extract_command_test.go b/v2/goi18n/extract_command_test.go index cab9061..3cfe581 100644 --- a/v2/goi18n/extract_command_test.go +++ b/v2/goi18n/extract_command_test.go @@ -34,6 +34,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", file: `package main |