| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "app/l10n_util.h" | 5 #include "app/l10n_util.h" |
| 6 | 6 |
| 7 #include "app/app_paths.h" | 7 #include "app/app_paths.h" |
| 8 #include "app/app_switches.h" | 8 #include "app/app_switches.h" |
| 9 #include "app/gfx/canvas.h" | 9 #include "app/gfx/canvas.h" |
| 10 #include "app/resource_bundle.h" | 10 #include "app/resource_bundle.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 | 38 |
| 39 // Added to the end of strings that are too big in TrucateString. | 39 // Added to the end of strings that are too big in TrucateString. |
| 40 static const wchar_t* const kElideString = L"\x2026"; | 40 static const wchar_t* const kElideString = L"\x2026"; |
| 41 | 41 |
| 42 // Get language and region from the OS. | 42 // Get language and region from the OS. |
| 43 void GetLanguageAndRegionFromOS(std::string* lang, std::string* region) { | 43 void GetLanguageAndRegionFromOS(std::string* lang, std::string* region) { |
| 44 // Later we may have to change this to be OS-dependent so that | 44 // Later we may have to change this to be OS-dependent so that |
| 45 // it's not affected by ICU's default locale. It's all right | 45 // it's not affected by ICU's default locale. It's all right |
| 46 // to do this way because SetICUDefaultLocale is internal | 46 // to do this way because SetICUDefaultLocale is internal |
| 47 // to this file and we know where/when it's called. | 47 // to this file and we know where/when it's called. |
| 48 Locale locale = Locale::getDefault(); | 48 icu::Locale locale = icu::Locale::getDefault(); |
| 49 const char* language = locale.getLanguage(); | 49 const char* language = locale.getLanguage(); |
| 50 const char* country = locale.getCountry(); | 50 const char* country = locale.getCountry(); |
| 51 DCHECK(language); | 51 DCHECK(language); |
| 52 *lang = language; | 52 *lang = language; |
| 53 *region = country; | 53 *region = country; |
| 54 } | 54 } |
| 55 | 55 |
| 56 // Convert Chrome locale name to ICU locale name | 56 // Convert Chrome locale name to ICU locale name |
| 57 std::string ICULocaleName(const std::string& locale_string) { | 57 std::string ICULocaleName(const std::string& locale_string) { |
| 58 // If not Spanish, just return it. | 58 // If not Spanish, just return it. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 82 | 82 |
| 83 // Sets the default locale of ICU. | 83 // Sets the default locale of ICU. |
| 84 // When the application locale (UI locale) of Chrome is specified with | 84 // When the application locale (UI locale) of Chrome is specified with |
| 85 // '--lang' command line flag or 'intl.app_locale' entry in the "Preferences", | 85 // '--lang' command line flag or 'intl.app_locale' entry in the "Preferences", |
| 86 // the default locale of ICU need to be changed to match the application locale | 86 // the default locale of ICU need to be changed to match the application locale |
| 87 // so that ICU functions work correctly in a locale-dependent manner. | 87 // so that ICU functions work correctly in a locale-dependent manner. |
| 88 // This is handy in that we don't have to call GetApplicationLocale() | 88 // This is handy in that we don't have to call GetApplicationLocale() |
| 89 // everytime we call locale-dependent ICU APIs as long as we make sure | 89 // everytime we call locale-dependent ICU APIs as long as we make sure |
| 90 // that this is called before any locale-dependent API is called. | 90 // that this is called before any locale-dependent API is called. |
| 91 UBool SetICUDefaultLocale(const std::string& locale_string) { | 91 UBool SetICUDefaultLocale(const std::string& locale_string) { |
| 92 Locale locale(ICULocaleName(locale_string).c_str()); | 92 icu::Locale locale(ICULocaleName(locale_string).c_str()); |
| 93 UErrorCode error_code = U_ZERO_ERROR; | 93 UErrorCode error_code = U_ZERO_ERROR; |
| 94 Locale::setDefault(locale, error_code); | 94 icu::Locale::setDefault(locale, error_code); |
| 95 // This return value is actually bogus because Locale object is | 95 // This return value is actually bogus because Locale object is |
| 96 // an ID and setDefault seems to always succeed (regardless of the | 96 // an ID and setDefault seems to always succeed (regardless of the |
| 97 // presence of actual locale data). However, | 97 // presence of actual locale data). However, |
| 98 // it does not hurt to have it as a sanity check. | 98 // it does not hurt to have it as a sanity check. |
| 99 return U_SUCCESS(error_code); | 99 return U_SUCCESS(error_code); |
| 100 } | 100 } |
| 101 | 101 |
| 102 // Returns true if |locale_name| has an alias in the ICU data file. | 102 // Returns true if |locale_name| has an alias in the ICU data file. |
| 103 bool IsDuplicateName(const std::string& locale_name) { | 103 bool IsDuplicateName(const std::string& locale_name) { |
| 104 static const char* const kDuplicateNames[] = { | 104 static const char* const kDuplicateNames[] = { |
| (...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 448 return kElideString; | 448 return kElideString; |
| 449 } | 449 } |
| 450 | 450 |
| 451 #if defined(WCHAR_T_IS_UTF32) | 451 #if defined(WCHAR_T_IS_UTF32) |
| 452 const string16 string_utf16 = WideToUTF16(string); | 452 const string16 string_utf16 = WideToUTF16(string); |
| 453 #else | 453 #else |
| 454 const std::wstring &string_utf16 = string; | 454 const std::wstring &string_utf16 = string; |
| 455 #endif | 455 #endif |
| 456 // Use a line iterator to find the first boundary. | 456 // Use a line iterator to find the first boundary. |
| 457 UErrorCode status = U_ZERO_ERROR; | 457 UErrorCode status = U_ZERO_ERROR; |
| 458 scoped_ptr<RuleBasedBreakIterator> bi(static_cast<RuleBasedBreakIterator*>( | 458 scoped_ptr<icu::RuleBasedBreakIterator> bi( |
| 459 RuleBasedBreakIterator::createLineInstance(Locale::getDefault(), | 459 static_cast<icu::RuleBasedBreakIterator*>( |
| 460 status))); | 460 icu::RuleBasedBreakIterator::createLineInstance( |
| 461 icu::Locale::getDefault(), status))); |
| 461 if (U_FAILURE(status)) | 462 if (U_FAILURE(status)) |
| 462 return string.substr(0, max) + kElideString; | 463 return string.substr(0, max) + kElideString; |
| 463 bi->setText(string_utf16.c_str()); | 464 bi->setText(string_utf16.c_str()); |
| 464 int32_t index = bi->preceding(static_cast<int32_t>(max)); | 465 int32_t index = bi->preceding(static_cast<int32_t>(max)); |
| 465 if (index == BreakIterator::DONE) { | 466 if (index == icu::BreakIterator::DONE) { |
| 466 index = static_cast<int32_t>(max); | 467 index = static_cast<int32_t>(max); |
| 467 } else { | 468 } else { |
| 468 // Found a valid break (may be the beginning of the string). Now use | 469 // Found a valid break (may be the beginning of the string). Now use |
| 469 // a character iterator to find the previous non-whitespace character. | 470 // a character iterator to find the previous non-whitespace character. |
| 470 StringCharacterIterator char_iterator(string_utf16.c_str()); | 471 icu::StringCharacterIterator char_iterator(string_utf16.c_str()); |
| 471 if (index == 0) { | 472 if (index == 0) { |
| 472 // No valid line breaks. Start at the end again. This ensures we break | 473 // No valid line breaks. Start at the end again. This ensures we break |
| 473 // on a valid character boundary. | 474 // on a valid character boundary. |
| 474 index = static_cast<int32_t>(max); | 475 index = static_cast<int32_t>(max); |
| 475 } | 476 } |
| 476 char_iterator.setIndex(index); | 477 char_iterator.setIndex(index); |
| 477 while (char_iterator.hasPrevious()) { | 478 while (char_iterator.hasPrevious()) { |
| 478 char_iterator.previous(); | 479 char_iterator.previous(); |
| 479 if (!(u_isspace(char_iterator.current()) || | 480 if (!(u_isspace(char_iterator.current()) || |
| 480 u_charType(char_iterator.current()) == U_CONTROL_CHAR || | 481 u_charType(char_iterator.current()) == U_CONTROL_CHAR || |
| (...skipping 15 matching lines...) Expand all Loading... |
| 496 return string.substr(0, index) + kElideString; | 497 return string.substr(0, index) + kElideString; |
| 497 } | 498 } |
| 498 | 499 |
| 499 #if defined(WCHAR_T_IS_UTF32) | 500 #if defined(WCHAR_T_IS_UTF32) |
| 500 std::wstring ToLower(const std::wstring& string) { | 501 std::wstring ToLower(const std::wstring& string) { |
| 501 return UTF16ToWide(ToLower(WideToUTF16(string))); | 502 return UTF16ToWide(ToLower(WideToUTF16(string))); |
| 502 } | 503 } |
| 503 #endif // defined(WCHAR_T_IS_UTF32) | 504 #endif // defined(WCHAR_T_IS_UTF32) |
| 504 | 505 |
| 505 string16 ToLower(const string16& string) { | 506 string16 ToLower(const string16& string) { |
| 506 UnicodeString lower_u_str( | 507 icu::UnicodeString lower_u_str( |
| 507 UnicodeString(string.c_str()).toLower(Locale::getDefault())); | 508 icu::UnicodeString(string.c_str()).toLower(icu::Locale::getDefault())); |
| 508 string16 result; | 509 string16 result; |
| 509 lower_u_str.extract(0, lower_u_str.length(), | 510 lower_u_str.extract(0, lower_u_str.length(), |
| 510 WriteInto(&result, lower_u_str.length() + 1)); | 511 WriteInto(&result, lower_u_str.length() + 1)); |
| 511 return result; | 512 return result; |
| 512 } | 513 } |
| 513 | 514 |
| 514 // Returns the text direction for the default ICU locale. It is assumed | 515 // Returns the text direction for the default ICU locale. It is assumed |
| 515 // that SetICUDefaultLocale has been called to set the default locale to | 516 // that SetICUDefaultLocale has been called to set the default locale to |
| 516 // the UI locale of Chrome. | 517 // the UI locale of Chrome. |
| 517 TextDirection GetTextDirection() { | 518 TextDirection GetTextDirection() { |
| 518 if (g_text_direction == UNKNOWN_DIRECTION) { | 519 if (g_text_direction == UNKNOWN_DIRECTION) { |
| 519 #if defined(TOOLKIT_GTK) | 520 #if defined(TOOLKIT_GTK) |
| 520 GtkTextDirection gtk_dir = gtk_widget_get_default_direction(); | 521 GtkTextDirection gtk_dir = gtk_widget_get_default_direction(); |
| 521 g_text_direction = | 522 g_text_direction = |
| 522 (gtk_dir == GTK_TEXT_DIR_LTR) ? LEFT_TO_RIGHT : RIGHT_TO_LEFT; | 523 (gtk_dir == GTK_TEXT_DIR_LTR) ? LEFT_TO_RIGHT : RIGHT_TO_LEFT; |
| 523 #else | 524 #else |
| 524 const Locale& locale = Locale::getDefault(); | 525 const icu::Locale& locale = icu::Locale::getDefault(); |
| 525 g_text_direction = GetTextDirectionForLocale(locale.getName()); | 526 g_text_direction = GetTextDirectionForLocale(locale.getName()); |
| 526 #endif | 527 #endif |
| 527 } | 528 } |
| 528 return g_text_direction; | 529 return g_text_direction; |
| 529 } | 530 } |
| 530 | 531 |
| 531 TextDirection GetTextDirectionForLocale(const char* locale_name) { | 532 TextDirection GetTextDirectionForLocale(const char* locale_name) { |
| 532 UScriptCode scripts[10]; // 10 scripts should be enough for any locale. | 533 UScriptCode scripts[10]; // 10 scripts should be enough for any locale. |
| 533 UErrorCode error = U_ZERO_ERROR; | 534 UErrorCode error = U_ZERO_ERROR; |
| 534 int n = uscript_getCode(locale_name, scripts, 10, &error); | 535 int n = uscript_getCode(locale_name, scripts, 10, &error); |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 661 if (GetTextDirection() == LEFT_TO_RIGHT) { | 662 if (GetTextDirection() == LEFT_TO_RIGHT) { |
| 662 return gfx::Canvas::TEXT_ALIGN_LEFT; | 663 return gfx::Canvas::TEXT_ALIGN_LEFT; |
| 663 } else { | 664 } else { |
| 664 return gfx::Canvas::TEXT_ALIGN_RIGHT; | 665 return gfx::Canvas::TEXT_ALIGN_RIGHT; |
| 665 } | 666 } |
| 666 } | 667 } |
| 667 | 668 |
| 668 | 669 |
| 669 // Compares the character data stored in two different strings by specified | 670 // Compares the character data stored in two different strings by specified |
| 670 // Collator instance. | 671 // Collator instance. |
| 671 UCollationResult CompareStringWithCollator(const Collator* collator, | 672 UCollationResult CompareStringWithCollator(const icu::Collator* collator, |
| 672 const std::wstring& lhs, | 673 const std::wstring& lhs, |
| 673 const std::wstring& rhs) { | 674 const std::wstring& rhs) { |
| 674 DCHECK(collator); | 675 DCHECK(collator); |
| 675 UErrorCode error = U_ZERO_ERROR; | 676 UErrorCode error = U_ZERO_ERROR; |
| 676 #if defined(WCHAR_T_IS_UTF32) | 677 #if defined(WCHAR_T_IS_UTF32) |
| 677 // Need to convert to UTF-16 to be compatible with UnicodeString's | 678 // Need to convert to UTF-16 to be compatible with UnicodeString's |
| 678 // constructor. | 679 // constructor. |
| 679 string16 lhs_utf16 = WideToUTF16(lhs); | 680 string16 lhs_utf16 = WideToUTF16(lhs); |
| 680 string16 rhs_utf16 = WideToUTF16(rhs); | 681 string16 rhs_utf16 = WideToUTF16(rhs); |
| 681 | 682 |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 784 } | 785 } |
| 785 | 786 |
| 786 void BiDiLineIterator::GetLogicalRun(int start, | 787 void BiDiLineIterator::GetLogicalRun(int start, |
| 787 int* end, | 788 int* end, |
| 788 UBiDiLevel* level) { | 789 UBiDiLevel* level) { |
| 789 DCHECK(bidi_ != NULL); | 790 DCHECK(bidi_ != NULL); |
| 790 ubidi_getLogicalRun(bidi_, start, end, level); | 791 ubidi_getLogicalRun(bidi_, start, end, level); |
| 791 } | 792 } |
| 792 | 793 |
| 793 } | 794 } |
| OLD | NEW |