message_formatter.h 4.4 KB

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