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

Side by Side Diff: ui/base/l10n/time_format.cc

Issue 23591040: Use ICU for string pluralization in the extension permission dialog. (Closed) Base URL: http://git.chromium.org/chromium/src.git@gtk-extension-install-dialog
Patch Set: Created 7 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
« no previous file with comments | « ui/base/l10n/l10n_util_plurals.cc ('k') | ui/ui.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 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/time_format.h" 5 #include "ui/base/l10n/time_format.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/lazy_instance.h" 9 #include "base/lazy_instance.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/scoped_vector.h" 12 #include "base/memory/scoped_vector.h"
13 #include "base/stl_util.h" 13 #include "base/stl_util.h"
14 #include "base/strings/string16.h" 14 #include "base/strings/string16.h"
15 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
16 #include "base/time/time.h" 16 #include "base/time/time.h"
17 #include "grit/ui_strings.h" 17 #include "grit/ui_strings.h"
18 #include "third_party/icu/source/common/unicode/locid.h" 18 #include "third_party/icu/source/common/unicode/locid.h"
19 #include "third_party/icu/source/i18n/unicode/datefmt.h" 19 #include "third_party/icu/source/i18n/unicode/datefmt.h"
20 #include "third_party/icu/source/i18n/unicode/plurfmt.h" 20 #include "third_party/icu/source/i18n/unicode/plurfmt.h"
21 #include "third_party/icu/source/i18n/unicode/plurrule.h" 21 #include "third_party/icu/source/i18n/unicode/plurrule.h"
22 #include "third_party/icu/source/i18n/unicode/smpdtfmt.h" 22 #include "third_party/icu/source/i18n/unicode/smpdtfmt.h"
23 #include "ui/base/l10n/l10n_util.h" 23 #include "ui/base/l10n/l10n_util.h"
24 #include "ui/base/l10n/l10n_util_plurals.h"
24 25
25 using base::Time; 26 using base::Time;
26 using base::TimeDelta; 27 using base::TimeDelta;
27 28
28 namespace { 29 namespace {
29 30
30 static const char kFallbackFormatSuffixShort[] = "}"; 31 static const char kFallbackFormatSuffixShort[] = "}";
31 static const char kFallbackFormatSuffixLeft[] = " left}"; 32 static const char kFallbackFormatSuffixLeft[] = " left}";
32 static const char kFallbackFormatSuffixAgo[] = " ago}"; 33 static const char kFallbackFormatSuffixAgo[] = " ago}";
33 34
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 const icu::PluralRules& rules, int index, FormatType format_type); 234 const icu::PluralRules& rules, int index, FormatType format_type);
234 235
235 DISALLOW_COPY_AND_ASSIGN(TimeFormatter); 236 DISALLOW_COPY_AND_ASSIGN(TimeFormatter);
236 }; 237 };
237 238
238 static base::LazyInstance<TimeFormatter> g_time_formatter = 239 static base::LazyInstance<TimeFormatter> g_time_formatter =
239 LAZY_INSTANCE_INITIALIZER; 240 LAZY_INSTANCE_INITIALIZER;
240 241
241 void TimeFormatter::BuildFormats( 242 void TimeFormatter::BuildFormats(
242 FormatType format_type, ScopedVector<icu::PluralFormat>* time_formats) { 243 FormatType format_type, ScopedVector<icu::PluralFormat>* time_formats) {
243 const icu::UnicodeString kKeywords[] = {
244 UNICODE_STRING_SIMPLE("other"), UNICODE_STRING_SIMPLE("one"),
245 UNICODE_STRING_SIMPLE("zero"), UNICODE_STRING_SIMPLE("two"),
246 UNICODE_STRING_SIMPLE("few"), UNICODE_STRING_SIMPLE("many")
247 };
248 UErrorCode err = U_ZERO_ERROR;
249 scoped_ptr<icu::PluralRules> rules(
250 icu::PluralRules::forLocale(icu::Locale::getDefault(), err));
251 if (U_FAILURE(err)) {
252 err = U_ZERO_ERROR;
253 icu::UnicodeString fallback_rules("one: n is 1", -1, US_INV);
254 rules.reset(icu::PluralRules::createRules(fallback_rules, err));
255 DCHECK(U_SUCCESS(err));
256 }
257
258 const MessageIDs& message_ids = GetMessageIDs(format_type); 244 const MessageIDs& message_ids = GetMessageIDs(format_type);
259 245
260 for (int i = 0; i < 4; ++i) { 246 for (int i = 0; i < 4; ++i) {
261 icu::UnicodeString pattern; 247 icu::UnicodeString pattern;
262 for (size_t j = 0; j < arraysize(kKeywords); ++j) { 248 std::vector<int> ids;
263 int msg_id = message_ids.ids[i][j]; 249 for (size_t j = 0; j < arraysize(message_ids.ids[i]); ++j) {
264 std::string sub_pattern = l10n_util::GetStringUTF8(msg_id); 250 ids.push_back(message_ids.ids[i][j]);
265 // NA means this keyword is not used in the current locale.
266 // Even if a translator translated for this keyword, we do not
267 // use it unless it's 'other' (j=0) or it's defined in the rules
268 // for the current locale. Special-casing of 'other' will be removed
269 // once ICU's isKeyword is fixed to return true for isKeyword('other').
270 if (sub_pattern.compare("NA") != 0 &&
271 (j == 0 || rules->isKeyword(kKeywords[j]))) {
272 pattern += kKeywords[j];
273 pattern += UNICODE_STRING_SIMPLE("{");
274 pattern += icu::UnicodeString(sub_pattern.c_str(), "UTF-8");
275 pattern += UNICODE_STRING_SIMPLE("}");
276 }
277 } 251 }
278 icu::PluralFormat* format = new icu::PluralFormat(*rules, pattern, err); 252 scoped_ptr<icu::PluralFormat> format = l10n_util::BuildPluralFormat(ids);
279 if (U_SUCCESS(err)) { 253 if (format) {
280 time_formats->push_back(format); 254 time_formats->push_back(format.release());
281 } else { 255 } else {
282 delete format; 256 scoped_ptr<icu::PluralRules> rules(l10n_util::BuildPluralRules());
283 time_formats->push_back(createFallbackFormat(*rules, i, format_type)); 257 time_formats->push_back(createFallbackFormat(*rules, i, format_type));
284 // Reset it so that next ICU call can proceed.
285 err = U_ZERO_ERROR;
286 } 258 }
287 } 259 }
288 } 260 }
289 261
290 // Create a hard-coded fallback plural format. This will never be called 262 // Create a hard-coded fallback plural format. This will never be called
291 // unless translators make a mistake. 263 // unless translators make a mistake.
292 icu::PluralFormat* TimeFormatter::createFallbackFormat( 264 icu::PluralFormat* TimeFormatter::createFallbackFormat(
293 const icu::PluralRules& rules, int index, FormatType format_type) { 265 const icu::PluralRules& rules, int index, FormatType format_type) {
294 const icu::UnicodeString kUnits[4][2] = { 266 const icu::UnicodeString kUnits[4][2] = {
295 { UNICODE_STRING_SIMPLE("sec"), UNICODE_STRING_SIMPLE("secs") }, 267 { UNICODE_STRING_SIMPLE("sec"), UNICODE_STRING_SIMPLE("secs") },
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
399 if (time >= tomorrow) 371 if (time >= tomorrow)
400 return string16(); 372 return string16();
401 else if (time >= midnight_today) 373 else if (time >= midnight_today)
402 return l10n_util::GetStringUTF16(IDS_PAST_TIME_TODAY); 374 return l10n_util::GetStringUTF16(IDS_PAST_TIME_TODAY);
403 else if (time >= yesterday) 375 else if (time >= yesterday)
404 return l10n_util::GetStringUTF16(IDS_PAST_TIME_YESTERDAY); 376 return l10n_util::GetStringUTF16(IDS_PAST_TIME_YESTERDAY);
405 return string16(); 377 return string16();
406 } 378 }
407 379
408 } // namespace ui 380 } // namespace ui
OLDNEW
« no previous file with comments | « ui/base/l10n/l10n_util_plurals.cc ('k') | ui/ui.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698