| 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 <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/command_line.h" |
| 9 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/i18n/base_i18n_switches.h" |
| 10 #include "base/logging.h" | 12 #include "base/logging.h" |
| 11 #include "base/strings/string_split.h" | 13 #include "base/strings/string_split.h" |
| 12 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 13 #include "base/strings/sys_string_conversions.h" | 15 #include "base/strings/sys_string_conversions.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 15 #include "third_party/icu/source/common/unicode/locid.h" | 17 #include "third_party/icu/source/common/unicode/locid.h" |
| 16 #include "third_party/icu/source/common/unicode/uchar.h" | 18 #include "third_party/icu/source/common/unicode/uchar.h" |
| 17 #include "third_party/icu/source/common/unicode/uscript.h" | 19 #include "third_party/icu/source/common/unicode/uscript.h" |
| 18 #include "third_party/icu/source/i18n/unicode/coll.h" | 20 #include "third_party/icu/source/i18n/unicode/coll.h" |
| 19 | 21 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 (property == U_RIGHT_TO_LEFT_OVERRIDE)) { | 59 (property == U_RIGHT_TO_LEFT_OVERRIDE)) { |
| 58 return base::i18n::RIGHT_TO_LEFT; | 60 return base::i18n::RIGHT_TO_LEFT; |
| 59 } else if ((property == U_LEFT_TO_RIGHT) || | 61 } else if ((property == U_LEFT_TO_RIGHT) || |
| 60 (property == U_LEFT_TO_RIGHT_EMBEDDING) || | 62 (property == U_LEFT_TO_RIGHT_EMBEDDING) || |
| 61 (property == U_LEFT_TO_RIGHT_OVERRIDE)) { | 63 (property == U_LEFT_TO_RIGHT_OVERRIDE)) { |
| 62 return base::i18n::LEFT_TO_RIGHT; | 64 return base::i18n::LEFT_TO_RIGHT; |
| 63 } | 65 } |
| 64 return base::i18n::UNKNOWN_DIRECTION; | 66 return base::i18n::UNKNOWN_DIRECTION; |
| 65 } | 67 } |
| 66 | 68 |
| 69 // Gets the explicitly forced text direction for debugging. If no forcing is |
| 70 // applied, returns UNKNOWN_DIRECTION. |
| 71 base::i18n::TextDirection GetForcedTextDirection() { |
| 72 // On iOS, check for RTL forcing. |
| 73 #if defined(OS_IOS) |
| 74 if (base::ios::IsInForcedRTL()) |
| 75 return base::i18n::RIGHT_TO_LEFT; |
| 76 #endif |
| 77 |
| 78 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 79 if (command_line->HasSwitch(switches::kForceUIDirection)) { |
| 80 std::string force_flag = |
| 81 command_line->GetSwitchValueASCII(switches::kForceUIDirection); |
| 82 |
| 83 if (force_flag == switches::kForceUIDirectionLTR) |
| 84 return base::i18n::LEFT_TO_RIGHT; |
| 85 |
| 86 if (force_flag == switches::kForceUIDirectionRTL) |
| 87 return base::i18n::RIGHT_TO_LEFT; |
| 88 } |
| 89 |
| 90 return base::i18n::UNKNOWN_DIRECTION; |
| 91 } |
| 92 |
| 67 } // namespace | 93 } // namespace |
| 68 | 94 |
| 69 namespace base { | 95 namespace base { |
| 70 namespace i18n { | 96 namespace i18n { |
| 71 | 97 |
| 72 // Represents the locale-specific ICU text direction. | 98 // Represents the locale-specific ICU text direction. |
| 73 static TextDirection g_icu_text_direction = UNKNOWN_DIRECTION; | 99 static TextDirection g_icu_text_direction = UNKNOWN_DIRECTION; |
| 74 | 100 |
| 75 // Convert the ICU default locale to a string. | 101 // Convert the ICU default locale to a string. |
| 76 std::string GetConfiguredLocale() { | 102 std::string GetConfiguredLocale() { |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 | 154 |
| 129 bool ICUIsRTL() { | 155 bool ICUIsRTL() { |
| 130 if (g_icu_text_direction == UNKNOWN_DIRECTION) { | 156 if (g_icu_text_direction == UNKNOWN_DIRECTION) { |
| 131 const icu::Locale& locale = icu::Locale::getDefault(); | 157 const icu::Locale& locale = icu::Locale::getDefault(); |
| 132 g_icu_text_direction = GetTextDirectionForLocaleInStartUp(locale.getName()); | 158 g_icu_text_direction = GetTextDirectionForLocaleInStartUp(locale.getName()); |
| 133 } | 159 } |
| 134 return g_icu_text_direction == RIGHT_TO_LEFT; | 160 return g_icu_text_direction == RIGHT_TO_LEFT; |
| 135 } | 161 } |
| 136 | 162 |
| 137 TextDirection GetTextDirectionForLocaleInStartUp(const char* locale_name) { | 163 TextDirection GetTextDirectionForLocaleInStartUp(const char* locale_name) { |
| 138 // On iOS, check for RTL forcing. | 164 // Check for direction forcing. |
| 139 #if defined(OS_IOS) | 165 TextDirection forced_direction = GetForcedTextDirection(); |
| 140 if (ios::IsInForcedRTL()) | 166 if (forced_direction != UNKNOWN_DIRECTION) |
| 141 return RIGHT_TO_LEFT; | 167 return forced_direction; |
| 142 #endif | |
| 143 | 168 |
| 144 // This list needs to be updated in alphabetical order if we add more RTL | 169 // This list needs to be updated in alphabetical order if we add more RTL |
| 145 // locales. | 170 // locales. |
| 146 static const char* kRTLLanguageCodes[] = {"ar", "fa", "he", "iw", "ur"}; | 171 static const char* kRTLLanguageCodes[] = {"ar", "fa", "he", "iw", "ur"}; |
| 147 std::vector<StringPiece> locale_split = | 172 std::vector<StringPiece> locale_split = |
| 148 SplitStringPiece(locale_name, "-_", KEEP_WHITESPACE, SPLIT_WANT_ALL); | 173 SplitStringPiece(locale_name, "-_", KEEP_WHITESPACE, SPLIT_WANT_ALL); |
| 149 const StringPiece& language_code = locale_split[0]; | 174 const StringPiece& language_code = locale_split[0]; |
| 150 if (std::binary_search(kRTLLanguageCodes, | 175 if (std::binary_search(kRTLLanguageCodes, |
| 151 kRTLLanguageCodes + arraysize(kRTLLanguageCodes), | 176 kRTLLanguageCodes + arraysize(kRTLLanguageCodes), |
| 152 language_code)) | 177 language_code)) |
| 153 return RIGHT_TO_LEFT; | 178 return RIGHT_TO_LEFT; |
| 154 return LEFT_TO_RIGHT; | 179 return LEFT_TO_RIGHT; |
| 155 } | 180 } |
| 156 | 181 |
| 157 TextDirection GetTextDirectionForLocale(const char* locale_name) { | 182 TextDirection GetTextDirectionForLocale(const char* locale_name) { |
| 158 // On iOS, check for RTL forcing. | 183 // Check for direction forcing. |
| 159 #if defined(OS_IOS) | 184 TextDirection forced_direction = GetForcedTextDirection(); |
| 160 if (ios::IsInForcedRTL()) | 185 if (forced_direction != UNKNOWN_DIRECTION) |
| 161 return RIGHT_TO_LEFT; | 186 return forced_direction; |
| 162 #endif | |
| 163 | 187 |
| 164 UErrorCode status = U_ZERO_ERROR; | 188 UErrorCode status = U_ZERO_ERROR; |
| 165 ULayoutType layout_dir = uloc_getCharacterOrientation(locale_name, &status); | 189 ULayoutType layout_dir = uloc_getCharacterOrientation(locale_name, &status); |
| 166 DCHECK(U_SUCCESS(status)); | 190 DCHECK(U_SUCCESS(status)); |
| 167 // Treat anything other than RTL as LTR. | 191 // Treat anything other than RTL as LTR. |
| 168 return (layout_dir != ULOC_LAYOUT_RTL) ? LEFT_TO_RIGHT : RIGHT_TO_LEFT; | 192 return (layout_dir != ULOC_LAYOUT_RTL) ? LEFT_TO_RIGHT : RIGHT_TO_LEFT; |
| 169 } | 193 } |
| 170 | 194 |
| 171 TextDirection GetFirstStrongCharacterDirection(const string16& text) { | 195 TextDirection GetFirstStrongCharacterDirection(const string16& text) { |
| 172 const UChar* string = text.c_str(); | 196 const UChar* string = text.c_str(); |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 415 begin == kRightToLeftOverride) | 439 begin == kRightToLeftOverride) |
| 416 ++begin_index; | 440 ++begin_index; |
| 417 size_t end_index = text.length() - 1; | 441 size_t end_index = text.length() - 1; |
| 418 if (text[end_index] == kPopDirectionalFormatting) | 442 if (text[end_index] == kPopDirectionalFormatting) |
| 419 --end_index; | 443 --end_index; |
| 420 return text.substr(begin_index, end_index - begin_index + 1); | 444 return text.substr(begin_index, end_index - begin_index + 1); |
| 421 } | 445 } |
| 422 | 446 |
| 423 } // namespace i18n | 447 } // namespace i18n |
| 424 } // namespace base | 448 } // namespace base |
| OLD | NEW |