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/atomicops.h" | |
13 #include "base/command_line.h" | 12 #include "base/command_line.h" |
14 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
15 #include "base/i18n/base_i18n_switches.h" | 14 #include "base/i18n/base_i18n_switches.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 "build/build_config.h" | 21 #include "build/build_config.h" |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 | 106 |
108 return base::i18n::UNKNOWN_DIRECTION; | 107 return base::i18n::UNKNOWN_DIRECTION; |
109 } | 108 } |
110 | 109 |
111 } // namespace | 110 } // namespace |
112 | 111 |
113 namespace base { | 112 namespace base { |
114 namespace i18n { | 113 namespace i18n { |
115 | 114 |
116 // Represents the locale-specific ICU text direction. | 115 // Represents the locale-specific ICU text direction. |
117 static subtle::Atomic32 g_icu_text_direction = | 116 static TextDirection g_icu_text_direction = UNKNOWN_DIRECTION; |
118 static_cast<subtle::Atomic32>(UNKNOWN_DIRECTION); | |
119 | 117 |
120 // Convert the ICU default locale to a string. | 118 // Convert the ICU default locale to a string. |
121 std::string GetConfiguredLocale() { | 119 std::string GetConfiguredLocale() { |
122 return GetLocaleString(icu::Locale::getDefault()); | 120 return GetLocaleString(icu::Locale::getDefault()); |
123 } | 121 } |
124 | 122 |
125 // Convert the ICU canonicalized locale to a string. | 123 // Convert the ICU canonicalized locale to a string. |
126 std::string GetCanonicalLocale(const std::string& locale) { | 124 std::string GetCanonicalLocale(const std::string& locale) { |
127 return GetLocaleString(icu::Locale::createCanonical(locale.c_str())); | 125 return GetLocaleString(icu::Locale::createCanonical(locale.c_str())); |
128 } | 126 } |
(...skipping 28 matching lines...) Expand all Loading... |
157 | 155 |
158 void SetICUDefaultLocale(const std::string& locale_string) { | 156 void SetICUDefaultLocale(const std::string& locale_string) { |
159 icu::Locale locale(ICULocaleName(locale_string).c_str()); | 157 icu::Locale locale(ICULocaleName(locale_string).c_str()); |
160 UErrorCode error_code = U_ZERO_ERROR; | 158 UErrorCode error_code = U_ZERO_ERROR; |
161 icu::Locale::setDefault(locale, error_code); | 159 icu::Locale::setDefault(locale, error_code); |
162 // This return value is actually bogus because Locale object is | 160 // This return value is actually bogus because Locale object is |
163 // an ID and setDefault seems to always succeed (regardless of the | 161 // an ID and setDefault seems to always succeed (regardless of the |
164 // presence of actual locale data). However, | 162 // presence of actual locale data). However, |
165 // it does not hurt to have it as a sanity check. | 163 // it does not hurt to have it as a sanity check. |
166 DCHECK(U_SUCCESS(error_code)); | 164 DCHECK(U_SUCCESS(error_code)); |
167 subtle::Release_Store( | 165 g_icu_text_direction = UNKNOWN_DIRECTION; |
168 &g_icu_text_direction, | |
169 static_cast<subtle::Atomic32>( | |
170 GetTextDirectionForLocaleInStartUp(locale.getName()))); | |
171 } | 166 } |
172 | 167 |
173 bool IsRTL() { | 168 bool IsRTL() { |
174 return ICUIsRTL(); | 169 return ICUIsRTL(); |
175 } | 170 } |
176 | 171 |
177 bool ICUIsRTL() { | 172 bool ICUIsRTL() { |
178 // Note: There is still a race if this is executed between the | 173 if (g_icu_text_direction == UNKNOWN_DIRECTION) { |
179 // icu::Locale::setDefault and the g_icu_text_direction store | 174 const icu::Locale& locale = icu::Locale::getDefault(); |
180 // that happens in SetICUDefaultLocale. | 175 g_icu_text_direction = GetTextDirectionForLocaleInStartUp(locale.getName()); |
181 return static_cast<TextDirection>( | 176 } |
182 subtle::Acquire_Load(&g_icu_text_direction)) == RIGHT_TO_LEFT; | 177 return g_icu_text_direction == RIGHT_TO_LEFT; |
183 } | 178 } |
184 | 179 |
185 TextDirection GetTextDirectionForLocaleInStartUp(const char* locale_name) { | 180 TextDirection GetTextDirectionForLocaleInStartUp(const char* locale_name) { |
186 // Check for direction forcing. | 181 // Check for direction forcing. |
187 TextDirection forced_direction = GetForcedTextDirection(); | 182 TextDirection forced_direction = GetForcedTextDirection(); |
188 if (forced_direction != UNKNOWN_DIRECTION) | 183 if (forced_direction != UNKNOWN_DIRECTION) |
189 return forced_direction; | 184 return forced_direction; |
190 | 185 |
191 // This list needs to be updated in alphabetical order if we add more RTL | 186 // This list needs to be updated in alphabetical order if we add more RTL |
192 // locales. | 187 // locales. |
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
461 begin == kRightToLeftOverride) | 456 begin == kRightToLeftOverride) |
462 ++begin_index; | 457 ++begin_index; |
463 size_t end_index = text.length() - 1; | 458 size_t end_index = text.length() - 1; |
464 if (text[end_index] == kPopDirectionalFormatting) | 459 if (text[end_index] == kPopDirectionalFormatting) |
465 --end_index; | 460 --end_index; |
466 return text.substr(begin_index, end_index - begin_index + 1); | 461 return text.substr(begin_index, end_index - begin_index + 1); |
467 } | 462 } |
468 | 463 |
469 } // namespace i18n | 464 } // namespace i18n |
470 } // namespace base | 465 } // namespace base |
OLD | NEW |