Add ICU message format support
[chromium-blink-merge.git] / base / i18n / message_formatter.h
blobbcdc3bc97752873a26e2a322978999753d169f24
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_
8 #include <stdint.h>
9 #include <string>
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"
17 U_NAMESPACE_BEGIN
18 class Formattable;
19 U_NAMESPACE_END
21 namespace base {
23 class Time;
25 namespace i18n {
27 class MessageFormatter;
29 namespace internal {
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();
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);
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
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.
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());
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());
103 private:
104 MessageFormatter() {}
105 DISALLOW_COPY_AND_ASSIGN(MessageFormatter);
108 } // namespace i18n
109 } // namespace base
111 #endif // BASE_I18N_MESSAGE_FORMATTER_H_