| OLD | NEW |
| 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 "base/i18n/rtl.h" | 5 #include "base/i18n/rtl.h" |
| 6 | 6 |
| 7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "third_party/icu/source/common/unicode/locid.h" | |
| 13 #include "third_party/icu/source/common/unicode/uchar.h" | 12 #include "third_party/icu/source/common/unicode/uchar.h" |
| 14 #include "third_party/icu/source/common/unicode/uscript.h" | 13 #include "third_party/icu/source/common/unicode/uscript.h" |
| 15 #include "third_party/icu/source/i18n/unicode/coll.h" | 14 #include "third_party/icu/source/i18n/unicode/coll.h" |
| 16 | 15 |
| 17 namespace { | 16 namespace { |
| 18 | 17 |
| 19 // Extract language, country and variant, but ignore keywords. For example, | |
| 20 // en-US, ca@valencia, ca-ES@valencia. | |
| 21 std::string GetLocaleString(const icu::Locale& locale) { | |
| 22 const char* language = locale.getLanguage(); | |
| 23 const char* country = locale.getCountry(); | |
| 24 const char* variant = locale.getVariant(); | |
| 25 | |
| 26 std::string result = | |
| 27 (language != NULL && *language != '\0') ? language : "und"; | |
| 28 | |
| 29 if (country != NULL && *country != '\0') { | |
| 30 result += '-'; | |
| 31 result += country; | |
| 32 } | |
| 33 | |
| 34 if (variant != NULL && *variant != '\0') { | |
| 35 std::string variant_str(variant); | |
| 36 StringToLowerASCII(&variant_str); | |
| 37 result += '@' + variant_str; | |
| 38 } | |
| 39 | |
| 40 return result; | |
| 41 } | |
| 42 | |
| 43 // Returns LEFT_TO_RIGHT or RIGHT_TO_LEFT if |character| has strong | 18 // Returns LEFT_TO_RIGHT or RIGHT_TO_LEFT if |character| has strong |
| 44 // directionality, returns UNKNOWN_DIRECTION if it doesn't. Please refer to | 19 // directionality, returns UNKNOWN_DIRECTION if it doesn't. Please refer to |
| 45 // http://unicode.org/reports/tr9/ for more information. | 20 // http://unicode.org/reports/tr9/ for more information. |
| 46 base::i18n::TextDirection GetCharacterDirection(UChar32 character) { | 21 base::i18n::TextDirection GetCharacterDirection(UChar32 character) { |
| 47 // Now that we have the character, we use ICU in order to query for the | 22 // Now that we have the character, we use ICU in order to query for the |
| 48 // appropriate Unicode BiDi character type. | 23 // appropriate Unicode BiDi character type. |
| 49 int32_t property = u_getIntPropertyValue(character, UCHAR_BIDI_CLASS); | 24 int32_t property = u_getIntPropertyValue(character, UCHAR_BIDI_CLASS); |
| 50 if ((property == U_RIGHT_TO_LEFT) || | 25 if ((property == U_RIGHT_TO_LEFT) || |
| 51 (property == U_RIGHT_TO_LEFT_ARABIC) || | 26 (property == U_RIGHT_TO_LEFT_ARABIC) || |
| 52 (property == U_RIGHT_TO_LEFT_EMBEDDING) || | 27 (property == U_RIGHT_TO_LEFT_EMBEDDING) || |
| 53 (property == U_RIGHT_TO_LEFT_OVERRIDE)) { | 28 (property == U_RIGHT_TO_LEFT_OVERRIDE)) { |
| 54 return base::i18n::RIGHT_TO_LEFT; | 29 return base::i18n::RIGHT_TO_LEFT; |
| 55 } else if ((property == U_LEFT_TO_RIGHT) || | 30 } else if ((property == U_LEFT_TO_RIGHT) || |
| 56 (property == U_LEFT_TO_RIGHT_EMBEDDING) || | 31 (property == U_LEFT_TO_RIGHT_EMBEDDING) || |
| 57 (property == U_LEFT_TO_RIGHT_OVERRIDE)) { | 32 (property == U_LEFT_TO_RIGHT_OVERRIDE)) { |
| 58 return base::i18n::LEFT_TO_RIGHT; | 33 return base::i18n::LEFT_TO_RIGHT; |
| 59 } | 34 } |
| 60 return base::i18n::UNKNOWN_DIRECTION; | 35 return base::i18n::UNKNOWN_DIRECTION; |
| 61 } | 36 } |
| 62 | 37 |
| 63 } // namespace | 38 } // namespace |
| 64 | 39 |
| 65 namespace base { | 40 namespace base { |
| 66 namespace i18n { | 41 namespace i18n { |
| 67 | 42 |
| 68 // Represents the locale-specific ICU text direction. | 43 // Represents the locale-specific ICU text direction. |
| 69 static TextDirection g_icu_text_direction = UNKNOWN_DIRECTION; | 44 static TextDirection g_icu_text_direction = UNKNOWN_DIRECTION; |
| 70 | 45 |
| 46 // Extract language, country and variant, but ignore keywords. For example, |
| 47 // en-US, ca@valencia, ca-ES@valencia. |
| 48 std::string GetLocaleString(const icu::Locale& locale) { |
| 49 const char* language = locale.getLanguage(); |
| 50 const char* country = locale.getCountry(); |
| 51 const char* variant = locale.getVariant(); |
| 52 |
| 53 std::string result = |
| 54 (language != NULL && *language != '\0') ? language : "und"; |
| 55 |
| 56 if (country != NULL && *country != '\0') { |
| 57 result += '-'; |
| 58 result += country; |
| 59 } |
| 60 |
| 61 if (variant != NULL && *variant != '\0') { |
| 62 std::string variant_str(variant); |
| 63 StringToLowerASCII(&variant_str); |
| 64 result += '@' + variant_str; |
| 65 } |
| 66 |
| 67 return result; |
| 68 } |
| 69 |
| 71 // Convert the ICU default locale to a string. | 70 // Convert the ICU default locale to a string. |
| 72 std::string GetConfiguredLocale() { | 71 std::string GetConfiguredLocale() { |
| 73 return GetLocaleString(icu::Locale::getDefault()); | 72 return GetLocaleString(icu::Locale::getDefault()); |
| 74 } | 73 } |
| 75 | 74 |
| 76 // Convert the ICU canonicalized locale to a string. | |
| 77 std::string GetCanonicalLocale(const char* locale) { | |
| 78 return GetLocaleString(icu::Locale::createCanonical(locale)); | |
| 79 } | |
| 80 | |
| 81 // Convert Chrome locale name to ICU locale name | 75 // Convert Chrome locale name to ICU locale name |
| 82 std::string ICULocaleName(const std::string& locale_string) { | 76 std::string ICULocaleName(const std::string& locale_string) { |
| 83 // If not Spanish, just return it. | 77 // If not Spanish, just return it. |
| 84 if (locale_string.substr(0, 2) != "es") | 78 if (locale_string.substr(0, 2) != "es") |
| 85 return locale_string; | 79 return locale_string; |
| 86 // Expand es to es-ES. | 80 // Expand es to es-ES. |
| 87 if (LowerCaseEqualsASCII(locale_string, "es")) | 81 if (LowerCaseEqualsASCII(locale_string, "es")) |
| 88 return "es-ES"; | 82 return "es-ES"; |
| 89 // Map es-419 (Latin American Spanish) to es-FOO depending on the system | 83 // Map es-419 (Latin American Spanish) to es-FOO depending on the system |
| 90 // locale. If it's es-RR other than es-ES, map to es-RR. Otherwise, map | 84 // locale. If it's es-RR other than es-ES, map to es-RR. Otherwise, map |
| (...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 385 begin == kRightToLeftOverride) | 379 begin == kRightToLeftOverride) |
| 386 ++begin_index; | 380 ++begin_index; |
| 387 size_t end_index = text.length() - 1; | 381 size_t end_index = text.length() - 1; |
| 388 if (text[end_index] == kPopDirectionalFormatting) | 382 if (text[end_index] == kPopDirectionalFormatting) |
| 389 --end_index; | 383 --end_index; |
| 390 return text.substr(begin_index, end_index - begin_index + 1); | 384 return text.substr(begin_index, end_index - begin_index + 1); |
| 391 } | 385 } |
| 392 | 386 |
| 393 } // namespace i18n | 387 } // namespace i18n |
| 394 } // namespace base | 388 } // namespace base |
| OLD | NEW |