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 #include "base/i18n/message_formatter.h"
7 #include "base/i18n/unicodestring.h"
8 #include "base/logging.h"
9 #include "base/numerics/safe_conversions.h"
10 #include "base/time/time.h"
11 #include "third_party/icu/source/common/unicode/unistr.h"
12 #include "third_party/icu/source/common/unicode/utypes.h"
13 #include "third_party/icu/source/i18n/unicode/fmtable.h"
14 #include "third_party/icu/source/i18n/unicode/msgfmt.h"
16 using icu::UnicodeString;
21 UnicodeString UnicodeStringFromStringPiece(StringPiece str) {
22 return UnicodeString::fromUTF8(
23 icu::StringPiece(str.data(), base::checked_cast<int32_t>(str.size())));
25 } // anonymous namespace
28 MessageArg::MessageArg() : formattable(nullptr) {}
30 MessageArg::MessageArg(const char* s)
31 : formattable(new icu::Formattable(UnicodeStringFromStringPiece(s))) {}
33 MessageArg::MessageArg(StringPiece s)
34 : formattable(new icu::Formattable(UnicodeStringFromStringPiece(s))) {}
36 MessageArg::MessageArg(const std::string& s)
37 : formattable(new icu::Formattable(UnicodeString::fromUTF8(s))) {}
39 MessageArg::MessageArg(const string16& s)
40 : formattable(new icu::Formattable(UnicodeString(s.data(), s.size()))) {}
42 MessageArg::MessageArg(int i) : formattable(new icu::Formattable(i)) {}
44 MessageArg::MessageArg(int64_t i) : formattable(new icu::Formattable(i)) {}
46 MessageArg::MessageArg(double d) : formattable(new icu::Formattable(d)) {}
48 MessageArg::MessageArg(const Time& t)
49 : formattable(new icu::Formattable(static_cast<UDate>(t.ToJsTime()))) {}
51 MessageArg::~MessageArg() = default;
53 // Tests if this argument has a value, and if so increments *count.
54 bool MessageArg::has_value(int *count) const {
55 if (formattable == nullptr)
62 } // namespace internal
64 string16 MessageFormatter::FormatWithNumberedArgs(
66 const internal::MessageArg& arg0,
67 const internal::MessageArg& arg1,
68 const internal::MessageArg& arg2,
69 const internal::MessageArg& arg3,
70 const internal::MessageArg& arg4,
71 const internal::MessageArg& arg5,
72 const internal::MessageArg& arg6) {
73 int32_t args_count = 0;
74 icu::Formattable args[] = {
75 arg0.has_value(&args_count) ? *arg0.formattable : icu::Formattable(),
76 arg1.has_value(&args_count) ? *arg1.formattable : icu::Formattable(),
77 arg2.has_value(&args_count) ? *arg2.formattable : icu::Formattable(),
78 arg3.has_value(&args_count) ? *arg3.formattable : icu::Formattable(),
79 arg4.has_value(&args_count) ? *arg4.formattable : icu::Formattable(),
80 arg5.has_value(&args_count) ? *arg5.formattable : icu::Formattable(),
81 arg6.has_value(&args_count) ? *arg6.formattable : icu::Formattable(),
84 UnicodeString msg_string(msg.data(), msg.size());
85 UErrorCode error = U_ZERO_ERROR;
86 icu::MessageFormat format(msg_string, error);
87 icu::UnicodeString formatted;
88 icu::FieldPosition ignore(icu::FieldPosition::DONT_CARE);
89 format.format(args, args_count, formatted, ignore, error);
90 if (U_FAILURE(error)) {
91 LOG(ERROR) << "MessageFormat(" << msg.as_string() << ") failed with "
92 << u_errorName(error);
95 return i18n::UnicodeStringToString16(formatted);
98 string16 MessageFormatter::FormatWithNamedArgs(
100 StringPiece name0, const internal::MessageArg& arg0,
101 StringPiece name1, const internal::MessageArg& arg1,
102 StringPiece name2, const internal::MessageArg& arg2,
103 StringPiece name3, const internal::MessageArg& arg3,
104 StringPiece name4, const internal::MessageArg& arg4,
105 StringPiece name5, const internal::MessageArg& arg5,
106 StringPiece name6, const internal::MessageArg& arg6) {
107 icu::UnicodeString names[] = {
108 UnicodeStringFromStringPiece(name0),
109 UnicodeStringFromStringPiece(name1),
110 UnicodeStringFromStringPiece(name2),
111 UnicodeStringFromStringPiece(name3),
112 UnicodeStringFromStringPiece(name4),
113 UnicodeStringFromStringPiece(name5),
114 UnicodeStringFromStringPiece(name6),
116 int32_t args_count = 0;
117 icu::Formattable args[] = {
118 arg0.has_value(&args_count) ? *arg0.formattable : icu::Formattable(),
119 arg1.has_value(&args_count) ? *arg1.formattable : icu::Formattable(),
120 arg2.has_value(&args_count) ? *arg2.formattable : icu::Formattable(),
121 arg3.has_value(&args_count) ? *arg3.formattable : icu::Formattable(),
122 arg4.has_value(&args_count) ? *arg4.formattable : icu::Formattable(),
123 arg5.has_value(&args_count) ? *arg5.formattable : icu::Formattable(),
124 arg6.has_value(&args_count) ? *arg6.formattable : icu::Formattable(),
127 UnicodeString msg_string(msg.data(), msg.size());
128 UErrorCode error = U_ZERO_ERROR;
129 icu::MessageFormat format(msg_string, error);
131 icu::UnicodeString formatted;
132 format.format(names, args, args_count, formatted, error);
133 if (U_FAILURE(error)) {
134 LOG(ERROR) << "MessageFormat(" << msg.as_string() << ") failed with "
135 << u_errorName(error);
138 return i18n::UnicodeStringToString16(formatted);