| 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 |
import "golang.org/x/text/message" |
| 8 |
|
| 9 |
func main() { |
| 10 |
p := message.NewPrinter(message.MatchLanguage("en")) |
| 11 |
|
| 12 |
// NOT EXTRACTED: strings passed to Println are not extracted. |
| 13 |
p.Println("Hello world!") |
| 14 |
|
| 15 |
// NOT EXTRACTED: strings passed to Print are not extracted. |
| 16 |
p.Print("Hello world!\n") |
| 17 |
|
| 18 |
// Extract and trim whitespace (TODO). |
| 19 |
p.Printf("Hello world!\n") |
| 20 |
|
| 21 |
// NOT EXTRACTED: city is not used as a pattern or passed to %m. |
| 22 |
city := "Amsterdam" |
| 23 |
// This comment is extracted. |
| 24 |
p.Printf("Hello %s!\n", city) |
| 25 |
|
| 26 |
person := "Sheila" |
| 27 |
place := "Zürich" |
| 28 |
|
| 29 |
// Substitutions replaced by variable names. |
| 30 |
p.Printf("%s is visiting %s!\n", |
| 31 |
person, // The person of matter. |
| 32 |
place, // Place the person is visiting. |
| 33 |
) |
| 34 |
|
| 35 |
pp := struct { |
| 36 |
Person string // The person of matter. // TODO: get this comment. |
| 37 |
Place string |
| 38 |
extra int |
| 39 |
}{ |
| 40 |
person, place, 4, |
| 41 |
} |
| 42 |
|
| 43 |
// extract will drop this comment in favor of the one below. |
| 44 |
p.Printf("%[1]s is visiting %[3]s!\n", // Field names are placeholders. |
| 45 |
pp.Person, |
| 46 |
pp.extra, |
| 47 |
pp.Place, // Place the person is visiting. |
| 48 |
) |
| 49 |
|
| 50 |
// Numeric literal becomes placeholder. |
| 51 |
p.Printf("%d files remaining!", 2) |
| 52 |
|
| 53 |
const n = 2 |
| 54 |
|
| 55 |
// Constant identifier becomes placeholder. |
| 56 |
p.Printf("%d more files remaining!", n) |
| 57 |
|
| 58 |
// Infer better names from type names. |
| 59 |
type referralCode int |
| 60 |
|
| 61 |
const c = referralCode(5) |
| 62 |
|
| 63 |
// Use type name as placeholder. |
| 64 |
p.Printf("Use the following code for your discount: %d\n", c) |
| 65 |
|
| 66 |
// Use constant name as message ID. |
| 67 |
const msgOutOfOrder = "%s is out of order!" // This comment wins. |
| 68 |
const device = "Soda machine" |
| 69 |
// This message has two IDs. |
| 70 |
p.Printf(msgOutOfOrder, device) |
| 71 |
|
| 72 |
// Multiple substitutions for same argument. |
| 73 |
miles := 1.2345 |
| 74 |
p.Printf("%.2[1]f miles traveled (%[1]f)", miles) |
| 75 |
} |