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

Side by Side Diff: chrome/common/plural_formatter.cc

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: upload after rebase Created 9 years, 2 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
« no previous file with comments | « chrome/common/plural_formatter.h ('k') | chrome/common/plural_formatter_example.grd » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #include "chrome/common/plural_formatter.h"
6
7 #include "base/scoped_ptr.h"
8 #include "base/stl_util-inl.h"
9 #include "base/utf_string_conversions.h"
10 #include "grit/generated_resources.h"
11 #include "ui/base/l10n/l10n_util.h"
12 #include "unicode/locid.h"
13 #include "unicode/plurfmt.h"
14 #include "unicode/plurrule.h"
15
16 namespace {
17
18 // This is the functor for mapping message IDs to strings. We reset
19 // it for tests so that we can avoid using grit for the tests.
20 PluralFormatter::StringSourceFunction g_string_source =
21 l10n_util::GetStringUTF8;
22
23 // This is used to override which locale is used for the translation.
24 // It is used by the tests to test different locales. If it is set to
25 // NULL, then the default locale is used.
26 const char* g_override_locale = NULL;
27
28 } // namespace
29
30 // An implementation class is necessary here because icu does some
31 // funky things with namespaces: it declares its stuff under a
32 // macro-constructed namespace name and then aliases it to 'icu',
33 // which means you have to include the icu header before any forward
34 // declaration (which defeats the purpose of forward declarations) in
35 // order to use the namespace "icu" or the macro.
36
37 class PluralFormatter::PluralFormatterImpl {
38 public:
39 PluralFormatterImpl();
40 bool Init(const int message_ids[]);
41 string16 GetPluralString(int number) const;
42 private:
43 scoped_ptr<icu::PluralFormat> formatter_;
44 };
45
46 PluralFormatter::PluralFormatterImpl::PluralFormatterImpl()
47 : formatter_(NULL) {}
48
49 bool PluralFormatter::PluralFormatterImpl::Init(const int message_ids[]) {
50 static const icu::UnicodeString kKeywords[] = {
51 UNICODE_STRING_SIMPLE("zero"), UNICODE_STRING_SIMPLE("one"),
52 UNICODE_STRING_SIMPLE("two"), UNICODE_STRING_SIMPLE("few"),
53 UNICODE_STRING_SIMPLE("many"), UNICODE_STRING_SIMPLE("other"),
54 };
55
56 UErrorCode error = U_ZERO_ERROR;
57 scoped_ptr<icu::PluralRules> rules;
58 if (g_override_locale) {
59 icu::Locale overridden_locale(g_override_locale);
60 rules.reset(icu::PluralRules::forLocale(overridden_locale, error));
61 } else {
62 rules.reset(icu::PluralRules::forLocale(icu::Locale::getDefault(), error));
63 }
64
65 // If we fail to create the rules for the given locale, we use
66 // fallback rules.
67 if (U_FAILURE(error)) {
68 error = U_ZERO_ERROR;
69 icu::UnicodeString fallback_rules("one: n is 1", -1, US_INV);
70 rules.reset(icu::PluralRules::createRules(fallback_rules, error));
71 if (U_FAILURE(error)) {
72 NOTREACHED() << "Failed to create fallback rule.";
73 return false;
74 }
75 }
76
77 icu::UnicodeString pattern;
78 for (size_t j = 0; j < arraysize(kKeywords); ++j) {
79 int msg_id = message_ids[j];
80 std::string sub_pattern = (*g_string_source)(msg_id);
81 // NA means this keyword is not used in the current locale. Even
82 // if a translator translated for this keyword, we do not use it
83 // unless it's 'other' or it's defined in the rules for the
84 // current locale. Special-casing of 'other' (j = 5) will be
85 // removed once ICU's isKeyword is fixed to return true for
86 // isKeyword("other").
87 if (sub_pattern.compare("NA") != 0 &&
88 (j == 5 || rules->isKeyword(kKeywords[j]))) {
89 pattern += kKeywords[j];
90 pattern += UNICODE_STRING_SIMPLE("{");
91 pattern += icu::UnicodeString(sub_pattern.c_str(), "UTF-8");
92 pattern += UNICODE_STRING_SIMPLE("}");
93 }
94 }
95
96 scoped_ptr<icu::PluralFormat> format(
97 new icu::PluralFormat(*rules, pattern, error));
98
99 // If format creation fails, we fail.
100 if (U_FAILURE(error)) {
101 formatter_.reset(NULL);
102 return false;
103 }
104
105 formatter_.swap(format);
106 return true;
107 }
108
109 string16 PluralFormatter::PluralFormatterImpl::GetPluralString(
110 int number) const {
111 DCHECK(formatter_.get() != NULL)
112 << "GetPluralString called before initializing formatter.";
113
114 icu::UnicodeString plural_string;
115 UErrorCode error = U_ZERO_ERROR;
116 plural_string = formatter_->format(number, error);
117
118 string16 result;
119 if (U_FAILURE(error)) {
120 NOTREACHED() << "Failed to format plural string.";
121 result.clear();
122 return result;
123 }
124
125 int capacity = plural_string.length() + 1;
126 plural_string.extract(static_cast<UChar*>(WriteInto(&result, capacity)),
127 capacity, error);
128
129 // On error, result will be the empty string.
130 if (U_FAILURE(error)) {
131 NOTREACHED() << "Failed to extract plural string.";
132 result.clear();
133 }
134
135 return result;
136 }
137
138 /////////////////////////////////////////////////////
139 // Implementation of the main class PluralFormatter.
140
141 PluralFormatter::PluralFormatter() : impl_(NULL) {}
142
143 PluralFormatter::~PluralFormatter() {
144 delete impl_;
145 }
146
147 bool PluralFormatter::Init(const int message_ids[]) {
148 impl_ = new PluralFormatter::PluralFormatterImpl;
149 return impl_->Init(message_ids);
150 }
151
152 bool PluralFormatter::Init(int zero_message_id,
153 int one_message_id,
154 int two_message_id,
155 int few_message_id,
156 int many_message_id,
157 int default_message_id) {
158 int message_ids[6];
159 message_ids[0] = zero_message_id;
160 message_ids[1] = one_message_id;
161 message_ids[2] = two_message_id;
162 message_ids[3] = few_message_id;
163 message_ids[4] = many_message_id;
164 message_ids[5] = default_message_id;
165 return Init(message_ids);
166 }
167
168 string16 PluralFormatter::GetPluralString(int number) const {
169 DCHECK(impl_ != NULL) << "GetPluralString called before Init";
170 return impl_->GetPluralString(number);
171 }
172
173 void PluralFormatter::SetStringSource(StringSourceFunction string_source) {
174 g_string_source = string_source;
175 }
176
177 void PluralFormatter::SetOverrideLocale(const char* overrride_locale) {
178 g_override_locale = overrride_locale;
179 }
OLDNEW
« no previous file with comments | « chrome/common/plural_formatter.h ('k') | chrome/common/plural_formatter_example.grd » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698