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 <limits.h> | 7 #include <limits.h> |
8 | 8 |
| 9 #include <memory> |
9 #include <vector> | 10 #include <vector> |
10 | 11 |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "third_party/icu/source/common/unicode/unistr.h" | 13 #include "third_party/icu/source/common/unicode/unistr.h" |
14 #include "third_party/icu/source/i18n/unicode/msgfmt.h" | 14 #include "third_party/icu/source/i18n/unicode/msgfmt.h" |
15 #include "ui/base/l10n/l10n_util.h" | 15 #include "ui/base/l10n/l10n_util.h" |
16 #include "ui/strings/grit/ui_strings.h" | 16 #include "ui/strings/grit/ui_strings.h" |
17 | 17 |
18 namespace ui { | 18 namespace ui { |
19 | 19 |
20 UI_BASE_EXPORT bool formatter_force_fallback = false; | 20 UI_BASE_EXPORT bool formatter_force_fallback = false; |
21 | 21 |
22 struct Pluralities { | 22 struct Pluralities { |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 " other{# days and }" | 136 " other{# days and }" |
137 }; | 137 }; |
138 static const Pluralities IDS_DURATION_HOUR_2ND = { | 138 static const Pluralities IDS_DURATION_HOUR_2ND = { |
139 IDS_TIME_HOURS_2ND, | 139 IDS_TIME_HOURS_2ND, |
140 "one{# hour}", | 140 "one{# hour}", |
141 " other{# hours}" | 141 " other{# hours}" |
142 }; | 142 }; |
143 | 143 |
144 namespace { | 144 namespace { |
145 | 145 |
146 scoped_ptr<icu::PluralRules> BuildPluralRules() { | 146 std::unique_ptr<icu::PluralRules> BuildPluralRules() { |
147 UErrorCode err = U_ZERO_ERROR; | 147 UErrorCode err = U_ZERO_ERROR; |
148 scoped_ptr<icu::PluralRules> rules( | 148 std::unique_ptr<icu::PluralRules> rules( |
149 icu::PluralRules::forLocale(icu::Locale::getDefault(), err)); | 149 icu::PluralRules::forLocale(icu::Locale::getDefault(), err)); |
150 if (U_FAILURE(err)) { | 150 if (U_FAILURE(err)) { |
151 err = U_ZERO_ERROR; | 151 err = U_ZERO_ERROR; |
152 icu::UnicodeString fallback_rules("one: n is 1", -1, US_INV); | 152 icu::UnicodeString fallback_rules("one: n is 1", -1, US_INV); |
153 rules.reset(icu::PluralRules::createRules(fallback_rules, err)); | 153 rules.reset(icu::PluralRules::createRules(fallback_rules, err)); |
154 DCHECK(U_SUCCESS(err)); | 154 DCHECK(U_SUCCESS(err)); |
155 } | 155 } |
156 return rules; | 156 return rules; |
157 } | 157 } |
158 | 158 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 UErrorCode error = U_ZERO_ERROR; | 224 UErrorCode error = U_ZERO_ERROR; |
225 FormatNumberInPlural(*detailed_format_[units][0], value_1, | 225 FormatNumberInPlural(*detailed_format_[units][0], value_1, |
226 formatted_string, &error); | 226 formatted_string, &error); |
227 DCHECK(U_SUCCESS(error)); | 227 DCHECK(U_SUCCESS(error)); |
228 FormatNumberInPlural(*detailed_format_[units][1], value_2, | 228 FormatNumberInPlural(*detailed_format_[units][1], value_2, |
229 formatted_string, &error); | 229 formatted_string, &error); |
230 DCHECK(U_SUCCESS(error)); | 230 DCHECK(U_SUCCESS(error)); |
231 return; | 231 return; |
232 } | 232 } |
233 | 233 |
234 scoped_ptr<icu::MessageFormat> Formatter::CreateFallbackFormat( | 234 std::unique_ptr<icu::MessageFormat> Formatter::CreateFallbackFormat( |
235 const icu::PluralRules& rules, | 235 const icu::PluralRules& rules, |
236 const Pluralities& pluralities) const { | 236 const Pluralities& pluralities) const { |
237 icu::UnicodeString pattern("{NUMBER, plural, "); | 237 icu::UnicodeString pattern("{NUMBER, plural, "); |
238 if (rules.isKeyword(UNICODE_STRING_SIMPLE("one"))) | 238 if (rules.isKeyword(UNICODE_STRING_SIMPLE("one"))) |
239 pattern += icu::UnicodeString(pluralities.fallback_one); | 239 pattern += icu::UnicodeString(pluralities.fallback_one); |
240 pattern += icu::UnicodeString(pluralities.fallback_other); | 240 pattern += icu::UnicodeString(pluralities.fallback_other); |
241 pattern.append(UChar(0x7du)); // "}" = U+007D | 241 pattern.append(UChar(0x7du)); // "}" = U+007D |
242 | 242 |
243 UErrorCode error = U_ZERO_ERROR; | 243 UErrorCode error = U_ZERO_ERROR; |
244 scoped_ptr<icu::MessageFormat> format( | 244 std::unique_ptr<icu::MessageFormat> format( |
245 new icu::MessageFormat(pattern, error)); | 245 new icu::MessageFormat(pattern, error)); |
246 DCHECK(U_SUCCESS(error)); | 246 DCHECK(U_SUCCESS(error)); |
247 return format; | 247 return format; |
248 } | 248 } |
249 | 249 |
250 scoped_ptr<icu::MessageFormat> Formatter::InitFormat( | 250 std::unique_ptr<icu::MessageFormat> Formatter::InitFormat( |
251 const Pluralities& pluralities) { | 251 const Pluralities& pluralities) { |
252 if (!formatter_force_fallback) { | 252 if (!formatter_force_fallback) { |
253 base::string16 pattern = l10n_util::GetStringUTF16(pluralities.id); | 253 base::string16 pattern = l10n_util::GetStringUTF16(pluralities.id); |
254 UErrorCode error = U_ZERO_ERROR; | 254 UErrorCode error = U_ZERO_ERROR; |
255 scoped_ptr<icu::MessageFormat> format(new icu::MessageFormat( | 255 std::unique_ptr<icu::MessageFormat> format(new icu::MessageFormat( |
256 icu::UnicodeString(FALSE, pattern.data(), pattern.length()), error)); | 256 icu::UnicodeString(FALSE, pattern.data(), pattern.length()), error)); |
257 DCHECK(U_SUCCESS(error)); | 257 DCHECK(U_SUCCESS(error)); |
258 if (format.get()) | 258 if (format.get()) |
259 return format; | 259 return format; |
260 } | 260 } |
261 | 261 |
262 scoped_ptr<icu::PluralRules> rules(BuildPluralRules()); | 262 std::unique_ptr<icu::PluralRules> rules(BuildPluralRules()); |
263 return CreateFallbackFormat(*rules, pluralities); | 263 return CreateFallbackFormat(*rules, pluralities); |
264 } | 264 } |
265 | 265 |
266 const Formatter* FormatterContainer::Get(TimeFormat::Format format, | 266 const Formatter* FormatterContainer::Get(TimeFormat::Format format, |
267 TimeFormat::Length length) const { | 267 TimeFormat::Length length) const { |
268 DCHECK(formatter_[format][length]) | 268 DCHECK(formatter_[format][length]) |
269 << "Combination of FORMAT_ELAPSED and LENGTH_LONG is not implemented!"; | 269 << "Combination of FORMAT_ELAPSED and LENGTH_LONG is not implemented!"; |
270 return formatter_[format][length].get(); | 270 return formatter_[format][length].get(); |
271 } | 271 } |
272 | 272 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 | 314 |
315 void FormatterContainer::Shutdown() { | 315 void FormatterContainer::Shutdown() { |
316 for (int format = 0; format < TimeFormat::FORMAT_COUNT; ++format) { | 316 for (int format = 0; format < TimeFormat::FORMAT_COUNT; ++format) { |
317 for (int length = 0; length < TimeFormat::LENGTH_COUNT; ++length) { | 317 for (int length = 0; length < TimeFormat::LENGTH_COUNT; ++length) { |
318 formatter_[format][length].reset(); | 318 formatter_[format][length].reset(); |
319 } | 319 } |
320 } | 320 } |
321 } | 321 } |
322 | 322 |
323 } // namespace ui | 323 } // namespace ui |
OLD | NEW |