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

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: review 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 // Copied from i18n/localization/formatter.cc with modifications
5
6 #include "base/i18n/message_formatter.h"
7
8 #include "base/logging.h"
9 #include "third_party/icu/source/common/unicode/unistr.h"
10 #include "third_party/icu/source/common/unicode/utypes.h"
11
12 using icu::UnicodeString;
13
14 namespace base {
15 namespace i18n {
16 namespace {
17 UnicodeString UnicodeStringFromStringPiece(StringPiece str) {
18 return UnicodeString::fromUTF8(
19 icu::StringPiece(str.data(), static_cast<int32_t>(str.size())));
Ryan Sleevi 2015/07/31 22:19:36 If you can depend on //base (and it seems you can)
jungshik at Google 2015/08/01 14:00:58 Thanks. Done
20 }
21 } // anonymous namespace
22
23 namespace internal {
24 MessageArg::MessageArg() : formattable(nullptr) {}
25
26 MessageArg::MessageArg(const char* s)
27 : formattable(new icu::Formattable(UnicodeStringFromStringPiece(s))) {}
28
29 MessageArg::MessageArg(StringPiece s)
30 : formattable(new icu::Formattable(UnicodeStringFromStringPiece(s))) {}
31
32 MessageArg::MessageArg(const std::string& s)
33 : formattable(new icu::Formattable(UnicodeString::fromUTF8(s))) {}
34
35 MessageArg::MessageArg(const string16& s)
36 : formattable(new icu::Formattable(UnicodeString(s.data(), s.size()))) {}
37
38 MessageArg::MessageArg(int i) : formattable(new icu::Formattable(i)) {}
39
40 MessageArg::MessageArg(double d) : formattable(new icu::Formattable(d)) {}
41
42 MessageArg::MessageArg(const Time& t)
43 : formattable(new icu::Formattable(static_cast<UDate>(t.ToJsTime()))) {}
44
45 MessageArg::~MessageArg() {}
46
47 // Tests if this argument has a value, and if so increments *count.
48 bool MessageArg::has_value(int *count) const {
49 if (formattable == nullptr)
50 return false;
51
52 ++*count;
53 return true;
54 }
55
56 } // namespace internal
57
58 string16 MessageFormatter::FormatWithNumberedArgs(
59 StringPiece16 msg,
60 const internal::MessageArg& arg0,
61 const internal::MessageArg& arg1,
62 const internal::MessageArg& arg2,
63 const internal::MessageArg& arg3,
64 const internal::MessageArg& arg4,
65 const internal::MessageArg& arg5,
66 const internal::MessageArg& arg6) {
67 int32_t args_count = 0;
68 icu::Formattable args[] = {
69 arg0.has_value(&args_count) ? *arg0.formattable : icu::Formattable(),
70 arg1.has_value(&args_count) ? *arg1.formattable : icu::Formattable(),
71 arg2.has_value(&args_count) ? *arg2.formattable : icu::Formattable(),
72 arg3.has_value(&args_count) ? *arg3.formattable : icu::Formattable(),
73 arg4.has_value(&args_count) ? *arg4.formattable : icu::Formattable(),
74 arg5.has_value(&args_count) ? *arg5.formattable : icu::Formattable(),
75 arg6.has_value(&args_count) ? *arg6.formattable : icu::Formattable(),
76 };
77
78 UnicodeString msg_string(msg.data(), msg.size());
79 UErrorCode error = U_ZERO_ERROR;
80 icu::MessageFormat format(msg_string, error);
81 icu::UnicodeString formatted;
82 icu::FieldPosition ignore(icu::FieldPosition::DONT_CARE);
83 format.format(args, args_count, formatted, ignore, error);
84 if (U_FAILURE(error)) {
85 LOG(ERROR) << "MessageFormat(" << msg.as_string() << ") failed with "
86 << u_errorName(error);
87 return string16();
88 }
89 return string16(formatted.getBuffer(), formatted.length());
90 }
91
92 string16 MessageFormatter::FormatWithNamedArgs(
93 StringPiece16 msg,
94 StringPiece name0, const internal::MessageArg& arg0,
95 StringPiece name1, const internal::MessageArg& arg1,
96 StringPiece name2, const internal::MessageArg& arg2,
97 StringPiece name3, const internal::MessageArg& arg3,
98 StringPiece name4, const internal::MessageArg& arg4,
99 StringPiece name5, const internal::MessageArg& arg5,
100 StringPiece name6, const internal::MessageArg& arg6) {
101 icu::UnicodeString names[] = {
102 UnicodeStringFromStringPiece(name0),
103 UnicodeStringFromStringPiece(name1),
104 UnicodeStringFromStringPiece(name2),
105 UnicodeStringFromStringPiece(name3),
106 UnicodeStringFromStringPiece(name4),
107 UnicodeStringFromStringPiece(name5),
108 UnicodeStringFromStringPiece(name6),
109 };
110 int32_t args_count = 0;
111 icu::Formattable args[] = {
112 arg0.has_value(&args_count) ? *arg0.formattable : icu::Formattable(),
113 arg1.has_value(&args_count) ? *arg1.formattable : icu::Formattable(),
114 arg2.has_value(&args_count) ? *arg2.formattable : icu::Formattable(),
115 arg3.has_value(&args_count) ? *arg3.formattable : icu::Formattable(),
116 arg4.has_value(&args_count) ? *arg4.formattable : icu::Formattable(),
117 arg5.has_value(&args_count) ? *arg5.formattable : icu::Formattable(),
118 arg6.has_value(&args_count) ? *arg6.formattable : icu::Formattable(),
119 };
120
121 UnicodeString msg_string(msg.data(), msg.size());
122 UErrorCode error = U_ZERO_ERROR;
123 icu::MessageFormat format(msg_string, error);
124
125 icu::UnicodeString formatted;
126 format.format(names, args, args_count, formatted, error);
127 if (U_FAILURE(error)) {
128 LOG(ERROR) << "MessageFormat(" << msg.as_string() << ") failed with "
129 << u_errorName(error);
130 return string16();
131 }
132 return string16(formatted.getBuffer(), formatted.length());
133 }
134
135 } // namespace i18n
136 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698