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 #include "chrome/common/plural_formatter.h" | |
6 | |
7 #include <vector> | |
8 | |
9 #include "base/scoped_ptr.h" | |
10 #include "base/stl_util-inl.h" | |
11 #include "base/string16.h" | |
12 #include "base/utf_string_conversions.h" | |
13 #include "grit/generated_resources.h" | |
14 #include "ui/base/l10n/l10n_util.h" | |
15 #include "unicode/locid.h" | |
16 #include "unicode/plurfmt.h" | |
17 #include "unicode/plurrule.h" | |
18 | |
19 namespace { | |
20 | |
21 // This is the functor for mapping message IDs to strings. We reset | |
22 // it for tests so that we can avoid grit for the tests. | |
23 static PluralFormatter::StringSourceFunction g_string_source = | |
24 l10n_util::GetStringUTF8; | |
25 | |
26 static const char* g_override_locale = NULL; | |
27 } // anonymous namespace | |
Paweł Hajdan Jr.
2011/03/25 09:32:19
nit: Add empty line above.
Greg Spencer (Chromium)
2011/03/25 23:05:31
Done.
| |
28 | |
29 class PluralFormatterImpl { | |
Paweł Hajdan Jr.
2011/03/25 09:32:19
Is it worth to have an Impl class here? How about
Greg Spencer (Chromium)
2011/03/25 23:05:31
This is a good point, and I considered it, but an
Paweł Hajdan Jr.
2011/03/28 18:52:28
Makes sense.
| |
30 public: | |
31 explicit PluralFormatterImpl(const int message_ids[]); | |
32 string16 GetPluralString(int number); | |
33 private: | |
34 scoped_ptr<icu::PluralFormat> formatter_; | |
35 }; | |
36 | |
37 PluralFormatterImpl::PluralFormatterImpl(const int message_ids[]) { | |
38 static const icu::UnicodeString kKeywords[] = { | |
39 UNICODE_STRING_SIMPLE("zero"), UNICODE_STRING_SIMPLE("one"), | |
40 UNICODE_STRING_SIMPLE("two"), UNICODE_STRING_SIMPLE("few"), | |
41 UNICODE_STRING_SIMPLE("many"), UNICODE_STRING_SIMPLE("other"), | |
42 }; | |
43 | |
44 UErrorCode err = U_ZERO_ERROR; | |
45 scoped_ptr<icu::PluralRules> rules; | |
46 if (g_override_locale) { | |
47 icu::Locale overridden_locale(g_override_locale); | |
48 rules.reset(icu::PluralRules::forLocale(overridden_locale, err)); | |
49 } else { | |
50 rules.reset(icu::PluralRules::forLocale(icu::Locale::getDefault(), err)); | |
51 } | |
52 if (U_FAILURE(err)) { | |
53 err = U_ZERO_ERROR; | |
54 icu::UnicodeString fallback_rules("one: n is 1", -1, US_INV); | |
55 rules.reset(icu::PluralRules::createRules(fallback_rules, err)); | |
56 DCHECK(U_SUCCESS(err)); | |
Paweł Hajdan Jr.
2011/03/25 09:32:19
DCHECKs are only for Debug builds. Don't you want
Greg Spencer (Chromium)
2011/03/25 23:05:31
In this case, we want to fall back to a simple rul
Paweł Hajdan Jr.
2011/03/28 18:52:28
My understanding of this code is that this DCHECK
| |
57 } | |
58 | |
59 icu::UnicodeString pattern; | |
60 for (size_t j = 0; j < arraysize(kKeywords); ++j) { | |
61 int msg_id = message_ids[j]; | |
62 std::string sub_pattern = (*g_string_source)(msg_id); | |
63 // NA means this keyword is not used in the current locale. Even | |
64 // if a translator translated for this keyword, we do not use it | |
65 // unless it's 'other' or it's defined in the rules for the | |
66 // current locale. Special-casing of 'other' (j = 5) will be | |
67 // removed once ICU's isKeyword is fixed to return true for | |
68 // isKeyword('other'). | |
69 if (sub_pattern.compare("NA") != 0 && | |
70 (j == 5 || rules->isKeyword(kKeywords[j]))) { | |
71 pattern += kKeywords[j]; | |
72 pattern += UNICODE_STRING_SIMPLE("{"); | |
73 pattern += icu::UnicodeString(sub_pattern.c_str(), "UTF-8"); | |
74 pattern += UNICODE_STRING_SIMPLE("}"); | |
75 } | |
76 } | |
77 | |
78 scoped_ptr<icu::PluralFormat> format( | |
79 new icu::PluralFormat(*rules, pattern, err)); | |
80 DCHECK(U_SUCCESS(err)); | |
Paweł Hajdan Jr.
2011/03/25 09:32:19
Similarly here, I think you want to always check t
Greg Spencer (Chromium)
2011/03/25 23:05:31
I had that at first, and went back to DCHECK to be
| |
81 formatter_.swap(format); | |
82 } | |
83 | |
84 string16 PluralFormatterImpl::GetPluralString(int number) { | |
85 icu::UnicodeString plural_string; | |
86 UErrorCode error = U_ZERO_ERROR; | |
87 plural_string = formatter_->format(number, error); | |
88 DCHECK(U_SUCCESS(error)); | |
89 string16 result; | |
90 int capacity = plural_string.length() + 1; | |
91 plural_string.extract(static_cast<UChar*>(WriteInto(&result, capacity)), | |
92 capacity, error); | |
93 DCHECK(U_SUCCESS(error)); | |
94 return result; | |
95 } | |
96 | |
97 PluralFormatter::PluralFormatter(int zero_message_id, | |
98 int one_message_id, | |
99 int two_message_id, | |
100 int few_message_id, | |
101 int many_message_id, | |
102 int default_message_id) { | |
103 int message_ids[6]; | |
104 message_ids[0] = zero_message_id; | |
105 message_ids[1] = one_message_id; | |
106 message_ids[2] = two_message_id; | |
107 message_ids[3] = few_message_id; | |
108 message_ids[4] = many_message_id; | |
109 message_ids[5] = default_message_id; | |
110 impl_ = new PluralFormatterImpl(message_ids); | |
111 } | |
112 | |
113 PluralFormatter::PluralFormatter(const int message_ids[]) | |
114 : impl_(new PluralFormatterImpl(message_ids)) { | |
115 } | |
116 | |
117 PluralFormatter::~PluralFormatter() { | |
118 delete impl_; | |
119 } | |
120 | |
121 string16 PluralFormatter::GetPluralString(int number) { | |
122 return impl_->GetPluralString(number); | |
123 } | |
124 | |
125 void PluralFormatter::SetStringSource(StringSourceFunction string_source) { | |
126 g_string_source = string_source; | |
127 } | |
128 | |
129 void PluralFormatter::SetOverrideLocale(const char* overrride_locale) { | |
130 g_override_locale = overrride_locale; | |
131 } | |
OLD | NEW |