Commit 47692796 authored by Evan Stade's avatar Evan Stade Committed by Commit Bot

WebLayer: set renderer locale/language to match system locale

This fixes navigator.language, navigator.languages and the
ACCEPT-LANGUAGE http header.

For now, the locale is only read at startup. In the future, WebLayer
should listen for changes to the system locale and allow the client
to specify an active locale that is not the system's default.

      LANGUAGE environment variable for Linux WebLayer shell.
      Navigate to test pages in linked bug, or google.com

Test: set Android system language to non English, or use
Bug: 1022616
Change-Id: Id680f38953c1a81fc155ef0d3434fa8fc0d59b6d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1906635Reviewed-by: default avatarJohn Abd-El-Malek <jam@chromium.org>
Commit-Queue: Evan Stade <estade@chromium.org>
Cr-Commit-Position: refs/heads/master@{#715444}
parent 869824a4
...@@ -142,6 +142,8 @@ jumbo_static_library("weblayer_lib") { ...@@ -142,6 +142,8 @@ jumbo_static_library("weblayer_lib") {
"browser/content_browser_client_impl.h", "browser/content_browser_client_impl.h",
"browser/file_select_helper.cc", "browser/file_select_helper.cc",
"browser/file_select_helper.h", "browser/file_select_helper.h",
"browser/i18n_util.cc",
"browser/i18n_util.h",
"browser/isolated_world_ids.h", "browser/isolated_world_ids.h",
"browser/navigation_controller_impl.cc", "browser/navigation_controller_impl.cc",
"browser/navigation_controller_impl.h", "browser/navigation_controller_impl.h",
...@@ -242,6 +244,7 @@ jumbo_static_library("weblayer_lib") { ...@@ -242,6 +244,7 @@ jumbo_static_library("weblayer_lib") {
"//services/network/public/mojom", "//services/network/public/mojom",
"//services/service_manager/embedder:embedder_result_codes", "//services/service_manager/embedder:embedder_result_codes",
"//skia", "//skia",
"//third_party/blink/public/common",
"//third_party/blink/public/strings", "//third_party/blink/public/strings",
"//third_party/icu", "//third_party/icu",
"//ui/base", "//ui/base",
......
...@@ -196,13 +196,13 @@ void ContentMainDelegateImpl::InitializeResourceBundle() { ...@@ -196,13 +196,13 @@ void ContentMainDelegateImpl::InitializeResourceBundle() {
command_line.GetSwitchValueASCII(switches::kProcessType).empty(); command_line.GetSwitchValueASCII(switches::kProcessType).empty();
if (is_browser_process) { if (is_browser_process) {
ui::SetLocalePaksStoredInApk(true); ui::SetLocalePaksStoredInApk(true);
// Passing an empty |pref_locale| yields the system default locale.
std::string locale = ui::ResourceBundle::InitSharedInstanceWithLocale( std::string locale = ui::ResourceBundle::InitSharedInstanceWithLocale(
base::android::GetDefaultLocaleString(), nullptr, {} /*pref_locale*/, nullptr, ui::ResourceBundle::LOAD_COMMON_RESOURCES);
ui::ResourceBundle::LOAD_COMMON_RESOURCES);
if (locale.empty()) { if (locale.empty()) {
LOG(WARNING) << "Failed to load locale .pak from apk."; LOG(WARNING) << "Failed to load locale .pak from apk.";
} }
base::i18n::SetICUDefaultLocale(locale);
// Try to directly mmap the resources.pak from the apk. Fall back to load // Try to directly mmap the resources.pak from the apk. Fall back to load
// from file, using PATH_SERVICE, otherwise. // from file, using PATH_SERVICE, otherwise.
......
...@@ -19,6 +19,7 @@ include_rules = [ ...@@ -19,6 +19,7 @@ include_rules = [
"+services/network/public", "+services/network/public",
"+services/service_manager", "+services/service_manager",
"+third_party/blink/public/common", "+third_party/blink/public/common",
"+third_party/blink/public/mojom",
"+ui/aura", "+ui/aura",
"+ui/android", "+ui/android",
"+ui/base", "+ui/base",
......
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
#include "url/gurl.h" #include "url/gurl.h"
#include "url/origin.h" #include "url/origin.h"
#include "weblayer/browser/browser_main_parts_impl.h" #include "weblayer/browser/browser_main_parts_impl.h"
#include "weblayer/browser/i18n_util.h"
#include "weblayer/browser/ssl_error_handler.h" #include "weblayer/browser/ssl_error_handler.h"
#include "weblayer/browser/tab_impl.h" #include "weblayer/browser/tab_impl.h"
#include "weblayer/browser/weblayer_content_browser_overlay_manifest.h" #include "weblayer/browser/weblayer_content_browser_overlay_manifest.h"
...@@ -96,9 +97,13 @@ ContentBrowserClientImpl::CreateBrowserMainParts( ...@@ -96,9 +97,13 @@ ContentBrowserClientImpl::CreateBrowserMainParts(
return browser_main_parts; return browser_main_parts;
} }
std::string ContentBrowserClientImpl::GetApplicationLocale() {
return i18n::GetApplicationLocale();
}
std::string ContentBrowserClientImpl::GetAcceptLangs( std::string ContentBrowserClientImpl::GetAcceptLangs(
content::BrowserContext* context) { content::BrowserContext* context) {
return "en-us,en"; return i18n::GetAcceptLangs();
} }
content::WebContentsViewDelegate* content::WebContentsViewDelegate*
...@@ -172,7 +177,7 @@ ContentBrowserClientImpl::CreateNetworkContext( ...@@ -172,7 +177,7 @@ ContentBrowserClientImpl::CreateNetworkContext(
network::mojom::NetworkContextParamsPtr context_params = network::mojom::NetworkContextParamsPtr context_params =
network::mojom::NetworkContextParams::New(); network::mojom::NetworkContextParams::New();
context_params->user_agent = GetUserAgent(); context_params->user_agent = GetUserAgent();
context_params->accept_language = "en-us,en"; context_params->accept_language = GetAcceptLangs(context);
if (!context->IsOffTheRecord()) { if (!context->IsOffTheRecord()) {
base::FilePath cookie_path = context->GetPath(); base::FilePath cookie_path = context->GetPath();
cookie_path = cookie_path.Append(FILE_PATH_LITERAL("Cookies")); cookie_path = cookie_path.Append(FILE_PATH_LITERAL("Cookies"));
......
...@@ -28,6 +28,7 @@ class ContentBrowserClientImpl : public content::ContentBrowserClient { ...@@ -28,6 +28,7 @@ class ContentBrowserClientImpl : public content::ContentBrowserClient {
// ContentBrowserClient overrides. // ContentBrowserClient overrides.
std::unique_ptr<content::BrowserMainParts> CreateBrowserMainParts( std::unique_ptr<content::BrowserMainParts> CreateBrowserMainParts(
const content::MainFunctionParams& parameters) override; const content::MainFunctionParams& parameters) override;
std::string GetApplicationLocale() override;
std::string GetAcceptLangs(content::BrowserContext* context) override; std::string GetAcceptLangs(content::BrowserContext* context) override;
content::WebContentsViewDelegate* GetWebContentsViewDelegate( content::WebContentsViewDelegate* GetWebContentsViewDelegate(
content::WebContents* web_contents) override; content::WebContents* web_contents) override;
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "weblayer/browser/i18n_util.h"
#include "base/i18n/rtl.h"
#include "net/http/http_util.h"
namespace weblayer {
namespace i18n {
std::string GetApplicationLocale() {
// The locale is set in ContentMainDelegateImpl::InitializeResourceBundle().
return base::i18n::GetConfiguredLocale();
}
std::string GetAcceptLangs() {
// TODO(estade): return more languages, not just the default.
return net::HttpUtil::ExpandLanguageList(GetApplicationLocale());
}
} // namespace i18n
} // namespace weblayer
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef WEBLAYER_BROWSER_I18N_UTIL_H_
#define WEBLAYER_BROWSER_I18N_UTIL_H_
#include <string>
namespace weblayer {
namespace i18n {
// Returns the currently-in-use ICU locale. This may be called on any thread.
std::string GetApplicationLocale();
// Returns a list of locales suitable for use in the ACCEPT-LANGUAGE header.
// This may be called on any thread.
std::string GetAcceptLangs();
} // namespace i18n
} // namespace weblayer
#endif // WEBLAYER_BROWSER_I18N_UTIL_H_
...@@ -13,8 +13,10 @@ ...@@ -13,8 +13,10 @@
#include "content/public/browser/render_view_host.h" #include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "content/public/common/browser_controls_state.h" #include "content/public/common/browser_controls_state.h"
#include "third_party/blink/public/mojom/renderer_preferences.mojom.h"
#include "ui/base/window_open_disposition.h" #include "ui/base/window_open_disposition.h"
#include "weblayer/browser/file_select_helper.h" #include "weblayer/browser/file_select_helper.h"
#include "weblayer/browser/i18n_util.h"
#include "weblayer/browser/isolated_world_ids.h" #include "weblayer/browser/isolated_world_ids.h"
#include "weblayer/browser/navigation_controller_impl.h" #include "weblayer/browser/navigation_controller_impl.h"
#include "weblayer/browser/profile_impl.h" #include "weblayer/browser/profile_impl.h"
...@@ -107,6 +109,12 @@ TabImpl::TabImpl(ProfileImpl* profile, ...@@ -107,6 +109,12 @@ TabImpl::TabImpl(ProfileImpl* profile,
profile_->GetBrowserContext()); profile_->GetBrowserContext());
web_contents_ = content::WebContents::Create(create_params); web_contents_ = content::WebContents::Create(create_params);
} }
// TODO(estade): set more preferences, and set them dynamically rather than
// just at startup.
web_contents_->GetMutableRendererPrefs()->accept_languages =
i18n::GetAcceptLangs();
std::unique_ptr<UserData> user_data = std::make_unique<UserData>(); std::unique_ptr<UserData> user_data = std::make_unique<UserData>();
user_data->controller = this; user_data->controller = this;
web_contents_->SetUserData(&kWebContentsUserDataKey, std::move(user_data)); web_contents_->SetUserData(&kWebContentsUserDataKey, std::move(user_data));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment