1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/i18n/message_formatter.h"
6
7 #include <string_view>
8
9 #include "base/check.h"
10 #include "base/i18n/unicodestring.h"
11 #include "base/logging.h"
12 #include "base/numerics/safe_conversions.h"
13 #include "base/time/time.h"
14 #include "third_party/icu/source/common/unicode/unistr.h"
15 #include "third_party/icu/source/common/unicode/utypes.h"
16 #include "third_party/icu/source/i18n/unicode/fmtable.h"
17 #include "third_party/icu/source/i18n/unicode/msgfmt.h"
18
19 using icu::UnicodeString;
20
21 namespace base {
22 namespace i18n {
23 namespace {
UnicodeStringFromStringView(std::string_view str)24 UnicodeString UnicodeStringFromStringView(std::string_view str) {
25 return UnicodeString::fromUTF8(
26 std::string_view(str.data(), base::checked_cast<int32_t>(str.size())));
27 }
28 } // anonymous namespace
29
30 namespace internal {
MessageArg()31 MessageArg::MessageArg() : formattable(nullptr) {}
32
MessageArg(const char * s)33 MessageArg::MessageArg(const char* s)
34 : formattable(new icu::Formattable(UnicodeStringFromStringView(s))) {}
35
MessageArg(std::string_view s)36 MessageArg::MessageArg(std::string_view s)
37 : formattable(new icu::Formattable(UnicodeStringFromStringView(s))) {}
38
MessageArg(const std::string & s)39 MessageArg::MessageArg(const std::string& s)
40 : formattable(new icu::Formattable(UnicodeString::fromUTF8(s))) {}
41
MessageArg(const std::u16string & s)42 MessageArg::MessageArg(const std::u16string& s)
43 : formattable(new icu::Formattable(UnicodeString(s.data(), s.size()))) {}
44
MessageArg(int i)45 MessageArg::MessageArg(int i) : formattable(new icu::Formattable(i)) {}
46
MessageArg(int64_t i)47 MessageArg::MessageArg(int64_t i) : formattable(new icu::Formattable(i)) {}
48
MessageArg(double d)49 MessageArg::MessageArg(double d) : formattable(new icu::Formattable(d)) {}
50
MessageArg(const Time & t)51 MessageArg::MessageArg(const Time& t)
52 : formattable(new icu::Formattable(
53 static_cast<UDate>(t.InMillisecondsFSinceUnixEpoch()))) {}
54
55 MessageArg::~MessageArg() = default;
56
57 // Tests if this argument has a value, and if so increments *count.
has_value(int * count) const58 bool MessageArg::has_value(int *count) const {
59 if (formattable == nullptr)
60 return false;
61
62 ++*count;
63 return true;
64 }
65
66 } // namespace internal
67
FormatWithNumberedArgs(std::u16string_view msg,const internal::MessageArg & arg0,const internal::MessageArg & arg1,const internal::MessageArg & arg2,const internal::MessageArg & arg3,const internal::MessageArg & arg4,const internal::MessageArg & arg5,const internal::MessageArg & arg6)68 std::u16string MessageFormatter::FormatWithNumberedArgs(
69 std::u16string_view msg,
70 const internal::MessageArg& arg0,
71 const internal::MessageArg& arg1,
72 const internal::MessageArg& arg2,
73 const internal::MessageArg& arg3,
74 const internal::MessageArg& arg4,
75 const internal::MessageArg& arg5,
76 const internal::MessageArg& arg6) {
77 int32_t args_count = 0;
78 icu::Formattable args[] = {
79 arg0.has_value(&args_count) ? *arg0.formattable : icu::Formattable(),
80 arg1.has_value(&args_count) ? *arg1.formattable : icu::Formattable(),
81 arg2.has_value(&args_count) ? *arg2.formattable : icu::Formattable(),
82 arg3.has_value(&args_count) ? *arg3.formattable : icu::Formattable(),
83 arg4.has_value(&args_count) ? *arg4.formattable : icu::Formattable(),
84 arg5.has_value(&args_count) ? *arg5.formattable : icu::Formattable(),
85 arg6.has_value(&args_count) ? *arg6.formattable : icu::Formattable(),
86 };
87
88 UnicodeString msg_string(msg.data(), msg.size());
89 UErrorCode error = U_ZERO_ERROR;
90 icu::MessageFormat format(msg_string, error);
91 icu::UnicodeString formatted;
92 icu::FieldPosition ignore(icu::FieldPosition::DONT_CARE);
93 format.format(args, args_count, formatted, ignore, error);
94 if (U_FAILURE(error)) {
95 LOG(ERROR) << "MessageFormat(" << msg << ") failed with "
96 << u_errorName(error);
97 return std::u16string();
98 }
99 return i18n::UnicodeStringToString16(formatted);
100 }
101
FormatWithNamedArgs(std::u16string_view msg,std::string_view name0,const internal::MessageArg & arg0,std::string_view name1,const internal::MessageArg & arg1,std::string_view name2,const internal::MessageArg & arg2,std::string_view name3,const internal::MessageArg & arg3,std::string_view name4,const internal::MessageArg & arg4,std::string_view name5,const internal::MessageArg & arg5,std::string_view name6,const internal::MessageArg & arg6)102 std::u16string MessageFormatter::FormatWithNamedArgs(
103 std::u16string_view msg,
104 std::string_view name0,
105 const internal::MessageArg& arg0,
106 std::string_view name1,
107 const internal::MessageArg& arg1,
108 std::string_view name2,
109 const internal::MessageArg& arg2,
110 std::string_view name3,
111 const internal::MessageArg& arg3,
112 std::string_view name4,
113 const internal::MessageArg& arg4,
114 std::string_view name5,
115 const internal::MessageArg& arg5,
116 std::string_view name6,
117 const internal::MessageArg& arg6) {
118 icu::UnicodeString names[] = {
119 UnicodeStringFromStringView(name0), UnicodeStringFromStringView(name1),
120 UnicodeStringFromStringView(name2), UnicodeStringFromStringView(name3),
121 UnicodeStringFromStringView(name4), UnicodeStringFromStringView(name5),
122 UnicodeStringFromStringView(name6),
123 };
124 int32_t args_count = 0;
125 icu::Formattable args[] = {
126 arg0.has_value(&args_count) ? *arg0.formattable : icu::Formattable(),
127 arg1.has_value(&args_count) ? *arg1.formattable : icu::Formattable(),
128 arg2.has_value(&args_count) ? *arg2.formattable : icu::Formattable(),
129 arg3.has_value(&args_count) ? *arg3.formattable : icu::Formattable(),
130 arg4.has_value(&args_count) ? *arg4.formattable : icu::Formattable(),
131 arg5.has_value(&args_count) ? *arg5.formattable : icu::Formattable(),
132 arg6.has_value(&args_count) ? *arg6.formattable : icu::Formattable(),
133 };
134
135 UnicodeString msg_string(msg.data(), msg.size());
136 UErrorCode error = U_ZERO_ERROR;
137 icu::MessageFormat format(msg_string, error);
138
139 icu::UnicodeString formatted;
140 format.format(names, args, args_count, formatted, error);
141 if (U_FAILURE(error)) {
142 LOG(ERROR) << "MessageFormat(" << msg << ") failed with "
143 << u_errorName(error);
144 return std::u16string();
145 }
146 return i18n::UnicodeStringToString16(formatted);
147 }
148
149 } // namespace i18n
150 } // namespace base
151