Chromium Code Reviews| 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> | |
| 8 | |
| 7 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 8 #include "base/logging.h" | 10 #include "base/logging.h" |
| 9 #include "base/metrics/field_trial.h" | 11 #include "base/strings/string_split.h" |
| 10 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 11 #include "base/strings/sys_string_conversions.h" | 13 #include "base/strings/sys_string_conversions.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 13 #include "third_party/icu/source/common/unicode/locid.h" | 15 #include "third_party/icu/source/common/unicode/locid.h" |
| 14 #include "third_party/icu/source/common/unicode/uchar.h" | 16 #include "third_party/icu/source/common/unicode/uchar.h" |
| 15 #include "third_party/icu/source/common/unicode/uscript.h" | 17 #include "third_party/icu/source/common/unicode/uscript.h" |
| 16 #include "third_party/icu/source/i18n/unicode/coll.h" | 18 #include "third_party/icu/source/i18n/unicode/coll.h" |
| 17 | 19 |
| 18 #if defined(OS_IOS) | 20 #if defined(OS_IOS) |
| 19 #include "base/ios/ios_util.h" | 21 #include "base/ios/ios_util.h" |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 120 g_icu_text_direction = UNKNOWN_DIRECTION; | 122 g_icu_text_direction = UNKNOWN_DIRECTION; |
| 121 } | 123 } |
| 122 | 124 |
| 123 bool IsRTL() { | 125 bool IsRTL() { |
| 124 return ICUIsRTL(); | 126 return ICUIsRTL(); |
| 125 } | 127 } |
| 126 | 128 |
| 127 bool ICUIsRTL() { | 129 bool ICUIsRTL() { |
| 128 if (g_icu_text_direction == UNKNOWN_DIRECTION) { | 130 if (g_icu_text_direction == UNKNOWN_DIRECTION) { |
| 129 const icu::Locale& locale = icu::Locale::getDefault(); | 131 const icu::Locale& locale = icu::Locale::getDefault(); |
| 130 g_icu_text_direction = GetTextDirectionForLocale(locale.getName()); | 132 g_icu_text_direction = GetTextDirectionForLocaleInStartUp(locale.getName()); |
| 131 } | 133 } |
| 132 return g_icu_text_direction == RIGHT_TO_LEFT; | 134 return g_icu_text_direction == RIGHT_TO_LEFT; |
| 133 } | 135 } |
| 134 | 136 |
| 137 TextDirection GetTextDirectionForLocaleInStartUp(const char* locale_name) { | |
| 138 // On iOS, check for RTL forcing. | |
| 139 #if defined(OS_IOS) | |
| 140 if (ios::IsInForcedRTL()) | |
| 141 return RIGHT_TO_LEFT; | |
| 142 #endif | |
| 143 | |
| 144 // This list needs to be updated in alphabetical order if we add more RTL | |
| 145 // locales. | |
| 146 static const char* kRTLLanguageCodes[] = {"ar", "fa", "he", "iw", "ur"}; | |
| 147 std::vector<StringPiece> locale_split = | |
| 148 SplitStringPiece(locale_name, "-_", KEEP_WHITESPACE, SPLIT_WANT_ALL); | |
| 149 const StringPiece& language_code = locale_split[0]; | |
| 150 if (std::binary_search(kRTLLanguageCodes, | |
| 151 kRTLLanguageCodes + arraysize(kRTLLanguageCodes), | |
| 152 language_code)) | |
| 153 return RIGHT_TO_LEFT; | |
| 154 return LEFT_TO_RIGHT; | |
| 155 } | |
| 156 | |
| 135 TextDirection GetTextDirectionForLocale(const char* locale_name) { | 157 TextDirection GetTextDirectionForLocale(const char* locale_name) { |
|
Alexei Svitkine (slow)
2015/08/12 15:40:02
Seems like the only place where GetTextDirectionFo
danduong
2015/08/12 17:22:42
That's a good question. jshin@ I know your origina
| |
| 136 const std::string group_name = | |
| 137 FieldTrialList::FindFullName("LightSpeed"); | |
| 138 // StartsWith allows flexibility for this experiment to apply to multiple | |
| 139 // group names. To start, this will apply to AvoidMMapOnStartup. | |
| 140 if (StartsWith(group_name, "AvoidMMap", CompareCase::SENSITIVE)) { | |
| 141 static const char kEnglishLocale[] = "en_"; | |
| 142 if (StartsWith(locale_name, kEnglishLocale, CompareCase::SENSITIVE)) | |
| 143 return LEFT_TO_RIGHT; | |
| 144 } | |
| 145 | |
| 146 // On iOS, check for RTL forcing. | 158 // On iOS, check for RTL forcing. |
| 147 #if defined(OS_IOS) | 159 #if defined(OS_IOS) |
| 148 if (ios::IsInForcedRTL()) | 160 if (ios::IsInForcedRTL()) |
| 149 return RIGHT_TO_LEFT; | 161 return RIGHT_TO_LEFT; |
| 150 #endif | 162 #endif |
| 151 | 163 |
| 152 UErrorCode status = U_ZERO_ERROR; | 164 UErrorCode status = U_ZERO_ERROR; |
| 153 ULayoutType layout_dir = uloc_getCharacterOrientation(locale_name, &status); | 165 ULayoutType layout_dir = uloc_getCharacterOrientation(locale_name, &status); |
| 154 DCHECK(U_SUCCESS(status)); | 166 DCHECK(U_SUCCESS(status)); |
| 155 // Treat anything other than RTL as LTR. | 167 // Treat anything other than RTL as LTR. |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 403 begin == kRightToLeftOverride) | 415 begin == kRightToLeftOverride) |
| 404 ++begin_index; | 416 ++begin_index; |
| 405 size_t end_index = text.length() - 1; | 417 size_t end_index = text.length() - 1; |
| 406 if (text[end_index] == kPopDirectionalFormatting) | 418 if (text[end_index] == kPopDirectionalFormatting) |
| 407 --end_index; | 419 --end_index; |
| 408 return text.substr(begin_index, end_index - begin_index + 1); | 420 return text.substr(begin_index, end_index - begin_index + 1); |
| 409 } | 421 } |
| 410 | 422 |
| 411 } // namespace i18n | 423 } // namespace i18n |
| 412 } // namespace base | 424 } // namespace base |
| OLD | NEW |