diff options
author | Nick Snyder <nickdsnyder@gmail.com> | 2019-05-02 09:43:17 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-02 09:43:17 +0300 |
commit | b2843f1401bcdec49ebbd5b729689ee36ea1e65a (patch) | |
tree | 040d4930a7859185628fa7a45b479db7aab4e053 | |
parent | 45eb7b31b500b0b87b086663f2494374a0fae70b (diff) |
escape extracted strings correctly (#165)
-rw-r--r-- | v2/goi18n/extract_command.go | 6 | ||||
-rw-r--r-- | v2/goi18n/extract_command_test.go | 61 |
2 files changed, 25 insertions, 42 deletions
diff --git a/v2/goi18n/extract_command.go b/v2/goi18n/extract_command.go index 05774cc..0ea8411 100644 --- a/v2/goi18n/extract_command.go +++ b/v2/goi18n/extract_command.go @@ -195,7 +195,11 @@ func extractStringLiteral(expr ast.Expr) (string, bool) { if v.Kind != token.STRING { return "", false } - return v.Value[1 : len(v.Value)-1], true + s := v.Value[1 : len(v.Value)-1] + if v.Value[0] == '"' { + s = strings.Replace(s, `\"`, `"`, -1) + } + return s, true case *ast.BinaryExpr: if v.Op != token.ADD { return "", false diff --git a/v2/goi18n/extract_command_test.go b/v2/goi18n/extract_command_test.go index 624ff59..2e3ae94 100644 --- a/v2/goi18n/extract_command_test.go +++ b/v2/goi18n/extract_command_test.go @@ -6,8 +6,6 @@ import ( "os" "path/filepath" "testing" - - "github.com/nicksnyder/go-i18n/v2/i18n" ) func TestExtract(t *testing.T) { @@ -16,14 +14,12 @@ func TestExtract(t *testing.T) { name string fileName string file string - messages []*i18n.Message activeFile []byte }{ { name: "no translations", fileName: "file.go", file: `package main`, - messages: nil, }, { name: "global declaration", @@ -36,11 +32,26 @@ func TestExtract(t *testing.T) { ID: "Plural ID", } `, - messages: []*i18n.Message{ - { - ID: "Plural ID", - }, - }, + }, + { + name: "escape", + fileName: "file.go", + file: `package main + + import "github.com/nicksnyder/go-i18n/v2/i18n" + + var a = &i18n.Message{ + ID: "a", + Other: "a \" b", + } + var b = &i18n.Message{ + ID: "b", + Other: ` + "`" + `a " b` + "`" + `, + } + `, + activeFile: []byte(`a = "a \" b" +b = "a \" b" +`), }, { name: "no extract from test", @@ -55,11 +66,6 @@ func TestExtract(t *testing.T) { l.Localize(&i18n.LocalizeConfig{MessageID: "Plural ID"}) } `, - messages: []*i18n.Message{ - { - ID: "Plural ID", - }, - }, }, { name: "must short form id only", @@ -74,11 +80,6 @@ func TestExtract(t *testing.T) { l.MustLocalize(&i18n.LocalizeConfig{MessageID: "Plural ID"}) } `, - messages: []*i18n.Message{ - { - ID: "Plural ID", - }, - }, }, { name: "custom package name", @@ -93,11 +94,6 @@ func TestExtract(t *testing.T) { } } `, - messages: []*i18n.Message{ - { - ID: "Plural ID", - }, - }, }, { name: "exhaustive plural translation", @@ -119,18 +115,6 @@ func TestExtract(t *testing.T) { } } `, - messages: []*i18n.Message{ - { - ID: "Plural ID", - Description: "Plural description", - Zero: "Zero translation", - One: "One translation", - Two: "Two translation", - Few: "Few translation", - Many: "Many translation", - Other: "Other translation", - }, - }, activeFile: []byte(`["Plural ID"] description = "Plural description" few = "Few translation" @@ -156,11 +140,6 @@ zero = "Zero translation" } } `, - messages: []*i18n.Message{ - { - ID: "Plural ID", - }, - }, }, } |