OLD | NEW |
(Empty) | |
| 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 |
| 5 #ifndef BASE_I18N_MESSAGE_FORMATTER_H_ |
| 6 #define BASE_I18N_MESSAGE_FORMATTER_H_ |
| 7 |
| 8 #include <stdint.h> |
| 9 #include <string> |
| 10 |
| 11 #include "base/i18n/base_i18n_export.h" |
| 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/strings/string16.h" |
| 14 #include "base/strings/string_piece.h" |
| 15 #include "third_party/icu/source/common/unicode/uversion.h" |
| 16 |
| 17 U_NAMESPACE_BEGIN |
| 18 class Formattable; |
| 19 U_NAMESPACE_END |
| 20 |
| 21 namespace base { |
| 22 |
| 23 class Time; |
| 24 |
| 25 namespace i18n { |
| 26 |
| 27 class MessageFormatter; |
| 28 |
| 29 namespace internal { |
| 30 |
| 31 class BASE_I18N_EXPORT MessageArg { |
| 32 public: |
| 33 MessageArg(const char* s); |
| 34 MessageArg(StringPiece s); |
| 35 MessageArg(const std::string& s); |
| 36 MessageArg(const string16& s); |
| 37 MessageArg(int i); |
| 38 MessageArg(int64_t i); |
| 39 MessageArg(double d); |
| 40 MessageArg(const Time& t); |
| 41 ~MessageArg(); |
| 42 |
| 43 private: |
| 44 friend class base::i18n::MessageFormatter; |
| 45 MessageArg(); |
| 46 // Tests if this argument has a value, and if so increments *count. |
| 47 bool has_value(int* count) const; |
| 48 scoped_ptr<icu::Formattable> formattable; |
| 49 DISALLOW_COPY_AND_ASSIGN(MessageArg); |
| 50 }; |
| 51 |
| 52 } // namespace internal |
| 53 |
| 54 // Message Formatter with the ICU message format syntax support. |
| 55 // It can format strings (UTF-8 and UTF-16), numbers and base::Time with |
| 56 // plural, gender and other 'selectors' support. This is handy if you |
| 57 // have multiple parameters of differnt types and some of them require |
| 58 // plural or gender/selector support. |
| 59 // |
| 60 // To use this API for locale-sensitive formatting, retrieve a 'message |
| 61 // template' in the ICU message format from a message bundle (e.g. with |
| 62 // l10n_util::GetStringUTF16()) and pass it to FormatWith{Named,Numbered}Args. |
| 63 // |
| 64 // MessageFormat specs: |
| 65 // http://icu-project.org/apiref/icu4j/com/ibm/icu/text/MessageFormat.html |
| 66 // http://icu-project.org/apiref/icu4c/classicu_1_1DecimalFormat.html#details |
| 67 // Examples: |
| 68 // http://userguide.icu-project.org/formatparse/messages |
| 69 // message_formatter_unittest.cc |
| 70 // go/plurals inside Google. |
| 71 // TODO(jshin): Document this API at sites.chromium.org and add a reference |
| 72 // here. |
| 73 |
| 74 class BASE_I18N_EXPORT MessageFormatter { |
| 75 public: |
| 76 static string16 FormatWithNamedArgs( |
| 77 StringPiece16 msg, |
| 78 StringPiece name0 = StringPiece(), |
| 79 const internal::MessageArg& arg0 = internal::MessageArg(), |
| 80 StringPiece name1 = StringPiece(), |
| 81 const internal::MessageArg& arg1 = internal::MessageArg(), |
| 82 StringPiece name2 = StringPiece(), |
| 83 const internal::MessageArg& arg2 = internal::MessageArg(), |
| 84 StringPiece name3 = StringPiece(), |
| 85 const internal::MessageArg& arg3 = internal::MessageArg(), |
| 86 StringPiece name4 = StringPiece(), |
| 87 const internal::MessageArg& arg4 = internal::MessageArg(), |
| 88 StringPiece name5 = StringPiece(), |
| 89 const internal::MessageArg& arg5 = internal::MessageArg(), |
| 90 StringPiece name6 = StringPiece(), |
| 91 const internal::MessageArg& arg6 = internal::MessageArg()); |
| 92 |
| 93 static string16 FormatWithNumberedArgs( |
| 94 StringPiece16 msg, |
| 95 const internal::MessageArg& arg0 = internal::MessageArg(), |
| 96 const internal::MessageArg& arg1 = internal::MessageArg(), |
| 97 const internal::MessageArg& arg2 = internal::MessageArg(), |
| 98 const internal::MessageArg& arg3 = internal::MessageArg(), |
| 99 const internal::MessageArg& arg4 = internal::MessageArg(), |
| 100 const internal::MessageArg& arg5 = internal::MessageArg(), |
| 101 const internal::MessageArg& arg6 = internal::MessageArg()); |
| 102 |
| 103 private: |
| 104 MessageFormatter() {} |
| 105 DISALLOW_COPY_AND_ASSIGN(MessageFormatter); |
| 106 }; |
| 107 |
| 108 } // namespace i18n |
| 109 } // namespace base |
| 110 |
| 111 #endif // BASE_I18N_MESSAGE_FORMATTER_H_ |
OLD | NEW |