Skip to content

Commit b2843f1

Browse files
authored
escape extracted strings correctly (#165)
1 parent 45eb7b3 commit b2843f1

File tree

2 files changed

+25
-42
lines changed

2 files changed

+25
-42
lines changed

v2/goi18n/extract_command.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,11 @@ func extractStringLiteral(expr ast.Expr) (string, bool) {
195195
if v.Kind != token.STRING {
196196
return "", false
197197
}
198-
return v.Value[1 : len(v.Value)-1], true
198+
s := v.Value[1 : len(v.Value)-1]
199+
if v.Value[0] == '"' {
200+
s = strings.Replace(s, `\"`, `"`, -1)
201+
}
202+
return s, true
199203
case *ast.BinaryExpr:
200204
if v.Op != token.ADD {
201205
return "", false

v2/goi18n/extract_command_test.go

+20-41
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"os"
77
"path/filepath"
88
"testing"
9-
10-
"github.com/nicksnyder/go-i18n/v2/i18n"
119
)
1210

1311
func TestExtract(t *testing.T) {
@@ -16,14 +14,12 @@ func TestExtract(t *testing.T) {
1614
name string
1715
fileName string
1816
file string
19-
messages []*i18n.Message
2017
activeFile []byte
2118
}{
2219
{
2320
name: "no translations",
2421
fileName: "file.go",
2522
file: `package main`,
26-
messages: nil,
2723
},
2824
{
2925
name: "global declaration",
@@ -36,11 +32,26 @@ func TestExtract(t *testing.T) {
3632
ID: "Plural ID",
3733
}
3834
`,
39-
messages: []*i18n.Message{
40-
{
41-
ID: "Plural ID",
42-
},
43-
},
35+
},
36+
{
37+
name: "escape",
38+
fileName: "file.go",
39+
file: `package main
40+
41+
import "github.com/nicksnyder/go-i18n/v2/i18n"
42+
43+
var a = &i18n.Message{
44+
ID: "a",
45+
Other: "a \" b",
46+
}
47+
var b = &i18n.Message{
48+
ID: "b",
49+
Other: ` + "`" + `a " b` + "`" + `,
50+
}
51+
`,
52+
activeFile: []byte(`a = "a \" b"
53+
b = "a \" b"
54+
`),
4455
},
4556
{
4657
name: "no extract from test",
@@ -55,11 +66,6 @@ func TestExtract(t *testing.T) {
5566
l.Localize(&i18n.LocalizeConfig{MessageID: "Plural ID"})
5667
}
5768
`,
58-
messages: []*i18n.Message{
59-
{
60-
ID: "Plural ID",
61-
},
62-
},
6369
},
6470
{
6571
name: "must short form id only",
@@ -74,11 +80,6 @@ func TestExtract(t *testing.T) {
7480
l.MustLocalize(&i18n.LocalizeConfig{MessageID: "Plural ID"})
7581
}
7682
`,
77-
messages: []*i18n.Message{
78-
{
79-
ID: "Plural ID",
80-
},
81-
},
8283
},
8384
{
8485
name: "custom package name",
@@ -93,11 +94,6 @@ func TestExtract(t *testing.T) {
9394
}
9495
}
9596
`,
96-
messages: []*i18n.Message{
97-
{
98-
ID: "Plural ID",
99-
},
100-
},
10197
},
10298
{
10399
name: "exhaustive plural translation",
@@ -119,18 +115,6 @@ func TestExtract(t *testing.T) {
119115
}
120116
}
121117
`,
122-
messages: []*i18n.Message{
123-
{
124-
ID: "Plural ID",
125-
Description: "Plural description",
126-
Zero: "Zero translation",
127-
One: "One translation",
128-
Two: "Two translation",
129-
Few: "Few translation",
130-
Many: "Many translation",
131-
Other: "Other translation",
132-
},
133-
},
134118
activeFile: []byte(`["Plural ID"]
135119
description = "Plural description"
136120
few = "Few translation"
@@ -156,11 +140,6 @@ zero = "Zero translation"
156140
}
157141
}
158142
`,
159-
messages: []*i18n.Message{
160-
{
161-
ID: "Plural ID",
162-
},
163-
},
164143
},
165144
}
166145

0 commit comments

Comments
 (0)