Index: base/i18n/message_formatter.cc |
diff --git a/base/i18n/message_formatter.cc b/base/i18n/message_formatter.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..75706ff6fa5c964eebb5182fc9ab716b783498d0 |
--- /dev/null |
+++ b/base/i18n/message_formatter.cc |
@@ -0,0 +1,136 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+// Copied from i18n/localization/formatter.cc with modifications |
+ |
+#include "base/i18n/message_formatter.h" |
+ |
+#include "base/logging.h" |
+#include "third_party/icu/source/common/unicode/unistr.h" |
+#include "third_party/icu/source/common/unicode/utypes.h" |
+ |
+ |
Avi (use Gerrit)
2015/07/30 19:19:48
one blank line
jungshik at Google
2015/07/31 21:36:15
Done.
|
+using icu::UnicodeString; |
+ |
+namespace base { |
+namespace i18n { |
+namespace { |
+ UnicodeString UnicodeStringFromStringPiece(StringPiece str) { |
+ return UnicodeString::fromUTF8( |
+ icu::StringPiece(str.data(), static_cast<int32_t>(str.size()))); |
+ } |
Avi (use Gerrit)
2015/07/30 19:19:48
no indentation for stuff in a namespace
jungshik at Google
2015/07/31 21:36:15
Done.
|
+} // anonymous namespace |
+ |
+namespace internal { |
+MessageArg::MessageArg(const char* s) |
+ : f(new icu::Formattable(UnicodeStringFromStringPiece(s))) {} |
+ |
+MessageArg::MessageArg(StringPiece s) |
+ : f(new icu::Formattable(UnicodeStringFromStringPiece(s))) {} |
+ |
+MessageArg::MessageArg(const std::string& s) |
+ : f(new icu::Formattable(UnicodeString::fromUTF8(s))) {} |
+ |
+MessageArg::MessageArg(const string16& s) |
+ : f(new icu::Formattable(UnicodeString(s.data(), s.size()))) {} |
+ |
+MessageArg::MessageArg(int i) : f(new icu::Formattable(i)) {} |
+ |
+MessageArg::MessageArg(double d) : f(new icu::Formattable(d)) {} |
+ |
+MessageArg::MessageArg(const Time& t) : |
+ f(new icu::Formattable(static_cast<UDate>(t.ToJsTime()))) {} |
Ryan Sleevi
2015/07/30 23:21:51
Wrong indent/wrap
jungshik at Google
2015/07/31 21:36:15
Done.
|
+ |
+MessageArg::~MessageArg() { delete f; } |
Ryan Sleevi
2015/07/30 23:21:50
Why not use a scoped_ptr<> for this?
jungshik at Google
2015/07/31 21:36:15
Done. Clang-chrome-style checker complained about
|
+ |
+// Tests if this argument has a value, and if so increments *count. |
+bool MessageArg::def(int *count) const { |
+ if (f != NULL) { |
Avi (use Gerrit)
2015/07/30 19:19:48
if (f)
?
At least nullptr
jungshik at Google
2015/07/31 21:36:15
Done and renamed |f| to |formattable|
|
+ ++*count; |
+ return true; |
+ } else { |
Ryan Sleevi
2015/07/30 23:21:50
Don't else after a return
https://www.chromium.or
jungshik at Google
2015/07/31 21:36:15
Done.
|
+ return false; |
+ } |
+} |
+ |
+} // namespace internal |
+ |
+string16 MessageFormatter::FormatWithNumberedArgs( |
+ StringPiece16 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) { |
+ int32_t args_count = 0; |
+ icu::Formattable args[] = { |
+ arg0.def(&args_count) ? *arg0.f : icu::Formattable(), |
+ arg1.def(&args_count) ? *arg1.f : icu::Formattable(), |
+ arg2.def(&args_count) ? *arg2.f : icu::Formattable(), |
+ arg3.def(&args_count) ? *arg3.f : icu::Formattable(), |
+ arg4.def(&args_count) ? *arg4.f : icu::Formattable(), |
+ arg5.def(&args_count) ? *arg5.f : icu::Formattable(), |
+ arg6.def(&args_count) ? *arg6.f : icu::Formattable(), |
+ }; |
+ |
+ UnicodeString msgString(msg.data(), msg.size()); |
Ryan Sleevi
2015/07/30 23:21:51
msg_string
jungshik at Google
2015/07/31 21:36:15
Done.
|
+ UErrorCode error = U_ZERO_ERROR; |
+ icu::MessageFormat fmt(msgString, error); |
Ryan Sleevi
2015/07/30 23:21:51
format
jungshik at Google
2015/07/31 21:36:15
Done. Also in FormatWithNamedArgs
|
+ icu::UnicodeString formatted; |
+ icu::FieldPosition ignore(icu::FieldPosition::DONT_CARE); |
+ fmt.format(args, args_count, formatted, ignore, error); |
+ if (U_FAILURE(error)) { |
+ LOG(ERROR) << "MessageFormat(" << msg.as_string() << ") failed with " |
+ << u_errorName(error); |
+ return string16(); |
+ } |
+ return string16(formatted.getBuffer(), formatted.length()); |
+} |
+ |
+string16 MessageFormatter::FormatWithNamedArgs( |
+ StringPiece16 msg, |
+ StringPiece name0, const internal::MessageArg& arg0, |
+ StringPiece name1, const internal::MessageArg& arg1, |
+ StringPiece name2, const internal::MessageArg& arg2, |
+ StringPiece name3, const internal::MessageArg& arg3, |
+ StringPiece name4, const internal::MessageArg& arg4, |
+ StringPiece name5, const internal::MessageArg& arg5, |
+ StringPiece name6, const internal::MessageArg& arg6) { |
+ icu::UnicodeString names[] = { |
+ UnicodeStringFromStringPiece(name0), |
+ UnicodeStringFromStringPiece(name1), |
+ UnicodeStringFromStringPiece(name2), |
+ UnicodeStringFromStringPiece(name3), |
+ UnicodeStringFromStringPiece(name4), |
+ UnicodeStringFromStringPiece(name5), |
+ UnicodeStringFromStringPiece(name6), |
+ }; |
+ int32_t args_count = 0; |
+ icu::Formattable args[] = { |
+ arg0.def(&args_count) ? *arg0.f : icu::Formattable(), |
+ arg1.def(&args_count) ? *arg1.f : icu::Formattable(), |
+ arg2.def(&args_count) ? *arg2.f : icu::Formattable(), |
+ arg3.def(&args_count) ? *arg3.f : icu::Formattable(), |
+ arg4.def(&args_count) ? *arg4.f : icu::Formattable(), |
+ arg5.def(&args_count) ? *arg5.f : icu::Formattable(), |
+ arg6.def(&args_count) ? *arg6.f : icu::Formattable(), |
+ }; |
+ |
+ UnicodeString msgString(msg.data(), msg.size()); |
Ryan Sleevi
2015/07/30 23:21:50
msg_string
jungshik at Google
2015/07/31 21:36:15
Done.
|
+ UErrorCode error = U_ZERO_ERROR; |
+ icu::MessageFormat fmt(msgString, error); |
Ryan Sleevi
2015/07/30 23:21:51
format
jungshik at Google
2015/07/31 21:36:15
Done.
|
+ |
+ icu::UnicodeString formatted; |
+ fmt.format(names, args, args_count, formatted, error); |
+ if (U_FAILURE(error)) { |
+ LOG(ERROR) << "MessageFormat(" << msg.as_string() << ") failed with " |
+ << u_errorName(error); |
+ return string16(); |
+ } |
+ return string16(formatted.getBuffer(), formatted.length()); |
+} |
+ |
+} // namespace i18n |
+} // namespace base |