doc.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 message implements formatted I/O for localized strings with functions
  5. // analogous to the fmt's print functions. It is a drop-in replacement for fmt.
  6. //
  7. // # Localized Formatting
  8. //
  9. // A format string can be localized by replacing any of the print functions of
  10. // fmt with an equivalent call to a Printer.
  11. //
  12. // p := message.NewPrinter(message.MatchLanguage("en"))
  13. // p.Println(123456.78) // Prints 123,456.78
  14. //
  15. // p.Printf("%d ducks in a row", 4331) // Prints 4,331 ducks in a row
  16. //
  17. // p := message.NewPrinter(message.MatchLanguage("nl"))
  18. // p.Printf("Hoogte: %.1f meter", 1244.9) // Prints Hoogte: 1,244.9 meter
  19. //
  20. // p := message.NewPrinter(message.MatchLanguage("bn"))
  21. // p.Println(123456.78) // Prints ১,২৩,৪৫৬.৭৮
  22. //
  23. // Printer currently supports numbers and specialized types for which packages
  24. // exist in x/text. Other builtin types such as time.Time and slices are
  25. // planned.
  26. //
  27. // Format strings largely have the same meaning as with fmt with the following
  28. // notable exceptions:
  29. // - flag # always resorts to fmt for printing
  30. // - verb 'f', 'e', 'g', 'd' use localized formatting unless the '#' flag is
  31. // specified.
  32. // - verb 'm' inserts a translation of a string argument.
  33. //
  34. // See package fmt for more options.
  35. //
  36. // # Translation
  37. //
  38. // The format strings that are passed to Printf, Sprintf, Fprintf, or Errorf
  39. // are used as keys to look up translations for the specified languages.
  40. // More on how these need to be specified below.
  41. //
  42. // One can use arbitrary keys to distinguish between otherwise ambiguous
  43. // strings:
  44. //
  45. // p := message.NewPrinter(language.English)
  46. // p.Printf("archive(noun)") // Prints "archive"
  47. // p.Printf("archive(verb)") // Prints "archive"
  48. //
  49. // p := message.NewPrinter(language.German)
  50. // p.Printf("archive(noun)") // Prints "Archiv"
  51. // p.Printf("archive(verb)") // Prints "archivieren"
  52. //
  53. // To retain the fallback functionality, use Key:
  54. //
  55. // p.Printf(message.Key("archive(noun)", "archive"))
  56. // p.Printf(message.Key("archive(verb)", "archive"))
  57. //
  58. // # Translation Pipeline
  59. //
  60. // Format strings that contain text need to be translated to support different
  61. // locales. The first step is to extract strings that need to be translated.
  62. //
  63. // 1. Install gotext
  64. //
  65. // go get -u golang.org/x/text/cmd/gotext
  66. // gotext -help
  67. //
  68. // 2. Mark strings in your source to be translated by using message.Printer,
  69. // instead of the functions of the fmt package.
  70. //
  71. // 3. Extract the strings from your source
  72. //
  73. // gotext extract
  74. //
  75. // The output will be written to the textdata directory.
  76. //
  77. // 4. Send the files for translation
  78. //
  79. // It is planned to support multiple formats, but for now one will have to
  80. // rewrite the JSON output to the desired format.
  81. //
  82. // 5. Inject translations into program
  83. //
  84. // 6. Repeat from 2
  85. //
  86. // Right now this has to be done programmatically with calls to Set or
  87. // SetString. These functions as well as the methods defined in
  88. // see also package golang.org/x/text/message/catalog can be used to implement
  89. // either dynamic or static loading of messages.
  90. //
  91. // # Plural and Gender Forms
  92. //
  93. // Translated messages can vary based on the plural and gender forms of
  94. // substitution values. In general, it is up to the translators to provide
  95. // alternative translations for such forms. See the packages in
  96. // golang.org/x/text/feature and golang.org/x/text/message/catalog for more
  97. // information.
  98. package message