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

Side by Side 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: Removing bogus DCHECK 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef CHROME_COMMON_PLURAL_FORMATTER_H__
6 #define CHROME_COMMON_PLURAL_FORMATTER_H__
7 #pragma once
8
9 // This file defines a method to format a plural value properly as a
10 // string for multiple locales.
11
12 #include <string>
13 #include <vector>
14
15 #include "base/gtest_prod_util.h"
16 #include "base/scoped_ptr.h"
17 #include "base/string16.h"
18
19 class PluralFormatterImpl;
20
21 // This class returns appropriate plural string for the locale:
22 // e.g. singular: "Your hovercraft has 1 eel." versus plural: "Your
23 // hovercraft has 42 eels." If you never have a particular value of
24 // something (e.g. if you never have "1 eel" in your hovercraft), you
25 // still need to specify a message for that value: plural rules in
26 // other languages are very different from English, and, for instance,
27 // the ONE message may be used for all numbers ending in one, not just
28 // a literal "1".
29 //
30 // Note that this class will ONLY format a string with zero or one
31 // integer "#" fields in it.
32 //
33 // NOTE: See the file plural_formatter_example.grd for an example of
34 // the messages you need to create to use this class. Please use that
35 // form, and don't invent your own: there's logic in there.
36 //
37 // For the example messages, the English strings would be "Your
38 // hovercraft has 1 eel." and "Your hovercraft has 42 eels." Other
39 // translations will be created for each other locale, based on how
40 // they handle plurals.
41 //
42 // For cases where your string doesn't make sense (i.e. there can never
43 // be zero eels in your hovercraft), you may leave out that string, and
44 // pass "-1" for the message ID when creating the PluralFormatter.
45 //
46 // Don't expect the "few" and "many" cases to be used for English: they
47 // won't be. Only ONE and DEFAULT are used for English.
48 //
49 // The messages would be handed to the PluralFormatter as follows:
50 //
51 // ===
52 // PluralFormatter formatter(IDS_EELS_ZERO,
53 // IDS_EEL_ONE,
54 // IDS_EELS_TWO,
55 // IDS_EELS_FEW,
56 // IDS_EELS_MANY,
57 // IDS_EELS_DEFAULT);
58 //
59 // // Result is "Your hovercraft has 1 eel."
60 // string16 result = formatter.GetPluralString(1);
61 //
62 // // Result is "Your hovercraft has 42 eels."
63 // string16 result = formatter.GetPluralString(42);
64 // ===
65 //
66 // When you create these strings in the main string file, please leave
67 // the descriptions intact (well, obviously, change the part that says
68 // "Describes the number of eels in a hovercraft."!) so that they
69 // serve as a guide for the translators to help reduce translation
70 // errors.
71
Paweł Hajdan Jr. 2011/03/25 09:32:19 nit: Remove the empty line for the class comment.
72 class PluralFormatter {
73 public:
74 typedef std::string (*StringSourceFunction)(int);
Paweł Hajdan Jr. 2011/03/25 09:32:19 nit: Can this typedef be moved to the private part
75
76 // There must be exactly six message ids, in increasing cardinality:
77 // zero, one, two, few, many, default.
78 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
79 PluralFormatter(int zero_message_id,
80 int one_message_id,
81 int two_message_id,
82 int few_message_id,
83 int many_message_id,
84 int default_message_id);
85 ~PluralFormatter();
86
87 string16 GetPluralString(int number);
88 private:
Paweł Hajdan Jr. 2011/03/25 09:32:19 nit: Add empty line above.
89 // The tests use SetStringSource(), SetOverrideLocale()
90 friend class PluralFormatterTest;
91 FRIEND_TEST_ALL_PREFIXES(PluralFormatterTest, Basic);
92 FRIEND_TEST_ALL_PREFIXES(PluralFormatterTest, TestLocales);
93
94 // Used by unit test to override resource string fetching function.
95 static void SetStringSource(StringSourceFunction string_source);
96
97 // Used by unit test to cycle through locales. Set to NULL to reset
98 // to default. Must be set before creating the PluralFormatter
99 // object that will use it.
100 static void SetOverrideLocale(const char* override_locale);
101
102 PluralFormatterImpl* impl_;
Paweł Hajdan Jr. 2011/03/25 09:32:19 Why not just scoped_ptr?
103 };
104
105 #endif // CHROME_COMMON_PLURAL_FORMATTER_H__
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698