forked from electron/electron
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: respect language preferences on Win/macOS
This commit partially fixes electron#18829 Chromium uses navigator.languages (Accept-Language) to determine fallback fonts on Windows, especially kanji/han characters in CJK. Previously the full preferences set to OS was not given to Chromium. 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 is 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>
- Loading branch information
1 parent
6ab317c
commit 02de6fa
Showing
7 changed files
with
121 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// 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/browser/api/electron_api_web_contents.h" | ||
|
||
#include "chrome/browser/browser_process.h" | ||
#include "third_party/blink/public/mojom/renderer_preferences.mojom.h" | ||
|
||
namespace electron { | ||
|
||
namespace api { | ||
|
||
void WebContents::SetAcceptLanguages(blink::mojom::RendererPreferences* prefs) { | ||
prefs->accept_languages = g_browser_process->GetApplicationLocale(); | ||
} | ||
|
||
} // namespace api | ||
|
||
} // namespace electron |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// 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/browser/api/electron_api_web_contents.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" | ||
#include "third_party/blink/public/mojom/renderer_preferences.mojom.h" | ||
|
||
namespace electron { | ||
|
||
namespace api { | ||
|
||
void WebContents::SetAcceptLanguages(blink::mojom::RendererPreferences* prefs) { | ||
std::vector<base::string16> languages; | ||
|
||
// Attempt to use API available on Windows 10 or later, which | ||
// returns the full list of language preferences. | ||
if (!GetLanguagesUsingGlobalization(&languages)) { | ||
base::win::i18n::GetThreadPreferredUILanguageList(&languages); | ||
} | ||
|
||
std::string accept_langs; | ||
for (const auto& language : languages) { | ||
accept_langs += base::SysWideToUTF8(language) + ','; | ||
} | ||
accept_langs.pop_back(); | ||
prefs->accept_languages = accept_langs; | ||
} | ||
|
||
bool WebContents::GetLanguagesUsingGlobalization( | ||
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 api | ||
|
||
} // namespace electron |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters