Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: respect system language preferences on Windows & macOS #23247

Merged
merged 1 commit into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions filenames.gni
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,10 @@ filenames = {
"shell/common/key_weak_map.h",
"shell/common/keyboard_util.cc",
"shell/common/keyboard_util.h",
"shell/common/language_util.h",
"shell/common/language_util_linux.cc",
"shell/common/language_util_mac.mm",
"shell/common/language_util_win.cc",
"shell/common/mac/main_application_bundle.h",
"shell/common/mac/main_application_bundle.mm",
"shell/common/mouse_util.cc",
Expand Down
20 changes: 18 additions & 2 deletions shell/browser/api/electron_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
#include "shell/common/gin_converters/value_converter.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/gin_helper/object_template_builder.h"
#include "shell/common/language_util.h"
#include "shell/common/mouse_util.h"
#include "shell/common/node_includes.h"
#include "shell/common/options_switches.h"
Expand All @@ -100,6 +101,7 @@
#include "third_party/blink/public/mojom/frame/find_in_page.mojom.h"
#include "third_party/blink/public/mojom/frame/fullscreen.mojom.h"
#include "third_party/blink/public/mojom/messaging/transferable_message.mojom.h"
#include "third_party/blink/public/mojom/renderer_preferences.mojom.h"
#include "ui/base/cursor/cursor.h"
#include "ui/base/mojom/cursor_type.mojom-shared.h"
#include "ui/display/screen.h"
Expand All @@ -121,7 +123,6 @@
#endif

#if defined(OS_LINUX) || defined(OS_WIN)
#include "third_party/blink/public/mojom/renderer_preferences.mojom.h"
#include "ui/gfx/font_render_params.h"
#endif

Expand Down Expand Up @@ -535,7 +536,22 @@ void WebContents::InitWithSessionAndOptions(
managed_web_contents()->GetView()->SetDelegate(this);

auto* prefs = web_contents()->GetMutableRendererPrefs();
prefs->accept_languages = g_browser_process->GetApplicationLocale();

// Collect preferred languages from OS and browser process. accept_languages
// effects HTTP header, navigator.languages, and CJK fallback font selection.
//
// Note that an application locale set to the browser process might be
// different with the one set to the preference list.
// (e.g. overridden with --lang)
std::string accept_languages =
g_browser_process->GetApplicationLocale() + ",";
for (auto const& language : electron::GetPreferredLanguages()) {
if (language == g_browser_process->GetApplicationLocale())
deepak1556 marked this conversation as resolved.
Show resolved Hide resolved
continue;
accept_languages += language + ",";
}
accept_languages.pop_back();
prefs->accept_languages = accept_languages;

#if defined(OS_LINUX) || defined(OS_WIN)
// Update font settings.
Expand Down
26 changes: 26 additions & 0 deletions shell/common/language_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2020 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

#ifndef SHELL_COMMON_LANGUAGE_UTIL_H_
#define SHELL_COMMON_LANGUAGE_UTIL_H_

#include <string>
#include <vector>

#include "base/strings/string16.h"

namespace electron {

// Return a list of user preferred languages from OS. The list doesn't include
// overrides from command line arguments.
std::vector<std::string> GetPreferredLanguages();

#if defined(OS_WIN)
bool GetPreferredLanguagesUsingGlobalization(
std::vector<base::string16>* languages);
#endif

} // namespace electron

#endif // SHELL_COMMON_LANGUAGE_UTIL_H_
17 changes: 17 additions & 0 deletions shell/common/language_util_linux.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) 2020 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

#include "shell/common/language_util.h"

#include "ui/base/l10n/l10n_util.h"

namespace electron {

std::vector<std::string> GetPreferredLanguages() {
// Return empty as there's no API to use. You may be able to use
// GetApplicationLocale() of a browser process.
return std::vector<std::string>{};
}

} // namespace electron
25 changes: 25 additions & 0 deletions shell/common/language_util_mac.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2020 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

#include "shell/common/language_util.h"

#import <Cocoa/Cocoa.h>
#include <string>
#include <vector>

#include "base/strings/sys_string_conversions.h"

namespace electron {

std::vector<std::string> GetPreferredLanguages() {
__block std::vector<std::string> languages;
[[NSLocale preferredLanguages]
enumerateObjectsUsingBlock:^(NSString* language, NSUInteger i,
BOOL* stop) {
languages.push_back(base::SysNSStringToUTF8(language));
}];
return languages;
}

} // namespace electron
76 changes: 76 additions & 0 deletions shell/common/language_util_win.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright (c) 2020 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

#include "shell/common/language_util.h"

#include <roapi.h>
#include <windows.system.userprofile.h>
#include <wrl.h>

#include "base/strings/sys_string_conversions.h"
#include "base/win/core_winrt_util.h"
#include "base/win/i18n.h"
#include "base/win/win_util.h"
#include "base/win/windows_version.h"

namespace electron {

std::vector<std::string> GetPreferredLanguages() {
std::vector<base::string16> languages16;

// Attempt to use API available on Windows 10 or later, which
// returns the full list of language preferences.
if (!GetPreferredLanguagesUsingGlobalization(&languages16)) {
base::win::i18n::GetThreadPreferredUILanguageList(&languages16);
}

std::vector<std::string> languages;
for (const auto& language : languages16) {
languages.push_back(base::SysWideToUTF8(language));
}
return languages;
}

bool GetPreferredLanguagesUsingGlobalization(
std::vector<base::string16>* languages) {
if (base::win::GetVersion() < base::win::Version::WIN10)
return false;
if (!base::win::ResolveCoreWinRTDelayload() ||
!base::win::ScopedHString::ResolveCoreWinRTStringDelayload())
return false;

base::win::ScopedHString guid = base::win::ScopedHString::Create(
RuntimeClass_Windows_System_UserProfile_GlobalizationPreferences);
Microsoft::WRL::ComPtr<
ABI::Windows::System::UserProfile::IGlobalizationPreferencesStatics>
prefs;

HRESULT hr =
base::win::RoGetActivationFactory(guid.get(), IID_PPV_ARGS(&prefs));
if (FAILED(hr))
return false;

ABI::Windows::Foundation::Collections::IVectorView<HSTRING>* langs;
hr = prefs->get_Languages(&langs);
if (FAILED(hr))
return false;

unsigned size;
hr = langs->get_Size(&size);
if (FAILED(hr))
return false;

for (unsigned i = 0; i < size; ++i) {
HSTRING hstr;
hr = langs->GetAt(i, &hstr);
if (SUCCEEDED(hr)) {
base::WStringPiece str = base::win::ScopedHString(hstr).Get();
languages->emplace_back(str.data(), str.size());
}
}

return true;
}

} // namespace electron
3 changes: 2 additions & 1 deletion spec-main/chromium-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,8 @@ describe('chromium features', () => {
const w = new BrowserWindow({ show: false });
await w.loadURL('about:blank');
const languages = await w.webContents.executeJavaScript('navigator.languages');
expect(languages).to.deep.equal([appLocale]);
expect(languages.length).to.be.greaterThan(0);
expect(languages).to.contain(appLocale);
});
});

Expand Down