jshin | 6a2dd2b | 2015-08-07 19:11:09 +0900 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "base/i18n/message_formatter.h" |
| 6 | |
jshin | f901f38 | 2017-03-23 18:57:16 +0900 | [diff] [blame] | 7 | #include "base/i18n/unicodestring.h" |
jshin | 6a2dd2b | 2015-08-07 19:11:09 +0900 | [diff] [blame] | 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" |
| 15 | |
| 16 | using icu::UnicodeString; |
| 17 | |
| 18 | namespace base { |
| 19 | namespace i18n { |
| 20 | namespace { |
| 21 | UnicodeString UnicodeStringFromStringPiece(StringPiece str) { |
| 22 | return UnicodeString::fromUTF8( |
| 23 | icu::StringPiece(str.data(), base::checked_cast<int32_t>(str.size()))); |
| 24 | } |
| 25 | } // anonymous namespace |
| 26 | |
| 27 | namespace internal { |
| 28 | MessageArg::MessageArg() : formattable(nullptr) {} |
| 29 | |
| 30 | MessageArg::MessageArg(const char* s) |
| 31 | : formattable(new icu::Formattable(UnicodeStringFromStringPiece(s))) {} |
| 32 | |
| 33 | MessageArg::MessageArg(StringPiece s) |
| 34 | : formattable(new icu::Formattable(UnicodeStringFromStringPiece(s))) {} |
| 35 | |
| 36 | MessageArg::MessageArg(const std::string& s) |
| 37 | : formattable(new icu::Formattable(UnicodeString::fromUTF8(s))) {} |
| 38 | |
| 39 | MessageArg::MessageArg(const string16& s) |
| 40 | : formattable(new icu::Formattable(UnicodeString(s.data(), s.size()))) {} |
| 41 | |
| 42 | MessageArg::MessageArg(int i) : formattable(new icu::Formattable(i)) {} |
| 43 | |
| 44 | MessageArg::MessageArg(int64_t i) : formattable(new icu::Formattable(i)) {} |
| 45 | |
| 46 | MessageArg::MessageArg(double d) : formattable(new icu::Formattable(d)) {} |
| 47 | |
| 48 | MessageArg::MessageArg(const Time& t) |
| 49 | : formattable(new icu::Formattable(static_cast<UDate>(t.ToJsTime()))) {} |
| 50 | |
Chris Watkins | d155d9f | 2017-11-29 16:16:38 +0900 | [diff] [blame^] | 51 | MessageArg::~MessageArg() = default; |
jshin | 6a2dd2b | 2015-08-07 19:11:09 +0900 | [diff] [blame] | 52 | |
| 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) |
| 56 | return false; |
| 57 | |
| 58 | ++*count; |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | } // namespace internal |
| 63 | |
| 64 | string16 MessageFormatter::FormatWithNumberedArgs( |
| 65 | StringPiece16 msg, |
| 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(), |
| 82 | }; |
| 83 | |
| 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); |
| 93 | return string16(); |
| 94 | } |
jshin | f901f38 | 2017-03-23 18:57:16 +0900 | [diff] [blame] | 95 | return i18n::UnicodeStringToString16(formatted); |
jshin | 6a2dd2b | 2015-08-07 19:11:09 +0900 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | string16 MessageFormatter::FormatWithNamedArgs( |
| 99 | StringPiece16 msg, |
| 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), |
| 115 | }; |
| 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(), |
| 125 | }; |
| 126 | |
| 127 | UnicodeString msg_string(msg.data(), msg.size()); |
| 128 | UErrorCode error = U_ZERO_ERROR; |
| 129 | icu::MessageFormat format(msg_string, error); |
| 130 | |
| 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); |
| 136 | return string16(); |
| 137 | } |
jshin | f901f38 | 2017-03-23 18:57:16 +0900 | [diff] [blame] | 138 | return i18n::UnicodeStringToString16(formatted); |
jshin | 6a2dd2b | 2015-08-07 19:11:09 +0900 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | } // namespace i18n |
| 142 | } // namespace base |