Skip to content

Commit f176d24

Browse files
sorahnitsakhhanazuki
authored
fix: respect system language preferences on Win/macOS (electron#23247)
This commit fixes electron#18829 Previously the full preferences set to OS was not given to Chromium. Also, this commit improves fallback font selection for CJK text. Chromium uses browser languages to determine fallback fonts on Windows, especially kanji/han characters in CJK. For instance, when user sets 'en-US, ja-JP' to Accept-Language, while Chromium chooses Japanese font for kanji text, but Electron chooses Chinese font. This is because only the first language was given to Accept-Language on Electron. This patch is based on electron#15532 Co-authored-by: Nitish Sakhawalkar <nitsakh@icloud.com> Co-authored-by: Kasumi Hanazuki <kasumi@rollingapple.net> Co-authored-by: Nitish Sakhawalkar <nitsakh@icloud.com> Co-authored-by: Kasumi Hanazuki <kasumi@rollingapple.net>
1 parent 45d1ebe commit f176d24

7 files changed

+168
-3
lines changed

filenames.gni

+4
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,10 @@ filenames = {
543543
"shell/common/key_weak_map.h",
544544
"shell/common/keyboard_util.cc",
545545
"shell/common/keyboard_util.h",
546+
"shell/common/language_util.h",
547+
"shell/common/language_util_linux.cc",
548+
"shell/common/language_util_mac.mm",
549+
"shell/common/language_util_win.cc",
546550
"shell/common/mac/main_application_bundle.h",
547551
"shell/common/mac/main_application_bundle.mm",
548552
"shell/common/mouse_util.cc",

shell/browser/api/electron_api_web_contents.cc

+18-2
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
#include "shell/common/gin_converters/value_converter.h"
9090
#include "shell/common/gin_helper/dictionary.h"
9191
#include "shell/common/gin_helper/object_template_builder.h"
92+
#include "shell/common/language_util.h"
9293
#include "shell/common/mouse_util.h"
9394
#include "shell/common/node_includes.h"
9495
#include "shell/common/options_switches.h"
@@ -100,6 +101,7 @@
100101
#include "third_party/blink/public/mojom/frame/find_in_page.mojom.h"
101102
#include "third_party/blink/public/mojom/frame/fullscreen.mojom.h"
102103
#include "third_party/blink/public/mojom/messaging/transferable_message.mojom.h"
104+
#include "third_party/blink/public/mojom/renderer_preferences.mojom.h"
103105
#include "ui/base/cursor/cursor.h"
104106
#include "ui/base/mojom/cursor_type.mojom-shared.h"
105107
#include "ui/display/screen.h"
@@ -121,7 +123,6 @@
121123
#endif
122124

123125
#if defined(OS_LINUX) || defined(OS_WIN)
124-
#include "third_party/blink/public/mojom/renderer_preferences.mojom.h"
125126
#include "ui/gfx/font_render_params.h"
126127
#endif
127128

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

537538
auto* prefs = web_contents()->GetMutableRendererPrefs();
538-
prefs->accept_languages = g_browser_process->GetApplicationLocale();
539+
540+
// Collect preferred languages from OS and browser process. accept_languages
541+
// effects HTTP header, navigator.languages, and CJK fallback font selection.
542+
//
543+
// Note that an application locale set to the browser process might be
544+
// different with the one set to the preference list.
545+
// (e.g. overridden with --lang)
546+
std::string accept_languages =
547+
g_browser_process->GetApplicationLocale() + ",";
548+
for (auto const& language : electron::GetPreferredLanguages()) {
549+
if (language == g_browser_process->GetApplicationLocale())
550+
continue;
551+
accept_languages += language + ",";
552+
}
553+
accept_languages.pop_back();
554+
prefs->accept_languages = accept_languages;
539555

540556
#if defined(OS_LINUX) || defined(OS_WIN)
541557
// Update font settings.

shell/common/language_util.h

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) 2020 GitHub, Inc.
2+
// Use of this source code is governed by the MIT license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef SHELL_COMMON_LANGUAGE_UTIL_H_
6+
#define SHELL_COMMON_LANGUAGE_UTIL_H_
7+
8+
#include <string>
9+
#include <vector>
10+
11+
#include "base/strings/string16.h"
12+
13+
namespace electron {
14+
15+
// Return a list of user preferred languages from OS. The list doesn't include
16+
// overrides from command line arguments.
17+
std::vector<std::string> GetPreferredLanguages();
18+
19+
#if defined(OS_WIN)
20+
bool GetPreferredLanguagesUsingGlobalization(
21+
std::vector<base::string16>* languages);
22+
#endif
23+
24+
} // namespace electron
25+
26+
#endif // SHELL_COMMON_LANGUAGE_UTIL_H_

shell/common/language_util_linux.cc

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) 2020 GitHub, Inc.
2+
// Use of this source code is governed by the MIT license that can be
3+
// found in the LICENSE file.
4+
5+
#include "shell/common/language_util.h"
6+
7+
#include "ui/base/l10n/l10n_util.h"
8+
9+
namespace electron {
10+
11+
std::vector<std::string> GetPreferredLanguages() {
12+
// Return empty as there's no API to use. You may be able to use
13+
// GetApplicationLocale() of a browser process.
14+
return std::vector<std::string>{};
15+
}
16+
17+
} // namespace electron

