Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/gohugoio/go-i18n.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Snyder <nickdsnyder@gmail.com>2021-01-18 22:54:34 +0300
committerGitHub <noreply@github.com>2021-01-18 22:54:34 +0300
commitc587b0dfc11a01d42c0c5f9cea07d040f7a10788 (patch)
tree7f8b6981e49cff59f1d417b8621f15b5d1bb2dde
parenta5b9f57ef0d4f3ea2277b1c9c3462c3e312e7a15 (diff)
fix escape sequences when extracting string literals (#244)v2.1.2
-rw-r--r--v2/goi18n/extract_command.go7
-rw-r--r--v2/goi18n/extract_command_test.go15
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