Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(923)

Side by Side Diff: base/i18n/message_formatter.cc

Issue 1140153005: ICU msg format support with more than one arguments (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: comments addressed Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
7 #include "base/logging.h"
8 #include "base/numerics/safe_conversions.h"
9 #include "base/time/time.h"
10 #include "third_party/icu/source/common/unicode/unistr.h"
11 #include "third_party/icu/source/common/unicode/utypes.h"
12 #include "third_party/icu/source/i18n/unicode/fmtable.h"
13 #include "third_party/icu/source/i18n/unicode/msgfmt.h"
14
15 using icu::UnicodeString;
16
17 namespace base {
18 namespace i18n {
19 namespace {
20 UnicodeString UnicodeStringFromStringPiece(StringPiece str) {
21 return UnicodeString::fromUTF8(
22 icu::StringPiece(str.data(), base::checked_cast<int32_t>(str.size())));
23 }
24 } // anonymous namespace
25
26 namespace internal {
27 MessageArg::MessageArg() : formattable(nullptr) {}
28
29 MessageArg::MessageArg(const char* s)
30 : formattable(new icu::Formattable(UnicodeStringFromStringPiece(s))) {}
31
32 MessageArg::MessageArg(StringPiece s)
33 : formattable(new icu::Formattable(UnicodeStringFromStringPiece(s))) {}
34
35 MessageArg::MessageArg(const std::string& s)
36 : formattable(new icu::Formattable(UnicodeString::fromUTF8(s))) {}
37
38 MessageArg::MessageArg(const string16& s)
39 : formattable(new icu::Formattable(UnicodeString(s.data(), s.size()))) {}
40
41 MessageArg::MessageArg(int i) : formattable(new icu::Formattable(i)) {}
42
43 MessageArg::MessageArg(double d) : formattable(new icu::Formattable(d)) {}
44
45 MessageArg::MessageArg(const Time& t)
46 : formattable(new icu::Formattable(static_cast<UDate>(t.ToJsTime()))) {}
47
48 MessageArg::~MessageArg() {}
49
50 // Tests if this argument has a value, and if so increments *count.
51 bool MessageArg::has_value(int *count) const {
52 if (formattable == nullptr)
53 return false;
54
55 ++*count;
56 return true;
57 }
58
59 } // namespace internal
60
61 string16 MessageFormatter::FormatWithNumberedArgs(
62 StringPiece16 msg,
63 const internal::MessageArg& arg0,
64 const internal::MessageArg& arg1,
65 const internal::MessageArg& arg2,
66 const internal::MessageArg& arg3,
67 const internal::MessageArg& arg4,
68 const internal::MessageArg& arg5,
69 const internal::MessageArg& arg6) {
70 int32_t args_count = 0;
71 icu::Formattable args[] = {
72 arg0.has_value(&args_count) ? *arg0.formattable : icu::Formattable(),
73 arg1.has_value(&args_count) ? *arg1.formattable : icu::Formattable(),
74 arg2.has_value(&args_count) ? *arg2.formattable : icu::Formattable(),
75 arg3.has_value(&args_count) ? *arg3.formattable : icu::Formattable(),
76 arg4.has_value(&args_count) ? *arg4.formattable : icu::Formattable(),
77 arg5.has_value(&args_count) ? *arg5.formattable : icu::Formattable(),
78 arg6.has_value(&args_count) ? *arg6.formattable : icu::Formattable(),
79 };
80
81 UnicodeString msg_string(msg.data(), msg.size());
82 UErrorCode error = U_ZERO_ERROR;
83 icu::MessageFormat format(msg_string, error);
84 icu::UnicodeString formatted;
85 icu::FieldPosition ignore(icu::FieldPosition::DONT_CARE);
86 format.format(args, args_count, formatted, ignore, error);
87 if (U_FAILURE(error)) {
88 LOG(ERROR) << "MessageFormat(" << msg.as_string() << ") failed with "
89 << u_errorName(error);
90 return string16();
91 }
92 return string16(formatted.getBuffer(), formatted.length());
93 }
94
95 string16 MessageFormatter::FormatWithNamedArgs(
96 StringPiece16 msg,
97 StringPiece name0, const internal::MessageArg& arg0,
98 StringPiece name1, const internal::MessageArg& arg1,
99 StringPiece name2, const internal::MessageArg& arg2,
100 StringPiece name3, const internal::MessageArg& arg3,
101 StringPiece name4, const internal::MessageArg& arg4,
102 StringPiece name5, const internal::MessageArg& arg5,
103 StringPiece name6, const internal::MessageArg& arg6) {
104 icu::UnicodeString names[] = {
105 UnicodeStringFromStringPiece(name0),
106 UnicodeStringFromStringPiece(name1),
107 UnicodeStringFromStringPiece(name2),
108 UnicodeStringFromStringPiece(name3),
109 UnicodeStringFromStringPiece(name4),
110 UnicodeStringFromStringPiece(name5),
111 UnicodeStringFromStringPiece(name6),
112 };
113 int32_t args_count = 0;
114 icu::Formattable args[] = {
115 arg0.has_value(&args_count) ? *arg0.formattable : icu::Formattable(),
116 arg1.has_value(&args_count) ? *arg1.formattable : icu::Formattable(),
117 arg2.has_value(&args_count) ? *arg2.formattable : icu::Formattable(),
118 arg3.has_value(&args_count) ? *arg3.formattable : icu::Formattable(),
119 arg4.has_value(&args_count) ? *arg4.formattable : icu::Formattable(),
120 arg5.has_value(&args_count) ? *arg5.formattable : icu::Formattable(),
121 arg6.has_value(&args_count) ? *arg6.formattable : icu::Formattable(),
122 };
123
124 UnicodeString msg_string(msg.data(), msg.size());
125 UErrorCode error = U_ZERO_ERROR;
126 icu::MessageFormat format(msg_string, error);
127
128 icu::UnicodeString formatted;
129 format.format(names, args, args_count, formatted, error);
130 if (U_FAILURE(error)) {
131 LOG(ERROR) << "MessageFormat(" << msg.as_string() << ") failed with "
132 << u_errorName(error);
133 return string16();
134 }
135 return string16(formatted.getBuffer(), formatted.length());
136 }
137
138 } // namespace i18n
139 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698