shell/common/language_util_mac.mm

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) 2020 GitHub, Inc.
2+
// Use of this source code is governed by the MIT license that can be
3+
// found in the LICENSE file.
4+
5+
#include "shell/common/language_util.h"
6+
7+
#import <Cocoa/Cocoa.h>
8+
#include <string>
9+
#include <vector>
10+
11+
#include "base/strings/sys_string_conversions.h"
12+
13+
namespace electron {
14+
15+
std::vector<std::string> GetPreferredLanguages() {
16+
__block std::vector<std::string> languages;
17+
[[NSLocale preferredLanguages]
18+
enumerateObjectsUsingBlock:^(NSString* language, NSUInteger i,
19+
BOOL* stop) {
20+
languages.push_back(base::SysNSStringToUTF8(language));
21+
}];
22+
return languages;
23+
}
24+
25+
} // namespace electron

shell/common/language_util_win.cc

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (c) 2020 GitHub, Inc.
2+
// Use of this source code is governed by the MIT license that can be
3+
// found in the LICENSE file.
4+
5+
#include "shell/common/language_util.h"
6+
7+
#include <roapi.h>
8+
#include <windows.system.userprofile.h>
9+
#include <wrl.h>
10+
11+
#include "base/strings/sys_string_conversions.h"
12+
#include "base/win/core_winrt_util.h"
13+
#include "base/win/i18n.h"
14+
#include "base/win/win_util.h"
15+
#include "base/win/windows_version.h"
16+
17+
namespace electron {
18+
19+
std::vector<std::string> GetPreferredLanguages() {
20+
std::vector<base::string16> languages16;
21+
22+
// Attempt to use API available on Windows 10 or later, which
23+
// returns the full list of language preferences.
24+
if (!GetPreferredLanguagesUsingGlobalization(&languages16)) {
25+
base::win::i18n::GetThreadPreferredUILanguageList(&languages16);
26+
}
27+
28+
std::vector<std::string> languages;
29+
for (const auto& language : languages16) {
30+
languages.push_back(base::SysWideToUTF8(language));
31+
}
32+
return languages;
33+
}
34+
35+
bool GetPreferredLanguagesUsingGlobalization(
36+
std::vector<base::string16>* languages) {
37+
if (base::win::GetVersion() < base::win::Version::WIN10)
38+
return false;
39+
if (!base::win::ResolveCoreWinRTDelayload() ||
40+
!base::win::ScopedHString::ResolveCoreWinRTStringDelayload())
41+
return false;
42+
43+
base::win::ScopedHString guid = base::win::ScopedHString::Create(
44+
RuntimeClass_Windows_System_UserProfile_GlobalizationPreferences);
45+
Microsoft::WRL::ComPtr<
46+
ABI::Windows::System::UserProfile::IGlobalizationPreferencesStatics>
47+
prefs;
48+
49+
HRESULT hr =
50+
base::win::RoGetActivationFactory(guid.get(), IID_PPV_ARGS(&prefs));
51+
if (FAILED(hr))
52+
return false;
53+
54+
ABI::Windows::Foundation::Collections::IVectorView<HSTRING>* langs;
55+
hr = prefs->get_Languages(&langs);
56+
if (FAILED(hr))
57+
return false;
58+
59+
unsigned size;
60+
hr = langs->get_Size(&size);
61+
if (FAILED(hr))
62+
return false;
63+
64+
for (unsigned i = 0; i < size; ++i) {
65+
HSTRING hstr;
66+
hr = langs->GetAt(i, &hstr);
67+
if (SUCCEEDED(hr)) {
68+
base::WStringPiece str = base::win::ScopedHString(hstr).Get();
69+
languages->emplace_back(str.data(), str.size());
70+
}
71+
}
72+
73+
return true;
74+
}
75+
76+
} // namespace electron

spec-main/chromium-spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,8 @@ describe('chromium features', () => {
319319
const w = new BrowserWindow({ show: false });
320320
await w.loadURL('about:blank');
321321
const languages = await w.webContents.executeJavaScript('navigator.languages');
322-
expect(languages).to.deep.equal([appLocale]);
322+
expect(languages.length).to.be.greaterThan(0);
323+
expect(languages).to.contain(appLocale);
323324
});
324325
});
325326

0 commit comments

Comments
 (0)