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 <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
11 | 11 |
12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
14 #include "base/i18n/base_i18n_switches.h" | 14 #include "base/i18n/base_i18n_switches.h" |
15 #include "base/lazy_instance.h" | |
16 #include "base/logging.h" | 15 #include "base/logging.h" |
17 #include "base/macros.h" | 16 #include "base/macros.h" |
18 #include "base/strings/string_split.h" | 17 #include "base/strings/string_split.h" |
19 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
20 #include "base/strings/sys_string_conversions.h" | 19 #include "base/strings/sys_string_conversions.h" |
21 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
22 #include "base/synchronization/lock.h" | |
23 #include "build/build_config.h" | 21 #include "build/build_config.h" |
24 #include "third_party/icu/source/common/unicode/locid.h" | 22 #include "third_party/icu/source/common/unicode/locid.h" |
25 #include "third_party/icu/source/common/unicode/uchar.h" | 23 #include "third_party/icu/source/common/unicode/uchar.h" |
26 #include "third_party/icu/source/common/unicode/uscript.h" | 24 #include "third_party/icu/source/common/unicode/uscript.h" |
27 #include "third_party/icu/source/i18n/unicode/coll.h" | 25 #include "third_party/icu/source/i18n/unicode/coll.h" |
28 | 26 |
29 #if defined(OS_IOS) | 27 #if defined(OS_IOS) |
30 #include "base/ios/ios_util.h" | 28 #include "base/ios/ios_util.h" |
31 #endif | 29 #endif |
32 | 30 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 | 94 |
97 return base::i18n::UNKNOWN_DIRECTION; | 95 return base::i18n::UNKNOWN_DIRECTION; |
98 } | 96 } |
99 | 97 |
100 } // namespace | 98 } // namespace |
101 | 99 |
102 namespace base { | 100 namespace base { |
103 namespace i18n { | 101 namespace i18n { |
104 | 102 |
105 // Represents the locale-specific ICU text direction. | 103 // Represents the locale-specific ICU text direction. |
106 static base::LazyInstance<base::Lock>::Leaky g_icu_text_direction_lock = | |
107 LAZY_INSTANCE_INITIALIZER; | |
108 static TextDirection g_icu_text_direction = UNKNOWN_DIRECTION; | 104 static TextDirection g_icu_text_direction = UNKNOWN_DIRECTION; |
109 | 105 |
110 // Convert the ICU default locale to a string. | 106 // Convert the ICU default locale to a string. |
111 std::string GetConfiguredLocale() { | 107 std::string GetConfiguredLocale() { |
112 return GetLocaleString(icu::Locale::getDefault()); | 108 return GetLocaleString(icu::Locale::getDefault()); |
113 } | 109 } |
114 | 110 |
115 // Convert the ICU canonicalized locale to a string. | 111 // Convert the ICU canonicalized locale to a string. |
116 std::string GetCanonicalLocale(const std::string& locale) { | 112 std::string GetCanonicalLocale(const std::string& locale) { |
117 return GetLocaleString(icu::Locale::createCanonical(locale.c_str())); | 113 return GetLocaleString(icu::Locale::createCanonical(locale.c_str())); |
(...skipping 29 matching lines...) Expand all Loading... |
147 | 143 |
148 void SetICUDefaultLocale(const std::string& locale_string) { | 144 void SetICUDefaultLocale(const std::string& locale_string) { |
149 icu::Locale locale(ICULocaleName(locale_string).c_str()); | 145 icu::Locale locale(ICULocaleName(locale_string).c_str()); |
150 UErrorCode error_code = U_ZERO_ERROR; | 146 UErrorCode error_code = U_ZERO_ERROR; |
151 icu::Locale::setDefault(locale, error_code); | 147 icu::Locale::setDefault(locale, error_code); |
152 // This return value is actually bogus because Locale object is | 148 // This return value is actually bogus because Locale object is |
153 // an ID and setDefault seems to always succeed (regardless of the | 149 // an ID and setDefault seems to always succeed (regardless of the |
154 // presence of actual locale data). However, | 150 // presence of actual locale data). However, |
155 // it does not hurt to have it as a sanity check. | 151 // it does not hurt to have it as a sanity check. |
156 DCHECK(U_SUCCESS(error_code)); | 152 DCHECK(U_SUCCESS(error_code)); |
157 { | 153 g_icu_text_direction = UNKNOWN_DIRECTION; |
158 base::AutoLock lock(g_icu_text_direction_lock.Get()); | |
159 g_icu_text_direction = UNKNOWN_DIRECTION; | |
160 } | |
161 } | 154 } |
162 | 155 |
163 bool IsRTL() { | 156 bool IsRTL() { |
164 return ICUIsRTL(); | 157 return ICUIsRTL(); |
165 } | 158 } |
166 | 159 |
167 bool ICUIsRTL() { | 160 bool ICUIsRTL() { |
168 base::AutoLock lock(g_icu_text_direction_lock.Get()); | |
169 if (g_icu_text_direction == UNKNOWN_DIRECTION) { | 161 if (g_icu_text_direction == UNKNOWN_DIRECTION) { |
170 const icu::Locale& locale = icu::Locale::getDefault(); | 162 const icu::Locale& locale = icu::Locale::getDefault(); |
171 g_icu_text_direction = GetTextDirectionForLocaleInStartUp(locale.getName()); | 163 g_icu_text_direction = GetTextDirectionForLocaleInStartUp(locale.getName()); |
172 } | 164 } |
173 return g_icu_text_direction == RIGHT_TO_LEFT; | 165 return g_icu_text_direction == RIGHT_TO_LEFT; |
174 } | 166 } |
175 | 167 |
176 TextDirection GetTextDirectionForLocaleInStartUp(const char* locale_name) { | 168 TextDirection GetTextDirectionForLocaleInStartUp(const char* locale_name) { |
177 // Check for direction forcing. | 169 // Check for direction forcing. |
178 TextDirection forced_direction = GetForcedTextDirection(); | 170 TextDirection forced_direction = GetForcedTextDirection(); |
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
452 begin == kRightToLeftOverride) | 444 begin == kRightToLeftOverride) |
453 ++begin_index; | 445 ++begin_index; |
454 size_t end_index = text.length() - 1; | 446 size_t end_index = text.length() - 1; |
455 if (text[end_index] == kPopDirectionalFormatting) | 447 if (text[end_index] == kPopDirectionalFormatting) |
456 --end_index; | 448 --end_index; |
457 return text.substr(begin_index, end_index - begin_index + 1); | 449 return text.substr(begin_index, end_index - begin_index + 1); |
458 } | 450 } |
459 | 451 |
460 } // namespace i18n | 452 } // namespace i18n |
461 } // namespace base | 453 } // namespace base |
OLD | NEW |