Chromium Code Reviews| 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> | |
|
tfarina
2011/03/26 02:05:15
is this necessary/used in this source file?
Greg Spencer (Chromium)
2011/03/28 23:13:37
Nope, not any more. Removed.
| |
| 8 | |
| 9 #include "base/scoped_ptr.h" | |
| 10 #include "base/stl_util-inl.h" | |
| 11 #include "base/string16.h" | |
|
tfarina
2011/03/26 02:05:15
you can remove this include.
Greg Spencer (Chromium)
2011/03/28 23:13:37
Done.
| |
| 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 using grit for the tests. | |
| 23 static PluralFormatter::StringSourceFunction g_string_source = | |
|
tfarina
2011/03/26 02:05:15
the static keyword is not necessary. Please, could
Greg Spencer (Chromium)
2011/03/28 23:13:37
Done.
| |
| 24 l10n_util::GetStringUTF8; | |
| 25 | |
| 26 // This is used to override which locale is used for the translation. | |
| 27 // It is used by the tests to test different locales. If it is set to | |
| 28 // NULL, then the default locale is used. | |
| 29 static const char* g_override_locale = NULL; | |
| 30 | |
| 31 } // anonymous namespace | |
|
tfarina
2011/03/26 02:05:15
Although I see in many places, and it's misleading
Greg Spencer (Chromium)
2011/03/28 23:13:37
I hate that convention: it looks like someone forg
| |
| 32 | |
| 33 // An implementation class is necessary here because icu does some | |
| 34 // funky things with namespaces: it declares its stuff under a | |
| 35 // macro-constructed namespace name and then aliases it to 'icu', | |
| 36 // which means you have to include the icu header before any forward | |
| 37 // declaration (which defeats the purpose of forward declarations) in | |
| 38 // order to use the namespace "icu" or the macro. | |
| 39 | |
| 40 class PluralFormatterImpl { | |
| 41 public: | |
| 42 PluralFormatterImpl(); | |
| 43 bool Init(const int message_ids[]); | |
| 44 string16 GetPluralString(int number) const; | |
| 45 private: | |
| 46 scoped_ptr<icu::PluralFormat> formatter_; | |
| 47 }; | |
| 48 | |
| 49 PluralFormatterImpl::PluralFormatterImpl() : formatter_(NULL) {} | |
| 50 | |
| 51 bool PluralFormatterImpl::Init(const int message_ids[]) { | |
| 52 static const icu::UnicodeString kKeywords[] = { | |
| 53 UNICODE_STRING_SIMPLE("zero"), UNICODE_STRING_SIMPLE("one"), | |
| 54 UNICODE_STRING_SIMPLE("two"), UNICODE_STRING_SIMPLE("few"), | |
| 55 UNICODE_STRING_SIMPLE("many"), UNICODE_STRING_SIMPLE("other"), | |
| 56 }; | |
| 57 | |
| 58 UErrorCode err = U_ZERO_ERROR; | |
| 59 scoped_ptr<icu::PluralRules> rules; | |
| 60 if (g_override_locale) { | |
| 61 icu::Locale overridden_locale(g_override_locale); | |
| 62 rules.reset(icu::PluralRules::forLocale(overridden_locale, err)); | |
| 63 } else { | |
| 64 rules.reset(icu::PluralRules::forLocale(icu::Locale::getDefault(), err)); | |
| 65 } | |
| 66 | |
| 67 // If we fail to create the rules for the given locale, we use | |
| 68 // fallback rules. | |
| 69 if (U_FAILURE(err)) { | |
| 70 err = U_ZERO_ERROR; | |
| 71 icu::UnicodeString fallback_rules("one: n is 1", -1, US_INV); | |
| 72 rules.reset(icu::PluralRules::createRules(fallback_rules, err)); | |
| 73 DCHECK(U_SUCCESS(err)); | |
| 74 } | |
| 75 | |
| 76 icu::UnicodeString pattern; | |
| 77 for (size_t j = 0; j < arraysize(kKeywords); ++j) { | |
| 78 int msg_id = message_ids[j]; | |
| 79 std::string sub_pattern = (*g_string_source)(msg_id); | |
| 80 // NA means this keyword is not used in the current locale. Even | |
| 81 // if a translator translated for this keyword, we do not use it | |
| 82 // unless it's 'other' or it's defined in the rules for the | |
| 83 // current locale. Special-casing of 'other' (j = 5) will be | |
| 84 // removed once ICU's isKeyword is fixed to return true for | |
| 85 // isKeyword('other'). | |
| 86 if (sub_pattern.compare("NA") != 0 && | |
| 87 (j == 5 || rules->isKeyword(kKeywords[j]))) { | |
| 88 pattern += kKeywords[j]; | |
| 89 pattern += UNICODE_STRING_SIMPLE("{"); | |
| 90 pattern += icu::UnicodeString(sub_pattern.c_str(), "UTF-8"); | |
| 91 pattern += UNICODE_STRING_SIMPLE("}"); | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 scoped_ptr<icu::PluralFormat> format( | |
| 96 new icu::PluralFormat(*rules, pattern, err)); | |
| 97 | |
| 98 // If format creation fails, we fail. | |
| 99 if (U_FAILURE(err)) { | |
| 100 formatter_.reset(NULL); | |
| 101 return false; | |
| 102 } | |
| 103 | |
| 104 formatter_.swap(format); | |
| 105 return true; | |
| 106 } | |
| 107 | |
| 108 string16 PluralFormatterImpl::GetPluralString(int number) const { | |
| 109 DCHECK(formatter_.get() != NULL) | |
| 110 << "GetPluralString called before initializing formatter."; | |
| 111 icu::UnicodeString plural_string; | |
| 112 UErrorCode error = U_ZERO_ERROR; | |
| 113 plural_string = formatter_->format(number, error); | |
| 114 DCHECK(U_SUCCESS(error)); | |
|
Paweł Hajdan Jr.
2011/03/28 18:52:29
Later case handles U_FAILURE, and this only DCHECK
Greg Spencer (Chromium)
2011/03/28 23:13:37
OK, I added handling for this to make it consisten
| |
| 115 string16 result; | |
| 116 int capacity = plural_string.length() + 1; | |
| 117 plural_string.extract(static_cast<UChar*>(WriteInto(&result, capacity)), | |
| 118 capacity, error); | |
| 119 DCHECK(U_SUCCESS(error)); | |
|
Paweł Hajdan Jr.
2011/03/28 18:52:29
nit: I'd suggest to make this a NOTREACHED() in th
Greg Spencer (Chromium)
2011/03/28 23:13:37
NOTREACHED is a good suggestion. I switched to th
| |
| 120 // On error, result will be the empty string. | |
| 121 if (U_FAILURE(error)) | |
| 122 result.clear(); | |
| 123 | |
| 124 return result; | |
| 125 } | |
| 126 | |
| 127 ///////////////////////////////////////////////////// | |
| 128 // Implementation of the main class PluralFormatter. | |
| 129 | |
| 130 PluralFormatter::PluralFormatter() : impl_(NULL) {} | |
| 131 | |
| 132 PluralFormatter::~PluralFormatter() { | |
| 133 delete impl_; | |
| 134 } | |
| 135 | |
| 136 bool PluralFormatter::Init(int zero_message_id, | |
| 137 int one_message_id, | |
| 138 int two_message_id, | |
| 139 int few_message_id, | |
| 140 int many_message_id, | |
| 141 int default_message_id) { | |
| 142 int message_ids[6]; | |
| 143 message_ids[0] = zero_message_id; | |
| 144 message_ids[1] = one_message_id; | |
| 145 message_ids[2] = two_message_id; | |
| 146 message_ids[3] = few_message_id; | |
| 147 message_ids[4] = many_message_id; | |
| 148 message_ids[5] = default_message_id; | |
| 149 return Init(message_ids); | |
| 150 } | |
| 151 | |
| 152 bool PluralFormatter::Init(const int message_ids[]) { | |
|
tfarina
2011/03/26 02:05:15
this should be implemented above. The order of the
Greg Spencer (Chromium)
2011/03/28 23:13:37
Sorry, missed one. Done.
| |
| 153 impl_ = new PluralFormatterImpl; | |
| 154 return impl_->Init(message_ids); | |
| 155 } | |
| 156 | |
| 157 string16 PluralFormatter::GetPluralString(int number) const { | |
| 158 DCHECK(impl_ != NULL) << "GetPluralString called before Init"; | |
| 159 return impl_->GetPluralString(number); | |
| 160 } | |
| 161 | |
| 162 void PluralFormatter::SetStringSource(StringSourceFunction string_source) { | |
| 163 g_string_source = string_source; | |
| 164 } | |
| 165 | |
| 166 void PluralFormatter::SetOverrideLocale(const char* overrride_locale) { | |
| 167 g_override_locale = overrride_locale; | |
| 168 } | |
| OLD | NEW |