| 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..85330000d66e89d8b6f3c8f82c411c7e78824fd9
|
| --- /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 "base/gtest_prod_util.h"
|
| +#include "base/string16.h"
|
| +
|
| +// 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:
|
| + // 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);
|
| +
|
| + class PluralFormatterImpl;
|
| +
|
| + PluralFormatterImpl* impl_;
|
| +};
|
| +
|
| +#endif // CHROME_COMMON_PLURAL_FORMATTER_H_
|
|
|