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

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: Mark's review comments 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/base.gypi ('k') | base/i18n/message_formatter.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..bcdc3bc97752873a26e2a322978999753d169f24
--- /dev/null
+++ b/base/i18n/message_formatter.h
@@ -0,0 +1,111 @@
+// 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.
+
+#ifndef BASE_I18N_MESSAGE_FORMATTER_H_
+#define BASE_I18N_MESSAGE_FORMATTER_H_
+
+#include <stdint.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 "third_party/icu/source/common/unicode/uversion.h"
+
+U_NAMESPACE_BEGIN
+class Formattable;
+U_NAMESPACE_END
+
+namespace base {
+
+class Time;
+
+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(int64_t i);
+ MessageArg(double d);
+ MessageArg(const Time& t);
+ ~MessageArg();
+
+ private:
+ friend class base::i18n::MessageFormatter;
+ MessageArg();
+ // Tests if this argument has a value, and if so increments *count.
+ bool has_value(int* count) const;
+ scoped_ptr<icu::Formattable> formattable;
+ 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 // BASE_I18N_MESSAGE_FORMATTER_H_
« no previous file with comments | « base/base.gypi ('k') | base/i18n/message_formatter.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698