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 base::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 std::string GetLocaleString(const icu::Locale& locale) { |
| 47 const char* language = locale.getLanguage(); |
| 48 const char* country = locale.getCountry(); |
| 49 const char* variant = locale.getVariant(); |
| 50 |
| 51 std::string result = |
| 52 (language != NULL && *language != '\0') ? language : "und"; |
| 53 |
| 54 if (country != NULL && *country != '\0') { |
| 55 result += '-'; |
| 56 result += country; |
| 57 } |
| 58 |
| 59 if (variant != NULL && *variant != '\0') { |
| 60 std::string variant_str(variant); |
| 61 base::StringToLowerASCII(&variant_str); |
| 62 result += '@' + variant_str; |
| 63 } |
| 64 |
| 65 return result; |
| 66 } |
| 67 |
71 // Convert the ICU default locale to a string. | 68 // Convert the ICU default locale to a string. |
72 std::string GetConfiguredLocale() { | 69 std::string GetConfiguredLocale() { |
73 return GetLocaleString(icu::Locale::getDefault()); | 70 return GetLocaleString(icu::Locale::getDefault()); |
74 } | 71 } |
75 | 72 |
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 | 73 // Convert Chrome locale name to ICU locale name |
82 std::string ICULocaleName(const std::string& locale_string) { | 74 std::string ICULocaleName(const std::string& locale_string) { |
83 // If not Spanish, just return it. | 75 // If not Spanish, just return it. |
84 if (locale_string.substr(0, 2) != "es") | 76 if (locale_string.substr(0, 2) != "es") |
85 return locale_string; | 77 return locale_string; |
86 // Expand es to es-ES. | 78 // Expand es to es-ES. |
87 if (LowerCaseEqualsASCII(locale_string, "es")) | 79 if (LowerCaseEqualsASCII(locale_string, "es")) |
88 return "es-ES"; | 80 return "es-ES"; |
89 // Map es-419 (Latin American Spanish) to es-FOO depending on the system | 81 // 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 | 82 // 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) | 377 begin == kRightToLeftOverride) |
386 ++begin_index; | 378 ++begin_index; |
387 size_t end_index = text.length() - 1; | 379 size_t end_index = text.length() - 1; |
388 if (text[end_index] == kPopDirectionalFormatting) | 380 if (text[end_index] == kPopDirectionalFormatting) |
389 --end_index; | 381 --end_index; |
390 return text.substr(begin_index, end_index - begin_index + 1); | 382 return text.substr(begin_index, end_index - begin_index + 1); |
391 } | 383 } |
392 | 384 |
393 } // namespace i18n | 385 } // namespace i18n |
394 } // namespace base | 386 } // namespace base |
OLD | NEW |