OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/base/l10n/formatter.h" | 5 #include "ui/base/l10n/formatter.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 scoped_ptr<icu::PluralRules> BuildPluralRules() { | 144 scoped_ptr<icu::PluralRules> BuildPluralRules() { |
145 UErrorCode err = U_ZERO_ERROR; | 145 UErrorCode err = U_ZERO_ERROR; |
146 scoped_ptr<icu::PluralRules> rules( | 146 scoped_ptr<icu::PluralRules> rules( |
147 icu::PluralRules::forLocale(icu::Locale::getDefault(), err)); | 147 icu::PluralRules::forLocale(icu::Locale::getDefault(), err)); |
148 if (U_FAILURE(err)) { | 148 if (U_FAILURE(err)) { |
149 err = U_ZERO_ERROR; | 149 err = U_ZERO_ERROR; |
150 icu::UnicodeString fallback_rules("one: n is 1", -1, US_INV); | 150 icu::UnicodeString fallback_rules("one: n is 1", -1, US_INV); |
151 rules.reset(icu::PluralRules::createRules(fallback_rules, err)); | 151 rules.reset(icu::PluralRules::createRules(fallback_rules, err)); |
152 DCHECK(U_SUCCESS(err)); | 152 DCHECK(U_SUCCESS(err)); |
153 } | 153 } |
154 return rules.Pass(); | 154 return rules; |
155 } | 155 } |
156 | 156 |
157 void FormatNumberInPlural(const icu::MessageFormat& format, int number, | 157 void FormatNumberInPlural(const icu::MessageFormat& format, int number, |
158 icu::UnicodeString* result, UErrorCode* err) { | 158 icu::UnicodeString* result, UErrorCode* err) { |
159 if (U_FAILURE(*err)) return; | 159 if (U_FAILURE(*err)) return; |
160 icu::Formattable formattable(number); | 160 icu::Formattable formattable(number); |
161 icu::FieldPosition ignore(icu::FieldPosition::DONT_CARE); | 161 icu::FieldPosition ignore(icu::FieldPosition::DONT_CARE); |
162 format.format(&formattable, 1, *result, ignore, *err); | 162 format.format(&formattable, 1, *result, ignore, *err); |
163 DCHECK(U_SUCCESS(*err)); | 163 DCHECK(U_SUCCESS(*err)); |
164 return; | 164 return; |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 icu::UnicodeString pattern("{NUMBER, plural, "); | 235 icu::UnicodeString pattern("{NUMBER, plural, "); |
236 if (rules.isKeyword(UNICODE_STRING_SIMPLE("one"))) | 236 if (rules.isKeyword(UNICODE_STRING_SIMPLE("one"))) |
237 pattern += icu::UnicodeString(pluralities.fallback_one); | 237 pattern += icu::UnicodeString(pluralities.fallback_one); |
238 pattern += icu::UnicodeString(pluralities.fallback_other); | 238 pattern += icu::UnicodeString(pluralities.fallback_other); |
239 pattern.append(UChar(0x7du)); // "}" = U+007D | 239 pattern.append(UChar(0x7du)); // "}" = U+007D |
240 | 240 |
241 UErrorCode error = U_ZERO_ERROR; | 241 UErrorCode error = U_ZERO_ERROR; |
242 scoped_ptr<icu::MessageFormat> format( | 242 scoped_ptr<icu::MessageFormat> format( |
243 new icu::MessageFormat(pattern, error)); | 243 new icu::MessageFormat(pattern, error)); |
244 DCHECK(U_SUCCESS(error)); | 244 DCHECK(U_SUCCESS(error)); |
245 return format.Pass(); | 245 return format; |
246 } | 246 } |
247 | 247 |
248 scoped_ptr<icu::MessageFormat> Formatter::InitFormat( | 248 scoped_ptr<icu::MessageFormat> Formatter::InitFormat( |
249 const Pluralities& pluralities) { | 249 const Pluralities& pluralities) { |
250 if (!formatter_force_fallback) { | 250 if (!formatter_force_fallback) { |
251 base::string16 pattern = l10n_util::GetStringUTF16(pluralities.id); | 251 base::string16 pattern = l10n_util::GetStringUTF16(pluralities.id); |
252 UErrorCode error = U_ZERO_ERROR; | 252 UErrorCode error = U_ZERO_ERROR; |
253 scoped_ptr<icu::MessageFormat> format(new icu::MessageFormat( | 253 scoped_ptr<icu::MessageFormat> format(new icu::MessageFormat( |
254 icu::UnicodeString(FALSE, pattern.data(), pattern.length()), error)); | 254 icu::UnicodeString(FALSE, pattern.data(), pattern.length()), error)); |
255 DCHECK(U_SUCCESS(error)); | 255 DCHECK(U_SUCCESS(error)); |
256 if (format.get()) | 256 if (format.get()) |
257 return format.Pass(); | 257 return format; |
258 } | 258 } |
259 | 259 |
260 scoped_ptr<icu::PluralRules> rules(BuildPluralRules()); | 260 scoped_ptr<icu::PluralRules> rules(BuildPluralRules()); |
261 return CreateFallbackFormat(*rules, pluralities); | 261 return CreateFallbackFormat(*rules, pluralities); |
262 } | 262 } |
263 | 263 |
264 const Formatter* FormatterContainer::Get(TimeFormat::Format format, | 264 const Formatter* FormatterContainer::Get(TimeFormat::Format format, |
265 TimeFormat::Length length) const { | 265 TimeFormat::Length length) const { |
266 DCHECK(formatter_[format][length]) | 266 DCHECK(formatter_[format][length]) |
267 << "Combination of FORMAT_ELAPSED and LENGTH_LONG is not implemented!"; | 267 << "Combination of FORMAT_ELAPSED and LENGTH_LONG is not implemented!"; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 | 312 |
313 void FormatterContainer::Shutdown() { | 313 void FormatterContainer::Shutdown() { |
314 for (int format = 0; format < TimeFormat::FORMAT_COUNT; ++format) { | 314 for (int format = 0; format < TimeFormat::FORMAT_COUNT; ++format) { |
315 for (int length = 0; length < TimeFormat::LENGTH_COUNT; ++length) { | 315 for (int length = 0; length < TimeFormat::LENGTH_COUNT; ++length) { |
316 formatter_[format][length].reset(); | 316 formatter_[format][length].reset(); |
317 } | 317 } |
318 } | 318 } |
319 } | 319 } |
320 | 320 |
321 } // namespace ui | 321 } // namespace ui |
OLD | NEW |