From f8f606ef03a8afd7564c7f5a1bf82b54e6a6b392 Mon Sep 17 00:00:00 2001 From: "evan@chromium.org" Date: Mon, 1 Mar 2010 08:27:32 +0000 Subject: linux: use ICU direction for determining text direction in renderer We use the GTK text direction in the browser process, but that is not available in the renderer. BUG=36624 TEST=Arabic error pages are properly RTL Review URL: http://codereview.chromium.org/660176 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@40260 0039d316-1c4b-4281-b951-d872f2087c98 --- app/l10n_util.cc | 26 +++++++++++++------------- app/l10n_util.h | 4 ++++ chrome/renderer/localized_error.cc | 22 +++++++++++++++++++--- chrome/renderer/localized_error.h | 2 ++ chrome/renderer/render_view.cc | 3 --- 5 files changed, 38 insertions(+), 19 deletions(-) diff --git a/app/l10n_util.cc b/app/l10n_util.cc index 8d418d2..727fe25 100644 --- a/app/l10n_util.cc +++ b/app/l10n_util.cc @@ -434,8 +434,8 @@ void SplitAndNormalizeLanguageList(const std::string& env_language, namespace l10n_util { -// Represents the locale-specific text direction. -static TextDirection g_text_direction = UNKNOWN_DIRECTION; +// Represents the locale-specific ICU text direction. +static TextDirection g_icu_text_direction = UNKNOWN_DIRECTION; std::string GetApplicationLocale(const std::wstring& pref_locale) { #if !defined(OS_MACOSX) @@ -809,21 +809,21 @@ string16 ToUpper(const string16& string) { return result; } -// Returns the text direction for the default ICU locale. It is assumed -// that SetICUDefaultLocale has been called to set the default locale to -// the UI locale of Chrome. +TextDirection GetICUTextDirection() { + if (g_icu_text_direction == UNKNOWN_DIRECTION) { + const icu::Locale& locale = icu::Locale::getDefault(); + g_icu_text_direction = GetTextDirectionForLocale(locale.getName()); + } + return g_icu_text_direction; +} + TextDirection GetTextDirection() { - if (g_text_direction == UNKNOWN_DIRECTION) { #if defined(TOOLKIT_GTK) - GtkTextDirection gtk_dir = gtk_widget_get_default_direction(); - g_text_direction = - (gtk_dir == GTK_TEXT_DIR_LTR) ? LEFT_TO_RIGHT : RIGHT_TO_LEFT; + GtkTextDirection gtk_dir = gtk_widget_get_default_direction(); + return (gtk_dir == GTK_TEXT_DIR_LTR) ? LEFT_TO_RIGHT : RIGHT_TO_LEFT; #else - const icu::Locale& locale = icu::Locale::getDefault(); - g_text_direction = GetTextDirectionForLocale(locale.getName()); + return GetICUTextDirection(); #endif - } - return g_text_direction; } TextDirection GetTextDirectionForLocale(const char* locale_name) { diff --git a/app/l10n_util.h b/app/l10n_util.h index 66b9d23..b87442c 100644 --- a/app/l10n_util.h +++ b/app/l10n_util.h @@ -194,6 +194,10 @@ enum TextDirection { // * LEFT_TO_RIGHT: Left-To-Right (e.g. English, Chinese, etc.); // * RIGHT_TO_LEFT: Right-To-Left (e.g. Arabic, Hebrew, etc.), and; // * UNKNOWN_DIRECTION: unknown (or error). +TextDirection GetICUTextDirection(); + +// Get the application text direction. (This is just the ICU direction, +// except on GTK.) TextDirection GetTextDirection(); // Returns the text direction for |locale_name|. diff --git a/chrome/renderer/localized_error.cc b/chrome/renderer/localized_error.cc index 2b49390..37dea28 100644 --- a/chrome/renderer/localized_error.cc +++ b/chrome/renderer/localized_error.cc @@ -90,10 +90,23 @@ WebErrorNetErrorMap net_error_options[] = { }, }; +bool LocaleIsRTL() { +#if defined(TOOLKIT_GTK) + // l10n_util::GetTextDirection uses the GTK text direction, which doesn't work + // within the renderer sandbox. + return l10n_util::GetICUTextDirection() == l10n_util::RIGHT_TO_LEFT; +#else + return l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT; +#endif +} + } // namespace void GetLocalizedErrorValues(const WebURLError& error, DictionaryValue* error_strings) { + bool rtl = LocaleIsRTL(); + error_strings->SetString(L"textdirection", rtl ? L"rtl" : L"ltr"); + // Grab strings that are applicable to all error pages error_strings->SetString(L"detailsLink", l10n_util::GetString(IDS_ERRORPAGES_DETAILS_LINK)); @@ -128,7 +141,7 @@ void GetLocalizedErrorValues(const WebURLError& error, std::wstring failed_url( ASCIIToWide(std::string(error.unreachableURL.spec()))); // URLs are always LTR. - if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT) + if (rtl) l10n_util::WrapStringWithLTRFormatting(&failed_url); error_strings->SetString(L"title", l10n_util::GetStringF(options.title_resource_id, @@ -169,7 +182,7 @@ void GetLocalizedErrorValues(const WebURLError& error, l10n_util::GetString(IDS_ERRORPAGES_SUGGESTION_HOMEPAGE)); std::wstring homepage(ASCIIToWide(failed_url.GetWithEmptyPath().spec())); // URLs are always LTR. - if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT) + if (rtl) l10n_util::WrapStringWithLTRFormatting(&homepage); suggest_home_page->SetString(L"homePage", homepage); // TODO(tc): we actually want the unicode hostname @@ -209,9 +222,12 @@ void GetLocalizedErrorValues(const WebURLError& error, void GetFormRepostErrorValues(const GURL& display_url, DictionaryValue* error_strings) { + bool rtl = LocaleIsRTL(); + error_strings->SetString(L"textdirection", rtl ? L"rtl" : L"ltr"); + std::wstring failed_url(ASCIIToWide(display_url.spec())); // URLs are always LTR. - if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT) + if (rtl) l10n_util::WrapStringWithLTRFormatting(&failed_url); error_strings->SetString( L"title", l10n_util::GetStringF(IDS_ERRORPAGES_TITLE_NOT_AVAILABLE, diff --git a/chrome/renderer/localized_error.h b/chrome/renderer/localized_error.h index 33300c4..1b3464b 100644 --- a/chrome/renderer/localized_error.h +++ b/chrome/renderer/localized_error.h @@ -12,6 +12,8 @@ namespace WebKit { struct WebURLError; } +// Fills |error_strings| with values to be used to build an error page used +// on HTTP errors, like 404 or connection reset. void GetLocalizedErrorValues(const WebKit::WebURLError& error, DictionaryValue* error_strings); diff --git a/chrome/renderer/render_view.cc b/chrome/renderer/render_view.cc index b3ef04b..fdf49a5 100644 --- a/chrome/renderer/render_view.cc +++ b/chrome/renderer/render_view.cc @@ -1391,9 +1391,6 @@ void RenderView::LoadNavigationErrorPage(WebFrame* frame, GetLocalizedErrorValues(error, &error_strings); resource_id = IDR_NET_ERROR_HTML; } - error_strings.SetString(L"textdirection", - (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT) ? - L"rtl" : L"ltr"); alt_html = GetAltHTMLForTemplate(error_strings, resource_id); } else { -- cgit v1.1