10000 cmd/gotext: improved extraction · golang/text@e204928 · GitHub
[go: up one dir, main page]

Skip to content

Commit e204928

Browse files
committed
cmd/gotext: improved extraction
- split extraction and translation data in two types - rewrite fmt strings into translator-readable format - more intelligent name picking, for instance: - use variable name for placeholders - if var is too short, use type name, if it differs from the underlying type. Change-Id: I80ae9c165892491df6fcedc340d56a08269f47fe Reviewed-on: https://go-review.googlesource.com/79237 Run-TryBot: Marcel van Lohuizen <mpvl@golang.org> Reviewed-by: Nigel Tao <nigeltao@golang.org>
1 parent 556d234 commit e204928

File tree

4 files changed

+399
-114
lines changed

4 files changed

+399
-114
lines changed

cmd/gotext/examples/main.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
//go:generate gotext extract
8+
9+
import (
10+
"golang.org/x/text/language"
11+
"golang.org/x/text/message"
12+
)
13+
14+
func main() {
15+
p := message.NewPrinter(language.English)
16+
17+
p.Print("Hello world!\n")
18+
19+
p.Println("Hello", "world!")
20+
21+
person := "Sheila"
22+
place := "Zürich"
23+
24+
p.Print("Hello ", person, " in ", place, "!\n")
25+
26+
// Greet a city.
27+
p.Print("Hello city!\n")
28+
29+
city := "Amsterdam"
30+
// Greet a city.
31+
p.Printf("Hello %s!\n", city)
32+
33+
town := "Amsterdam"
34+
// Greet a town.
35+
p.Printf("Hello %s!\n",
36+
town, // Town
37+
)
38+
39+
// Person visiting a place.
40+
p.Printf("%s is visiting %s!\n",
41+
person, // The person of matter.
42+
place, // Place the person is visiting.
43+
)
44+
45+
pp := struct {
46+
Person string // The person of matter. // TODO: get this comment.
47+
Place string
48+
}{
49+
person, place,
50+
}
51+
52+
// extract will drop this comment in favor of the one below.
53+
p.Printf("%s is visiting %s!\n", // Person visiting a place.
54+
pp.Person,
55+
pp.Place, // Place the person is visiting.
56+
)
57+
58+
// Numeric literal
59+
p.Printf("%d files remaining!", 2)
60+
61+
const n = 2
62+
63+
// Numeric var
64+
p.Printf("%d more files remaining!", n)
65+
66+
type referralCode int
67+
68+
c := referralCode(5)
69+
p.Printf("Use the following code for your discount: %d\n", c)
70+
}
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
[
2+
{
3+
"key": [
4+
"Hello %s!\n"
5+
],
6+
"message": {
7+
"msg": "Hello {City}!\n"
8+
},
9+
"args": [
10+
{
11+
"id": "City",
12+
"argNum": 1,
13+
"format": [
14+
"%s"
15+
],
16+
"type": "string",
17+
"underlyingType": "string",
18+
"expr": "city",
19+
"position": "golang.org/x/text/cmd/gotext/examples/main.go:31:26"
20+
}
21+
],
22+
"position": "golang.org/x/text/cmd/gotext/examples/main.go:31:10"
23+
},
24+
{
25+
"key": [
26+
"Hello %s!\n"
27+
],
28+
"message": {
29+
"msg": "Hello {Town}!\n"
30+
},
31+
"args": [
32+
{
33+
"id": "Town",
34+
"argNum": 1,
35+
"format": [
36+
"%s"
37+
],
38+
"type": "string",
39+
"underlyingType": "string",
40+
"expr": "town",
41+
"comment": "Town",
42+
"position": "golang.org/x/text/cmd/gotext/examples/main.go:36:3"
43+
}
44+
],
45+
"position": "golang.org/x/text/cmd/gotext/examples/main.go:35:10"
46+
},
47+
{
48+
"key": [
49+
"%s is visiting %s!\n"
50+
],
51+
"message": {
52+
"msg": "{Person} is visiting {Place}!\n"
53+
},
54+
"args": [
55+
{
56+
"id": "Person",
57+
"argNum": 1,
58+
"format": [
59+
"%s"
60+
],
61+
"type": "string",
62+
"underlyingType": "string",
63+
"expr": "person",
64+
"comment": "The person of matter.",
65+
"position": "golang.org/x/text/cmd/gotext/examples/main.go:41:3"
66+
},
67+
{
68+
"id": "Place",
69+
"argNum": 2,
70+
"format": [
71+
"%s"
72+
],
73+
"type": "string",
74+
"underlyingType": "string",
75+
"expr": "place",
76+
"comment": "Place the person is visiting.",
77+
"position": "golang.org/x/text/cmd/gotext/examples/main.go:42:3"
78+
}
79+
],
80+
"position": "golang.org/x/text/cmd/gotext/examples/main.go:40:10"
81+
},
82+
{
83+
"key": [
84+
"%s is visiting %s!\n"
85+
],
86+
"message": {
87+
"msg": "{Person} is visiting {Place}!\n"
88+
},
89+
"comment": "Person visiting a place.",
90+
"args": [
91+
{
92+
"id": "Person",
93+
"argNum": 1,
94+
"format": [
95+
"%s"
96+
],
97+
"type": "string",
98+
"underlyingType": "string",
99+
"expr": "pp.Person",
100+
"position": "golang.org/x/text/cmd/gotext/examples/main.go:54:3"
101+
},
102+
{
103+
"id": "Place",
104+
"argNum": 2,
105+
"format": [
106+
"%s"
107+
],
108+
"type": "string",
109+
"underlyingType": "string",
110+
"expr": "pp.Place",
111+
"comment": "Place the person is visiting.",
112+
"position": "golang.org/x/text/cmd/gotext/examples/main.go:55:3"
113+
}
114+
],
115+
"position": "golang.org/x/text/cmd/gotext/examples/main.go:53:10"
116+
},
117+
{
118+
"key": [
119+
"%d files remaining!"
120+
],
121+
"message": {
122+
"msg": "{2} files remaining!"
123+
},
124+
"args": [
125+
{
126+
"id": "2",
127+
"argNum": 1,
128+
"format": [
129+
"%d"
130+
],
131+
"type": "int",
132+
"underlyingType": "int",
133+
"expr": "2",
134+
"value": "2",
135+
"position": "golang.org/x/text/cmd/gotext/examples/main.go:59:34"
136+
}
137+
],
138+
"position": "golang.org/x/text/cmd/gotext/examples/main.go:59:10"
139+
},
140+
{
141+
"key": [
142+
"%d more files remaining!"
143+
],
144+
"message": {
145+
"msg": "{N} more files remaining!"
146+
},
147+
"args": [
148+
{
149+
"id": "N",
150+
"argNum": 1,
151+
"format": [
152+
"%d"
153+
],
154+
"type": "int",
155+
"underlyingType": "int",
156+
"expr": "n",
157+
"value": "2",
158+
"position": "golang.org/x/text/cmd/gotext/examples/main.go:64:39"
159+
}
160+
],
161+
"position": "golang.org/x/text/cmd/gotext/examples/main.go:64:10"
162+
},
163+
{
164+
"key": [
165+
"Use the following code for your discount: %d\n"
166+
],
167+
"message": {
168+
"msg": "Use the following code for your discount: {ReferralCode}\n"
169+
},
170+
"args": [
171+
{
172+
"id": "ReferralCode",
173+
"argNum": 1,
174+
"format": [
175+
"%d"
176+
],
177+
"type": "golang.org/x/text/cmd/gotext/examples.referralCode",
178+
"underlyingType": "int",
179+
"expr": "c",
180+
"position": "golang.org/x/text/cmd/gotext/examples/main.go:69:61"
181+
}
182+
],
183+
"position": "golang.org/x/text/cmd/gotext/examples/main.go:69:10"
184+
}
185+
]

0 commit comments

Comments
 (0)
0