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

Unified Diff: chrome/common/plural_formatter.h

Issue 6736003: This adds a formatter for plurals that works for all locales. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Initial review changes Created 9 years, 9 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: chrome/common/plural_formatter.h
diff --git a/chrome/common/plural_formatter.h b/chrome/common/plural_formatter.h
new file mode 100644
index 0000000000000000000000000000000000000000..9a6c263f363a4bf006ecdef126f063582c6495a6
--- /dev/null
+++ b/chrome/common/plural_formatter.h
@@ -0,0 +1,107 @@
+// Copyright (c) 2011 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 CHROME_COMMON_PLURAL_FORMATTER_H__
+#define CHROME_COMMON_PLURAL_FORMATTER_H__
tfarina 2011/03/26 02:05:15 remove extra _ ;)
Greg Spencer (Chromium) 2011/03/28 23:13:37 Huh. How did that get there? Removed.
+#pragma once
+
+// This file defines a method to format a plural value properly as a
+// string for multiple locales.
+
+#include <string>
+#include <vector>
tfarina 2011/03/26 02:05:15 I think you can remove this. It's not used in this
Greg Spencer (Chromium) 2011/03/28 23:13:37 Done.
+
+#include "base/gtest_prod_util.h"
+#include "base/scoped_ptr.h"
tfarina 2011/03/26 02:05:15 you can remove this include.
Greg Spencer (Chromium) 2011/03/28 23:13:37 Done.
+#include "base/string16.h"
+
+class PluralFormatterImpl;
tfarina 2011/03/26 02:05:15 Please, could you move this to line 103? It doesn'
Greg Spencer (Chromium) 2011/03/28 23:13:37 Done.
+
+// This class returns appropriate plural string for the locale:
+// e.g. singular: "Your hovercraft has 1 eel." versus plural: "Your
+// hovercraft has 42 eels." If you never have a particular value of
+// something (e.g. if you never have "1 eel" in your hovercraft), you
+// still need to specify a message for that value: plural rules in
+// other languages are very different from English, and, for instance,
+// the ONE message may be used for all numbers ending in one, not just
+// a literal "1".
+//
+// Note that this class will ONLY format a string with zero or one
+// integer "#" fields in it.
+//
+// NOTE: See the file plural_formatter_example.grd for an example of
+// the messages you need to create to use this class. Please use that
+// form, and don't invent your own: there's logic in there.
+//
+// For the example messages, the English strings would be "Your
+// hovercraft has 1 eel." and "Your hovercraft has 42 eels." Other
+// translations will be created for each other locale, based on how
+// they handle plurals.
+//
+// For cases where your string doesn't make sense (i.e. there can never
+// be zero eels in your hovercraft), you may leave out that string, and
+// pass "-1" for the message ID when creating the PluralFormatter.
+//
+// Don't expect the "few" and "many" cases to be used for English: they
+// won't be. Only ONE and DEFAULT are used for English.
+//
+// The messages would be handed to the PluralFormatter as follows:
+//
+// ===
+// PluralFormatter formatter(IDS_EELS_ZERO,
+// IDS_EEL_ONE,
+// IDS_EELS_TWO,
+// IDS_EELS_FEW,
+// IDS_EELS_MANY,
+// IDS_EELS_DEFAULT);
+//
+// // Result is "Your hovercraft has 1 eel."
+// string16 result = formatter.GetPluralString(1);
+//
+// // Result is "Your hovercraft has 42 eels."
+// string16 result = formatter.GetPluralString(42);
+// ===
+//
+// When you create these strings in the main string file, please leave
+// the descriptions intact (well, obviously, change the part that says
+// "Describes the number of eels in a hovercraft."!) so that they
+// serve as a guide for the translators to help reduce translation
+// errors.
+
+class PluralFormatter {
+ public:
+ typedef std::string (*StringSourceFunction)(int);
+
+ PluralFormatter();
+ ~PluralFormatter();
+
+ // There must be exactly six message ids, in increasing cardinality:
+ // zero, one, two, few, many, default.
+ bool Init(const int message_ids[]);
+ bool Init(int zero_message_id,
+ int one_message_id,
+ int two_message_id,
+ int few_message_id,
+ int many_message_id,
+ int default_message_id);
+
+ string16 GetPluralString(int number) const;
+ private:
tfarina 2011/03/26 02:05:15 please, add a blank line above.
+ // The tests use SetStringSource(), SetOverrideLocale()
+ friend class PluralFormatterTest;
+ FRIEND_TEST_ALL_PREFIXES(PluralFormatterTest, Basic);
+ FRIEND_TEST_ALL_PREFIXES(PluralFormatterTest, TestLocales);
+
+ // Used by unit test to override resource string fetching function.
+ static void SetStringSource(StringSourceFunction string_source);
+
+ // Used by unit test to cycle through locales. Set to NULL to reset
+ // to default. Must be set before creating the PluralFormatter
+ // object that will use it.
+ static void SetOverrideLocale(const char* override_locale);
+
+ PluralFormatterImpl* impl_;
+};
+
+#endif // CHROME_COMMON_PLURAL_FORMATTER_H__

Powered by Google App Engine
This is Rietveld 408576698