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

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: 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 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__
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.
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>
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.
14
15 #include "base/gtest_prod_util.h"
16 #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.
17 #include "base/string16.h"
18
19 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.
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
72 class PluralFormatter {
73 public:
74 typedef std::string (*StringSourceFunction)(int);
75
76 PluralFormatter();
77 ~PluralFormatter();
78
79 // There must be exactly six message ids, in increasing cardinality:
80 // zero, one, two, few, many, default.
81 bool Init(const int message_ids[]);
82 bool Init(int zero_message_id,
83 int one_message_id,
84 int two_message_id,
85 int few_message_id,
86 int many_message_id,
87 int default_message_id);
88
89 string16 GetPluralString(int number) const;
90 private:
tfarina 2011/03/26 02:05:15 please, add a blank line above.
91 // The tests use SetStringSource(), SetOverrideLocale()
92 friend class PluralFormatterTest;
93 FRIEND_TEST_ALL_PREFIXES(PluralFormatterTest, Basic);
94 FRIEND_TEST_ALL_PREFIXES(PluralFormatterTest, TestLocales);
95
96 // Used by unit test to override resource string fetching function.
97 static void SetStringSource(StringSourceFunction string_source);
98
99 // Used by unit test to cycle through locales. Set to NULL to reset
100 // to default. Must be set before creating the PluralFormatter
101 // object that will use it.
102 static void SetOverrideLocale(const char* override_locale);
103
104 PluralFormatterImpl* impl_;
105 };
106
107 #endif // CHROME_COMMON_PLURAL_FORMATTER_H__
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698