test1.go 1.9 KB

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