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

Unified Diff: base/i18n/message_formatter.h

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, 5 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 side-by-side diff with in-line comments
Download patch
Index: base/i18n/message_formatter.h
diff --git a/base/i18n/message_formatter.h b/base/i18n/message_formatter.h
new file mode 100644
index 0000000000000000000000000000000000000000..1044950c777b67253c33a786290eb78531affc1c
--- /dev/null
+++ b/base/i18n/message_formatter.h
@@ -0,0 +1,103 @@
+// 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.h with modifications.
Ryan Sleevi 2015/07/31 22:19:36 Seems like there should at least be a newline betw
jungshik at Google 2015/08/01 14:00:58 Before making the CL, I talked to OSPO and the two
+
+#ifndef BASE_I18N_MESSAGE_FORMATTER_H_
+#define BASE_I18N_MESSAGE_FORMATTER_H_
+
+#include <string>
+
+#include "base/i18n/base_i18n_export.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/strings/string16.h"
+#include "base/strings/string_piece.h"
+#include "base/time/time.h"
+#include "third_party/icu/source/i18n/unicode/msgfmt.h"
+
+namespace base {
+namespace i18n {
+
+class MessageFormatter;
+
+namespace internal {
+
+class BASE_I18N_EXPORT MessageArg {
+ public:
+ MessageArg(const char* s);
+ MessageArg(StringPiece s);
+ MessageArg(const std::string& s);
+ MessageArg(const string16& s);
+ MessageArg(int i);
+ MessageArg(double d);
+ MessageArg(const Time& t);
Ryan Sleevi 2015/07/31 22:19:36 You could forward declare base::Time and avoid the
jungshik at Google 2015/08/01 14:00:58 Done.
+
+ ~MessageArg();
+
+ private:
+ friend class base::i18n::MessageFormatter;
+ MessageArg();
Ryan Sleevi 2015/07/31 22:19:36 style nits: newline between 38 and 39, newline bet
jungshik at Google 2015/08/01 14:00:58 Done.
+ bool has_value(int* count) const;
+ scoped_ptr<icu::Formattable> formattable;
Ryan Sleevi 2015/07/31 22:19:36 You could forward declare this, but I'm guessing t
jungshik at Google 2015/08/01 14:00:58 Thanks for the suggestion. Nothing in ICU against
Ryan Sleevi 2015/08/03 08:01:41 I guess the forward declaration doesn't really buy
+ DISALLOW_COPY_AND_ASSIGN(MessageArg);
+};
+
+} // namespace internal
+
+// Message Formatter with the ICU message format syntax support.
+// It can format strings (UTF-8 and UTF-16), numbers and base::Time with
+// plural, gender and other 'selectors' support. This is handy if you
+// have multiple parameters of differnt types and some of them require
+// plural or gender/selector support.
+//
+// To use this API for locale-sensitive formatting, retrieve a 'message
+// template' in the ICU message format from a message bundle (e.g. with
+// l10n_util::GetStringUTF16()) and pass it to FormatWith{Named,Numbered}Args.
+//
+// MessageFormat specs:
+// http://icu-project.org/apiref/icu4j/com/ibm/icu/text/MessageFormat.html
+// http://icu-project.org/apiref/icu4c/classicu_1_1DecimalFormat.html#details
+// Examples:
+// http://userguide.icu-project.org/formatparse/messages
+// message_formatter_unittest.cc
+// go/plurals inside Google.
+// TODO(jshin): Document this API at sites.chromium.org and add a reference
+// here.
+
+class BASE_I18N_EXPORT MessageFormatter {
+ public:
+ static string16 FormatWithNamedArgs(
+ StringPiece16 msg,
+ StringPiece name0 = StringPiece(),
+ const internal::MessageArg& arg0 = internal::MessageArg(),
+ StringPiece name1 = StringPiece(),
+ const internal::MessageArg& arg1 = internal::MessageArg(),
+ StringPiece name2 = StringPiece(),
+ const internal::MessageArg& arg2 = internal::MessageArg(),
+ StringPiece name3 = StringPiece(),
+ const internal::MessageArg& arg3 = internal::MessageArg(),
+ StringPiece name4 = StringPiece(),
+ const internal::MessageArg& arg4 = internal::MessageArg(),
+ StringPiece name5 = StringPiece(),
+ const internal::MessageArg& arg5 = internal::MessageArg(),
+ StringPiece name6 = StringPiece(),
+ const internal::MessageArg& arg6 = internal::MessageArg());
+
+ static string16 FormatWithNumberedArgs(
+ StringPiece16 msg,
+ const internal::MessageArg& arg0 = internal::MessageArg(),
+ const internal::MessageArg& arg1 = internal::MessageArg(),
+ const internal::MessageArg& arg2 = internal::MessageArg(),
+ const internal::MessageArg& arg3 = internal::MessageArg(),
+ const internal::MessageArg& arg4 = internal::MessageArg(),
+ const internal::MessageArg& arg5 = internal::MessageArg(),
+ const internal::MessageArg& arg6 = internal::MessageArg());
+
+ private:
+ MessageFormatter() {}
+ DISALLOW_COPY_AND_ASSIGN(MessageFormatter);
+};
+
+} // namespace i18n
+} // namespace base
+#endif

Powered by Google App Engine
This is Rietveld 408576698