Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(163)

Side by Side Diff: base/i18n/rtl.cc

Issue 834183005: Move two locale functions from rtl.h into l10n_util.h (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/strings/string_util.h" 9 #include "base/strings/string_util.h"
10 #include "base/strings/sys_string_conversions.h" 10 #include "base/strings/sys_string_conversions.h"
11 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "third_party/icu/source/common/unicode/locid.h" 12 #include "third_party/icu/source/common/unicode/locid.h"
13 #include "third_party/icu/source/common/unicode/uchar.h" 13 #include "third_party/icu/source/common/unicode/uchar.h"
14 #include "third_party/icu/source/common/unicode/uscript.h" 14 #include "third_party/icu/source/common/unicode/uscript.h"
15 #include "third_party/icu/source/i18n/unicode/coll.h" 15 #include "third_party/icu/source/i18n/unicode/coll.h"
16 16
17 namespace { 17 namespace {
18 18
19 // Extract language, country and variant, but ignore keywords. For example,
20 // en-US, ca@valencia, ca-ES@valencia.
21 std::string GetLocaleString(const icu::Locale& locale) {
22 const char* language = locale.getLanguage();
23 const char* country = locale.getCountry();
24 const char* variant = locale.getVariant();
25
26 std::string result =
27 (language != NULL && *language != '\0') ? language : "und";
28
29 if (country != NULL && *country != '\0') {
30 result += '-';
31 result += country;
32 }
33
34 if (variant != NULL && *variant != '\0') {
35 std::string variant_str(variant);
36 base::StringToLowerASCII(&variant_str);
37 result += '@' + variant_str;
38 }
39
40 return result;
41 }
42
43 // Returns LEFT_TO_RIGHT or RIGHT_TO_LEFT if |character| has strong 19 // Returns LEFT_TO_RIGHT or RIGHT_TO_LEFT if |character| has strong
44 // directionality, returns UNKNOWN_DIRECTION if it doesn't. Please refer to 20 // directionality, returns UNKNOWN_DIRECTION if it doesn't. Please refer to
45 // http://unicode.org/reports/tr9/ for more information. 21 // http://unicode.org/reports/tr9/ for more information.
46 base::i18n::TextDirection GetCharacterDirection(UChar32 character) { 22 base::i18n::TextDirection GetCharacterDirection(UChar32 character) {
47 // Now that we have the character, we use ICU in order to query for the 23 // Now that we have the character, we use ICU in order to query for the
48 // appropriate Unicode BiDi character type. 24 // appropriate Unicode BiDi character type.
49 int32_t property = u_getIntPropertyValue(character, UCHAR_BIDI_CLASS); 25 int32_t property = u_getIntPropertyValue(character, UCHAR_BIDI_CLASS);
50 if ((property == U_RIGHT_TO_LEFT) || 26 if ((property == U_RIGHT_TO_LEFT) ||
51 (property == U_RIGHT_TO_LEFT_ARABIC) || 27 (property == U_RIGHT_TO_LEFT_ARABIC) ||
52 (property == U_RIGHT_TO_LEFT_EMBEDDING) || 28 (property == U_RIGHT_TO_LEFT_EMBEDDING) ||
53 (property == U_RIGHT_TO_LEFT_OVERRIDE)) { 29 (property == U_RIGHT_TO_LEFT_OVERRIDE)) {
54 return base::i18n::RIGHT_TO_LEFT; 30 return base::i18n::RIGHT_TO_LEFT;
55 } else if ((property == U_LEFT_TO_RIGHT) || 31 } else if ((property == U_LEFT_TO_RIGHT) ||
56 (property == U_LEFT_TO_RIGHT_EMBEDDING) || 32 (property == U_LEFT_TO_RIGHT_EMBEDDING) ||
57 (property == U_LEFT_TO_RIGHT_OVERRIDE)) { 33 (property == U_LEFT_TO_RIGHT_OVERRIDE)) {
58 return base::i18n::LEFT_TO_RIGHT; 34 return base::i18n::LEFT_TO_RIGHT;
59 } 35 }
60 return base::i18n::UNKNOWN_DIRECTION; 36 return base::i18n::UNKNOWN_DIRECTION;
61 } 37 }
62 38
63 } // namespace 39 } // namespace
64 40
65 namespace base { 41 namespace base {
66 namespace i18n { 42 namespace i18n {
67 43
68 // Represents the locale-specific ICU text direction. 44 // Represents the locale-specific ICU text direction.
69 static TextDirection g_icu_text_direction = UNKNOWN_DIRECTION; 45 static TextDirection g_icu_text_direction = UNKNOWN_DIRECTION;
70 46
71 // Convert the ICU default locale to a string.
72 std::string GetConfiguredLocale() {
73 return GetLocaleString(icu::Locale::getDefault());
74 }
75
76 // Convert the ICU canonicalized locale to a string.
77 std::string GetCanonicalLocale(const char* locale) {
78 return GetLocaleString(icu::Locale::createCanonical(locale));
79 }
80
81 // Convert Chrome locale name to ICU locale name 47 // Convert Chrome locale name to ICU locale name
82 std::string ICULocaleName(const std::string& locale_string) { 48 std::string ICULocaleName(const std::string& locale_string) {
83 // If not Spanish, just return it. 49 // If not Spanish, just return it.
84 if (locale_string.substr(0, 2) != "es") 50 if (locale_string.substr(0, 2) != "es")
85 return locale_string; 51 return locale_string;
86 // Expand es to es-ES. 52 // Expand es to es-ES.
87 if (LowerCaseEqualsASCII(locale_string, "es")) 53 if (LowerCaseEqualsASCII(locale_string, "es"))
88 return "es-ES"; 54 return "es-ES";
89 // Map es-419 (Latin American Spanish) to es-FOO depending on the system 55 // Map es-419 (Latin American Spanish) to es-FOO depending on the system
90 // locale. If it's es-RR other than es-ES, map to es-RR. Otherwise, map 56 // locale. If it's es-RR other than es-ES, map to es-RR. Otherwise, map
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after
385 begin == kRightToLeftOverride) 351 begin == kRightToLeftOverride)
386 ++begin_index; 352 ++begin_index;
387 size_t end_index = text.length() - 1; 353 size_t end_index = text.length() - 1;
388 if (text[end_index] == kPopDirectionalFormatting) 354 if (text[end_index] == kPopDirectionalFormatting)
389 --end_index; 355 --end_index;
390 return text.substr(begin_index, end_index - begin_index + 1); 356 return text.substr(begin_index, end_index - begin_index + 1);
391 } 357 }
392 358
393 } // namespace i18n 359 } // namespace i18n
394 } // namespace base 360 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698