OLD | NEW |
---|---|
(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 | |
14 #include "base/gtest_prod_util.h" | |
15 #include "base/string16.h" | |
16 | |
17 // This class returns appropriate plural string for the locale: | |
18 // e.g. singular: "Your hovercraft has 1 eel." versus plural: "Your | |
19 // hovercraft has 42 eels." If you never have a particular value of | |
20 // something (e.g. if you never have "1 eel" in your hovercraft), you | |
21 // still need to specify a message for that value: plural rules in | |
22 // other languages are very different from English, and, for instance, | |
23 // the ONE message may be used for all numbers ending in one, not just | |
24 // a literal "1". | |
25 // | |
26 // Note that this class will ONLY format a string with zero or one | |
27 // integer "#" fields in it. | |
28 // | |
29 // NOTE: See the file plural_formatter_example.grd for an example of | |
30 // the messages you need to create to use this class. Please use that | |
31 // form, and don't invent your own: there's logic in there. | |
32 // | |
33 // For the example messages, the English strings would be "Your | |
34 // hovercraft has 1 eel." and "Your hovercraft has 42 eels." Other | |
35 // translations will be created for each other locale, based on how | |
36 // they handle plurals. | |
37 // | |
38 // For cases where your string doesn't make sense (i.e. there can never | |
39 // be zero eels in your hovercraft), you may leave out that string, and | |
40 // pass "-1" for the message ID when creating the PluralFormatter. | |
41 // | |
42 // Don't expect the "few" and "many" cases to be used for English: they | |
43 // won't be. Only ONE and DEFAULT are used for English. | |
44 // | |
45 // The messages would be handed to the PluralFormatter as follows: | |
46 // | |
47 // === | |
48 // PluralFormatter formatter(IDS_EELS_ZERO, | |
49 // IDS_EEL_ONE, | |
50 // IDS_EELS_TWO, | |
51 // IDS_EELS_FEW, | |
52 // IDS_EELS_MANY, | |
53 // IDS_EELS_DEFAULT); | |
54 // | |
55 // // Result is "Your hovercraft has 1 eel." | |
56 // string16 result = formatter.GetPluralString(1); | |
57 // | |
58 // // Result is "Your hovercraft has 42 eels." | |
59 // string16 result = formatter.GetPluralString(42); | |
60 // === | |
61 // | |
62 // When you create these strings in the main string file, please leave | |
63 // the descriptions intact (well, obviously, change the part that says | |
64 // "Describes the number of eels in a hovercraft."!) so that they | |
65 // serve as a guide for the translators to help reduce translation | |
66 // errors. | |
67 | |
tfarina
2011/04/12 01:25:01
remove this blank line, please.
| |
68 class PluralFormatter { | |
69 public: | |
70 typedef std::string (*StringSourceFunction)(int); | |
71 | |
72 PluralFormatter(); | |
73 ~PluralFormatter(); | |
74 | |
75 // There must be exactly six message ids, in increasing cardinality: | |
76 // zero, one, two, few, many, default. | |
77 bool Init(const int message_ids[]); | |
78 bool Init(int zero_message_id, | |
tfarina
2011/04/12 01:25:01
could add a blank line between these two methods?
| |
79 int one_message_id, | |
80 int two_message_id, | |
81 int few_message_id, | |
82 int many_message_id, | |
83 int default_message_id); | |
84 | |
85 string16 GetPluralString(int number) const; | |
86 private: | |
tfarina
2011/04/12 01:25:01
please, add a blank line above.
| |
87 // The tests use SetStringSource(), SetOverrideLocale() | |
88 friend class PluralFormatterTest; | |
89 FRIEND_TEST_ALL_PREFIXES(PluralFormatterTest, Basic); | |
90 FRIEND_TEST_ALL_PREFIXES(PluralFormatterTest, TestLocales); | |
91 | |
92 // Used by unit test to override resource string fetching function. | |
93 static void SetStringSource(StringSourceFunction string_source); | |
94 | |
95 // Used by unit test to cycle through locales. Set to NULL to reset | |
96 // to default. Must be set before creating the PluralFormatter | |
97 // object that will use it. | |
98 static void SetOverrideLocale(const char* override_locale); | |
99 | |
100 class PluralFormatterImpl; | |
tfarina
2011/04/12 01:25:01
Could you rename to simply Impl? So in the impleme
| |
101 | |
tfarina
2011/04/12 01:25:01
no blank line here too, please.
| |
102 PluralFormatterImpl* impl_; | |
103 }; | |
104 | |
105 #endif // CHROME_COMMON_PLURAL_FORMATTER_H_ | |
OLD | NEW |