Chromium Code Reviews| 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..ef0c5023d4064481f8fa13324e3934b5d5acfa87 |
| --- /dev/null |
| +++ b/chrome/common/plural_formatter.h |
| @@ -0,0 +1,105 @@ |
| +// 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__ |
| +#pragma once |
| + |
| +// This file defines a method to format a plural value properly as a |
| +// string for multiple locales. |
| + |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/gtest_prod_util.h" |
| +#include "base/scoped_ptr.h" |
| +#include "base/string16.h" |
| + |
| +class PluralFormatterImpl; |
| + |
| +// 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. |
| + |
|
Paweł Hajdan Jr.
2011/03/25 09:32:19
nit: Remove the empty line for the class comment.
|
| +class PluralFormatter { |
| + public: |
| + typedef std::string (*StringSourceFunction)(int); |
|
Paweł Hajdan Jr.
2011/03/25 09:32:19
nit: Can this typedef be moved to the private part
|
| + |
| + // There must be exactly six message ids, in increasing cardinality: |
| + // zero, one, two, few, many, default. |
| + explicit PluralFormatter(const int message_ids[]); |
|
Paweł Hajdan Jr.
2011/03/25 09:32:19
nit: Why do we have two ctors that do the same thi
|
| + PluralFormatter(int zero_message_id, |
| + int one_message_id, |
| + int two_message_id, |
| + int few_message_id, |
| + int many_message_id, |
| + int default_message_id); |
| + ~PluralFormatter(); |
| + |
| + string16 GetPluralString(int number); |
| + private: |
|
Paweł Hajdan Jr.
2011/03/25 09:32:19
nit: Add empty 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_; |
|
Paweł Hajdan Jr.
2011/03/25 09:32:19
Why not just scoped_ptr?
|
| +}; |
| + |
| +#endif // CHROME_COMMON_PLURAL_FORMATTER_H__ |