diff options
-rw-r--r-- | base/i18n/rtl.cc | 36 | ||||
-rw-r--r-- | base/i18n/rtl.h | 6 | ||||
-rw-r--r-- | base/i18n/rtl_unittest.cc | 22 |
3 files changed, 52 insertions, 12 deletions
diff --git a/base/i18n/rtl.cc b/base/i18n/rtl.cc index 93f91c2..ac9589c 100644 --- a/base/i18n/rtl.cc +++ b/base/i18n/rtl.cc @@ -4,9 +4,11 @@ #include "base/i18n/rtl.h" +#include <algorithm> + #include "base/files/file_path.h" #include "base/logging.h" -#include "base/metrics/field_trial.h" +#include "base/strings/string_split.h" #include "base/strings/string_util.h" #include "base/strings/sys_string_conversions.h" #include "base/strings/utf_string_conversions.h" @@ -127,22 +129,32 @@ bool IsRTL() { bool ICUIsRTL() { if (g_icu_text_direction == UNKNOWN_DIRECTION) { const icu::Locale& locale = icu::Locale::getDefault(); - g_icu_text_direction = GetTextDirectionForLocale(locale.getName()); + g_icu_text_direction = GetTextDirectionForLocaleInStartUp(locale.getName()); } return g_icu_text_direction == RIGHT_TO_LEFT; } -TextDirection GetTextDirectionForLocale(const char* locale_name) { - const std::string group_name = - FieldTrialList::FindFullName("LightSpeed"); - // StartsWith allows flexibility for this experiment to apply to multiple - // group names. To start, this will apply to AvoidMMapOnStartup. - if (StartsWith(group_name, "AvoidMMap", CompareCase::SENSITIVE)) { - static const char kEnglishLocale[] = "en_"; - if (StartsWith(locale_name, kEnglishLocale, CompareCase::SENSITIVE)) - return LEFT_TO_RIGHT; - } +TextDirection GetTextDirectionForLocaleInStartUp(const char* locale_name) { +// On iOS, check for RTL forcing. +#if defined(OS_IOS) + if (ios::IsInForcedRTL()) + return RIGHT_TO_LEFT; +#endif + + // This list needs to be updated in alphabetical order if we add more RTL + // locales. + static const char* kRTLLanguageCodes[] = {"ar", "fa", "he", "iw", "ur"}; + std::vector<StringPiece> locale_split = + SplitStringPiece(locale_name, "-_", KEEP_WHITESPACE, SPLIT_WANT_ALL); + const StringPiece& language_code = locale_split[0]; + if (std::binary_search(kRTLLanguageCodes, + kRTLLanguageCodes + arraysize(kRTLLanguageCodes), + language_code)) + return RIGHT_TO_LEFT; + return LEFT_TO_RIGHT; +} +TextDirection GetTextDirectionForLocale(const char* locale_name) { // On iOS, check for RTL forcing. #if defined(OS_IOS) if (ios::IsInForcedRTL()) diff --git a/base/i18n/rtl.h b/base/i18n/rtl.h index 9b9a0dc..bba93ce 100644 --- a/base/i18n/rtl.h +++ b/base/i18n/rtl.h @@ -61,6 +61,12 @@ BASE_I18N_EXPORT bool IsRTL(); BASE_I18N_EXPORT bool ICUIsRTL(); // Returns the text direction for |locale_name|. +// As a startup optimization, this method checks the locale against a list of +// Chrome-supported RTL locales. +BASE_I18N_EXPORT TextDirection +GetTextDirectionForLocaleInStartUp(const char* locale_name); + +// Returns the text direction for |locale_name|. BASE_I18N_EXPORT TextDirection GetTextDirectionForLocale( const char* locale_name); diff --git a/base/i18n/rtl_unittest.cc b/base/i18n/rtl_unittest.cc index 87ac87d..ddaff1e 100644 --- a/base/i18n/rtl_unittest.cc +++ b/base/i18n/rtl_unittest.cc @@ -399,6 +399,28 @@ TEST_F(RTLTest, GetTextDirection) { EXPECT_EQ(LEFT_TO_RIGHT, GetTextDirectionForLocale("ja")); } +TEST_F(RTLTest, GetTextDirectionForLocaleInStartUp) { + EXPECT_EQ(RIGHT_TO_LEFT, GetTextDirectionForLocaleInStartUp("ar")); + EXPECT_EQ(RIGHT_TO_LEFT, GetTextDirectionForLocaleInStartUp("ar_EG")); + EXPECT_EQ(RIGHT_TO_LEFT, GetTextDirectionForLocaleInStartUp("he")); + EXPECT_EQ(RIGHT_TO_LEFT, GetTextDirectionForLocaleInStartUp("he_IL")); + // iw is an obsolete code for Hebrew. + EXPECT_EQ(RIGHT_TO_LEFT, GetTextDirectionForLocaleInStartUp("iw")); + // Although we're not yet localized to Farsi and Urdu, we + // do have the text layout direction information for them. + EXPECT_EQ(RIGHT_TO_LEFT, GetTextDirectionForLocaleInStartUp("fa")); + EXPECT_EQ(RIGHT_TO_LEFT, GetTextDirectionForLocaleInStartUp("ur")); + EXPECT_EQ(LEFT_TO_RIGHT, GetTextDirectionForLocaleInStartUp("en")); + // Chinese in China with '-'. + EXPECT_EQ(LEFT_TO_RIGHT, GetTextDirectionForLocaleInStartUp("zh-CN")); + // Filipino : 3-letter code + EXPECT_EQ(LEFT_TO_RIGHT, GetTextDirectionForLocaleInStartUp("fil")); + // Russian + EXPECT_EQ(LEFT_TO_RIGHT, GetTextDirectionForLocaleInStartUp("ru")); + // Japanese that uses multiple scripts + EXPECT_EQ(LEFT_TO_RIGHT, GetTextDirectionForLocaleInStartUp("ja")); +} + TEST_F(RTLTest, UnadjustStringForLocaleDirection) { // These test strings are borrowed from WrapPathWithLTRFormatting const wchar_t* cases[] = { |