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