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

Side by Side Diff: chrome/browser/autofill/autofill_country.cc

Issue 10224004: Use Android API for GetDisplayNameForLocale(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 8 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 | Annotate | Revision Log
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 "chrome/browser/autofill/autofill_country.h" 5 #include "chrome/browser/autofill/autofill_country.h"
6 6
7 #include <map> 7 #include <map>
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <stdint.h> 9 #include <stdint.h>
10 #include <utility> 10 #include <utility>
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after
383 383
384 // Returns an ICU collator -- i.e. string comparator -- appropriate for the 384 // Returns an ICU collator -- i.e. string comparator -- appropriate for the
385 // given |locale|. 385 // given |locale|.
386 icu::Collator* GetCollatorForLocale(const std::string& locale); 386 icu::Collator* GetCollatorForLocale(const std::string& locale);
387 387
388 // Returns the ICU sort key corresponding to |str| for the given |collator|. 388 // Returns the ICU sort key corresponding to |str| for the given |collator|.
389 // Uses |buffer| as temporary storage, and might resize |buffer| as a side- 389 // Uses |buffer| as temporary storage, and might resize |buffer| as a side-
390 // effect. |buffer_size| should specify the |buffer|'s size, and is updated if 390 // effect. |buffer_size| should specify the |buffer|'s size, and is updated if
391 // the |buffer| is resized. 391 // the |buffer| is resized.
392 const std::string GetSortKey(const icu::Collator& collator, 392 const std::string GetSortKey(const icu::Collator& collator,
393 const icu::UnicodeString& str, 393 const string16& str,
394 scoped_array<uint8_t>* buffer, 394 scoped_array<uint8_t>* buffer,
395 int32_t* buffer_size) const; 395 int32_t* buffer_size) const;
396 396
397 397
398 // Maps from common country names, including 2- and 3-letter country codes, 398 // Maps from common country names, including 2- and 3-letter country codes,
399 // to the corresponding 2-letter country codes. The keys are uppercase ASCII 399 // to the corresponding 2-letter country codes. The keys are uppercase ASCII
400 // strings. 400 // strings.
401 std::map<std::string, std::string> common_names_; 401 std::map<std::string, std::string> common_names_;
402 402
403 // The outer map keys are ICU locale identifiers. 403 // The outer map keys are ICU locale identifiers.
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
476 } 476 }
477 477
478 void CountryNames::AddLocalizedNamesForLocale(const std::string& locale) { 478 void CountryNames::AddLocalizedNamesForLocale(const std::string& locale) {
479 // Nothing to do if we've previously added the localized names for the given 479 // Nothing to do if we've previously added the localized names for the given
480 // |locale|. 480 // |locale|.
481 if (locales_to_localized_names_.count(locale)) 481 if (locales_to_localized_names_.count(locale))
482 return; 482 return;
483 483
484 std::map<std::string, std::string> localized_names; 484 std::map<std::string, std::string> localized_names;
485 485
486 icu::Locale icu_locale(locale.c_str()); 486 icu::Locale icu_locale(locale.c_str());
Ilya Sherman 2012/04/25 20:10:36 nit: This seems to no longer be used.
Xianzhu 2012/04/25 22:31:46 Done.
487 const icu::Collator* collator = GetCollatorForLocale(locale); 487 const icu::Collator* collator = GetCollatorForLocale(locale);
488 488
489 int32_t buffer_size = 1000; 489 int32_t buffer_size = 1000;
490 scoped_array<uint8_t> buffer(new uint8_t[buffer_size]); 490 scoped_array<uint8_t> buffer(new uint8_t[buffer_size]);
491 491
492 for (CountryDataMap::Iterator it = CountryDataMap::Begin(); 492 for (CountryDataMap::Iterator it = CountryDataMap::Begin();
493 it != CountryDataMap::End(); 493 it != CountryDataMap::End();
494 ++it) { 494 ++it) {
495 const std::string& country_code = it->first; 495 const std::string& country_code = it->first;
496 496 string16 country_name = l10n_util::GetDisplayNameForCountry(country_code,
497 icu::Locale country_locale(NULL, country_code.c_str()); 497 locale, false);
Ilya Sherman 2012/04/25 20:10:36 nit: I don't fully follow the significance of the
Xianzhu 2012/04/25 22:31:46 I'm also not sure about this. I included the param
Xianzhu 2012/04/25 22:49:27 Just noticed that the is_for_ui parameter of the o
498 icu::UnicodeString country_name;
499 country_locale.getDisplayName(icu_locale, country_name);
500 std::string sort_key = GetSortKey(*collator, 498 std::string sort_key = GetSortKey(*collator,
501 country_name, 499 country_name,
502 &buffer, 500 &buffer,
503 &buffer_size); 501 &buffer_size);
504 502
505 localized_names.insert(std::make_pair(sort_key, country_code)); 503 localized_names.insert(std::make_pair(sort_key, country_code));
506 } 504 }
507 505
508 locales_to_localized_names_.insert(std::make_pair(locale, localized_names)); 506 locales_to_localized_names_.insert(std::make_pair(locale, localized_names));
509 } 507 }
510 508
511 const std::string CountryNames::GetCountryCodeForLocalizedName( 509 const std::string CountryNames::GetCountryCodeForLocalizedName(
512 const string16& country_name, 510 const string16& country_name,
513 const std::string& locale) { 511 const std::string& locale) {
514 AddLocalizedNamesForLocale(locale); 512 AddLocalizedNamesForLocale(locale);
515 513
516 icu::Collator* collator = GetCollatorForLocale(locale); 514 icu::Collator* collator = GetCollatorForLocale(locale);
517 515
518 // As recommended[1] by ICU, initialize the buffer size to four times the 516 // As recommended[1] by ICU, initialize the buffer size to four times the
519 // source string length. 517 // source string length.
520 // [1] http://userguide.icu-project.org/collation/api#TOC-Examples 518 // [1] http://userguide.icu-project.org/collation/api#TOC-Examples
521 int32_t buffer_size = country_name.size() * 4; 519 int32_t buffer_size = country_name.size() * 4;
522 scoped_array<uint8_t> buffer(new uint8_t[buffer_size]); 520 scoped_array<uint8_t> buffer(new uint8_t[buffer_size]);
523 std::string sort_key = GetSortKey(*collator, 521 std::string sort_key = GetSortKey(*collator,
524 country_name.c_str(), 522 country_name,
525 &buffer, 523 &buffer,
526 &buffer_size); 524 &buffer_size);
527 525
528 const std::map<std::string, std::string>& localized_names = 526 const std::map<std::string, std::string>& localized_names =
529 locales_to_localized_names_[locale]; 527 locales_to_localized_names_[locale];
530 std::map<std::string, std::string>::const_iterator result = 528 std::map<std::string, std::string>::const_iterator result =
531 localized_names.find(sort_key); 529 localized_names.find(sort_key);
532 530
533 if (result != localized_names.end()) 531 if (result != localized_names.end())
534 return result->second; 532 return result->second;
(...skipping 13 matching lines...) Expand all
548 ignored = U_ZERO_ERROR; 546 ignored = U_ZERO_ERROR;
549 collator->setAttribute(UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, ignored); 547 collator->setAttribute(UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, ignored);
550 548
551 collators_.insert(std::make_pair(locale, collator)); 549 collators_.insert(std::make_pair(locale, collator));
552 } 550 }
553 551
554 return collators_[locale]; 552 return collators_[locale];
555 } 553 }
556 554
557 const std::string CountryNames::GetSortKey(const icu::Collator& collator, 555 const std::string CountryNames::GetSortKey(const icu::Collator& collator,
558 const icu::UnicodeString& str, 556 const string16& str,
559 scoped_array<uint8_t>* buffer, 557 scoped_array<uint8_t>* buffer,
560 int32_t* buffer_size) const { 558 int32_t* buffer_size) const {
561 DCHECK(buffer); 559 DCHECK(buffer);
562 DCHECK(buffer_size); 560 DCHECK(buffer_size);
563 561
564 int32_t expected_size = collator.getSortKey(str, buffer->get(), *buffer_size); 562 icu::UnicodeString icu_str(str.c_str(), str.length());
563 int32_t expected_size = collator.getSortKey(icu_str, buffer->get(),
564 *buffer_size);
565 if (expected_size > *buffer_size) { 565 if (expected_size > *buffer_size) {
566 // If there wasn't enough space, grow the buffer and try again. 566 // If there wasn't enough space, grow the buffer and try again.
567 *buffer_size = expected_size; 567 *buffer_size = expected_size;
568 buffer->reset(new uint8_t[*buffer_size]); 568 buffer->reset(new uint8_t[*buffer_size]);
569 DCHECK(buffer->get()); 569 DCHECK(buffer->get());
570 570
571 expected_size = collator.getSortKey(str, buffer->get(), *buffer_size); 571 expected_size = collator.getSortKey(icu_str, buffer->get(), *buffer_size);
572 DCHECK_EQ(*buffer_size, expected_size); 572 DCHECK_EQ(*buffer_size, expected_size);
573 } 573 }
574 574
575 return std::string(reinterpret_cast<const char*>(buffer->get())); 575 return std::string(reinterpret_cast<const char*>(buffer->get()));
576 } 576 }
577 577
578 // Returns the country name corresponding to |country_code|, localized to the
579 // |display_locale|.
580 string16 GetDisplayName(const std::string& country_code,
581 const icu::Locale& display_locale) {
582 icu::Locale country_locale(NULL, country_code.c_str());
583 icu::UnicodeString name;
584 country_locale.getDisplayName(display_locale, name);
585
586 DCHECK_GT(name.length(), 0);
587 return string16(name.getBuffer(), name.length());
588 }
589
590 } // namespace 578 } // namespace
591 579
592 AutofillCountry::AutofillCountry(const std::string& country_code, 580 AutofillCountry::AutofillCountry(const std::string& country_code,
593 const std::string& locale) { 581 const std::string& locale) {
594 const CountryDataMap::Iterator result = CountryDataMap::Find(country_code); 582 const CountryDataMap::Iterator result = CountryDataMap::Find(country_code);
595 DCHECK(result != CountryDataMap::End()); 583 DCHECK(result != CountryDataMap::End());
596 const CountryData& data = result->second; 584 const CountryData& data = result->second;
597 585
598 country_code_ = country_code; 586 country_code_ = country_code;
599 name_ = GetDisplayName(country_code, icu::Locale(locale.c_str())); 587 name_ = l10n_util::GetDisplayNameForCountry(country_code, locale, false);
600 postal_code_label_ = l10n_util::GetStringUTF16(data.postal_code_label_id); 588 postal_code_label_ = l10n_util::GetStringUTF16(data.postal_code_label_id);
601 state_label_ = l10n_util::GetStringUTF16(data.state_label_id); 589 state_label_ = l10n_util::GetStringUTF16(data.state_label_id);
602 } 590 }
603 591
604 AutofillCountry::~AutofillCountry() { 592 AutofillCountry::~AutofillCountry() {
605 } 593 }
606 594
607 // static 595 // static
608 void AutofillCountry::GetAvailableCountries( 596 void AutofillCountry::GetAvailableCountries(
609 std::vector<std::string>* country_codes) { 597 std::vector<std::string>* country_codes) {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
651 639
652 AutofillCountry::AutofillCountry(const std::string& country_code, 640 AutofillCountry::AutofillCountry(const std::string& country_code,
653 const string16& name, 641 const string16& name,
654 const string16& postal_code_label, 642 const string16& postal_code_label,
655 const string16& state_label) 643 const string16& state_label)
656 : country_code_(country_code), 644 : country_code_(country_code),
657 name_(name), 645 name_(name),
658 postal_code_label_(postal_code_label), 646 postal_code_label_(postal_code_label),
659 state_label_(state_label) { 647 state_label_(state_label) {
660 } 648 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698