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 "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/metrics/field_trial.h" | |
| 10 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 11 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 13 #include "third_party/icu/source/common/unicode/locid.h" | 12 #include "third_party/icu/source/common/unicode/locid.h" |
| 14 #include "third_party/icu/source/common/unicode/uchar.h" | 13 #include "third_party/icu/source/common/unicode/uchar.h" |
| 15 #include "third_party/icu/source/common/unicode/uscript.h" | 14 #include "third_party/icu/source/common/unicode/uscript.h" |
| 16 #include "third_party/icu/source/i18n/unicode/coll.h" | 15 #include "third_party/icu/source/i18n/unicode/coll.h" |
| 17 | 16 |
| 18 namespace { | 17 namespace { |
| 19 | 18 |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 119 g_icu_text_direction = UNKNOWN_DIRECTION; | 118 g_icu_text_direction = UNKNOWN_DIRECTION; |
| 120 } | 119 } |
| 121 | 120 |
| 122 bool IsRTL() { | 121 bool IsRTL() { |
| 123 return ICUIsRTL(); | 122 return ICUIsRTL(); |
| 124 } | 123 } |
| 125 | 124 |
| 126 bool ICUIsRTL() { | 125 bool ICUIsRTL() { |
| 127 if (g_icu_text_direction == UNKNOWN_DIRECTION) { | 126 if (g_icu_text_direction == UNKNOWN_DIRECTION) { |
| 128 const icu::Locale& locale = icu::Locale::getDefault(); | 127 const icu::Locale& locale = icu::Locale::getDefault(); |
| 129 g_icu_text_direction = GetTextDirectionForLocale(locale.getName()); | 128 g_icu_text_direction = GetTextDirectionForLocaleInStartUp(locale.getName()); |
| 130 } | 129 } |
| 131 return g_icu_text_direction == RIGHT_TO_LEFT; | 130 return g_icu_text_direction == RIGHT_TO_LEFT; |
| 132 } | 131 } |
| 133 | 132 |
| 133 TextDirection GetTextDirectionForLocaleInStartUp(const char* locale_name) { | |
| 134 // This list needs to be updated if we add more RTL locales. | |
| 135 static const char* kRTLLocales[] = { | |
| 136 "ar", | |
| 137 "fa", | |
| 138 "he", | |
| 139 "iw", | |
| 140 "ur" | |
| 141 }; | |
| 142 for (const char* rtl_locale : kRTLLocales) { | |
| 143 if (StartsWith(locale_name, rtl_locale, CompareCase::SENSITIVE)) | |
|
jungshik at Google
2015/08/11 20:38:29
One more thing I forgot to mention. Sorry that I d
jungshik at Google
2015/08/11 20:41:59
Because the only 3-letter language code in the cur
danduong
2015/08/11 21:03:16
That was definitely an oversight on my part. Thank
| |
| 144 return RIGHT_TO_LEFT; | |
| 145 } | |
| 146 return LEFT_TO_RIGHT; | |
| 147 } | |
| 148 | |
| 134 TextDirection GetTextDirectionForLocale(const char* locale_name) { | 149 TextDirection GetTextDirectionForLocale(const char* locale_name) { |
| 135 const std::string group_name = | |
| 136 FieldTrialList::FindFullName("LightSpeed"); | |
| 137 // StartsWith allows flexibility for this experiment to apply to multiple | |
| 138 // group names. To start, this will apply to AvoidMMapOnStartup. | |
| 139 if (StartsWith(group_name, "AvoidMMap", CompareCase::SENSITIVE)) { | |
| 140 static const char kEnglishLocale[] = "en_"; | |
| 141 if (StartsWith(locale_name, kEnglishLocale, CompareCase::SENSITIVE)) | |
| 142 return LEFT_TO_RIGHT; | |
| 143 } | |
| 144 UErrorCode status = U_ZERO_ERROR; | 150 UErrorCode status = U_ZERO_ERROR; |
| 145 ULayoutType layout_dir = uloc_getCharacterOrientation(locale_name, &status); | 151 ULayoutType layout_dir = uloc_getCharacterOrientation(locale_name, &status); |
| 146 DCHECK(U_SUCCESS(status)); | 152 DCHECK(U_SUCCESS(status)); |
| 147 // Treat anything other than RTL as LTR. | 153 // Treat anything other than RTL as LTR. |
| 148 return (layout_dir != ULOC_LAYOUT_RTL) ? LEFT_TO_RIGHT : RIGHT_TO_LEFT; | 154 return (layout_dir != ULOC_LAYOUT_RTL) ? LEFT_TO_RIGHT : RIGHT_TO_LEFT; |
| 149 } | 155 } |
| 150 | 156 |
| 151 TextDirection GetFirstStrongCharacterDirection(const string16& text) { | 157 TextDirection GetFirstStrongCharacterDirection(const string16& text) { |
| 152 const UChar* string = text.c_str(); | 158 const UChar* string = text.c_str(); |
| 153 size_t length = text.length(); | 159 size_t length = text.length(); |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 395 begin == kRightToLeftOverride) | 401 begin == kRightToLeftOverride) |
| 396 ++begin_index; | 402 ++begin_index; |
| 397 size_t end_index = text.length() - 1; | 403 size_t end_index = text.length() - 1; |
| 398 if (text[end_index] == kPopDirectionalFormatting) | 404 if (text[end_index] == kPopDirectionalFormatting) |
| 399 --end_index; | 405 --end_index; |
| 400 return text.substr(begin_index, end_index - begin_index + 1); | 406 return text.substr(begin_index, end_index - begin_index + 1); |
| 401 } | 407 } |
| 402 | 408 |
| 403 } // namespace i18n | 409 } // namespace i18n |
| 404 } // namespace base | 410 } // namespace base |
| OLD | NEW |