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.
5 #ifndef BASE_I18N_MESSAGE_FORMATTER_H_
6 #define BASE_I18N_MESSAGE_FORMATTER_H_
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"
27 class MessageFormatter
;
31 class BASE_I18N_EXPORT MessageArg
{
33 MessageArg(const char* s
);
34 MessageArg(StringPiece s
);
35 MessageArg(const std::string
& s
);
36 MessageArg(const string16
& s
);
38 MessageArg(int64_t i
);
40 MessageArg(const Time
& t
);
44 friend class base::i18n::MessageFormatter
;
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
);
52 } // namespace internal
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.
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.
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
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
74 class BASE_I18N_EXPORT MessageFormatter
{
76 static string16
FormatWithNamedArgs(
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());
93 static string16
FormatWithNumberedArgs(
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());
104 MessageFormatter() {}
105 DISALLOW_COPY_AND_ASSIGN(MessageFormatter
);
111 #endif // BASE_I18N_MESSAGE_FORMATTER_H_