message_formatter.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_I18N_MESSAGE_FORMATTER_H_
  5. #define BASE_I18N_MESSAGE_FORMATTER_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <string>
  9. #include "base/i18n/base_i18n_export.h"
  10. #include "base/macros.h"
  11. #include "base/strings/string16.h"
  12. #include "base/strings/string_piece.h"
  13. #include "third_party/icu/source/common/unicode/uversion.h"
  14. U_NAMESPACE_BEGIN
  15. class Formattable;
  16. U_NAMESPACE_END
  17. namespace base {
  18. class Time;
  19. namespace i18n {
  20. class MessageFormatter;
  21. namespace internal {
  22. class BASE_I18N_EXPORT MessageArg {
  23. public:
  24. MessageArg(const char* s);
  25. MessageArg(StringPiece s);
  26. MessageArg(const std::string& s);
  27. MessageArg(const string16& s);
  28. MessageArg(int i);
  29. MessageArg(int64_t i);
  30. MessageArg(double d);
  31. MessageArg(const Time& t);
  32. ~MessageArg();
  33. private:
  34. friend class base::i18n::MessageFormatter;
  35. MessageArg();
  36. // Tests if this argument has a value, and if so increments *count.
  37. bool has_value(int* count) const;
  38. std::unique_ptr<icu::Formattable> formattable;
  39. DISALLOW_COPY_AND_ASSIGN(MessageArg);
  40. };
  41. } // namespace internal
  42. // Message Formatter with the ICU message format syntax support.
  43. // It can format strings (UTF-8 and UTF-16), numbers and base::Time with
  44. // plural, gender and other 'selectors' support. This is handy if you
  45. // have multiple parameters of differnt types and some of them require
  46. // plural or gender/selector support.
  47. //
  48. // To use this API for locale-sensitive formatting, retrieve a 'message
  49. // template' in the ICU message format from a message bundle (e.g. with
  50. // l10n_util::GetStringUTF16()) and pass it to FormatWith{Named,Numbered}Args.
  51. //
  52. // MessageFormat specs:
  53. // http://icu-project.org/apiref/icu4j/com/ibm/icu/text/MessageFormat.html
  54. // http://icu-project.org/apiref/icu4c/classicu_1_1DecimalFormat.html#details
  55. // Examples:
  56. // http://userguide.icu-project.org/formatparse/messages
  57. // message_formatter_unittest.cc
  58. // go/plurals inside Google.
  59. // TODO(jshin): Document this API in md format docs.
  60. // Caveat:
  61. // When plural/select/gender is used along with other format specifiers such
  62. // as date or number, plural/select/gender should be at the top level. It's
  63. // not an ICU restriction but a constraint imposed by Google's translation
  64. // infrastructure. Message A does not work. It must be revised to Message B.
  65. //
  66. // A.
  67. // Rated <ph name="RATING">{0, number,0.0}<ex>3.2</ex></ph>
  68. // by {1, plural, =1{a user} other{# users}}
  69. //
  70. // B.
  71. // {1, plural,
  72. // =1{Rated <ph name="RATING">{0, number,0.0}<ex>3.2</ex></ph>
  73. // by a user.}
  74. // other{Rated <ph name="RATING">{0, number,0.0}<ex>3.2</ex></ph>
  75. // by # users.}}
  76. class BASE_I18N_EXPORT MessageFormatter {
  77. public:
  78. static string16 FormatWithNamedArgs(
  79. StringPiece16 msg,
  80. StringPiece name0 = StringPiece(),
  81. const internal::MessageArg& arg0 = internal::MessageArg(),
  82. StringPiece name1 = StringPiece(),
  83. const internal::MessageArg& arg1 = internal::MessageArg(),
  84. StringPiece name2 = StringPiece(),
  85. const internal::MessageArg& arg2 = internal::MessageArg(),
  86. StringPiece name3 = StringPiece(),
  87. const internal::MessageArg& arg3 = internal::MessageArg(),
  88. StringPiece name4 = StringPiece(),
  89. const internal::MessageArg& arg4 = internal::MessageArg(),
  90. StringPiece name5 = StringPiece(),
  91. const internal::MessageArg& arg5 = internal::MessageArg(),
  92. StringPiece name6 = StringPiece(),
  93. const internal::MessageArg& arg6 = internal::MessageArg());
  94. static string16 FormatWithNumberedArgs(
  95. StringPiece16 msg,
  96. const internal::MessageArg& arg0 = internal::MessageArg(),
  97. const internal::MessageArg& arg1 = internal::MessageArg(),
  98. const internal::MessageArg& arg2 = internal::MessageArg(),
  99. const internal::MessageArg& arg3 = internal::MessageArg(),
  100. const internal::MessageArg& arg4 = internal::MessageArg(),
  101. const internal::MessageArg& arg5 = internal::MessageArg(),
  102. const internal::MessageArg& arg6 = internal::MessageArg());
  103. private:
  104. MessageFormatter() = delete;
  105. DISALLOW_COPY_AND_ASSIGN(MessageFormatter);
  106. };
  107. } // namespace i18n
  108. } // namespace base
  109. #endif // BASE_I18N_MESSAGE_FORMATTER_H_