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 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>
- Loading branch information
1 parent
7377bb3
commit 510a454
Showing
7 changed files
with
167 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
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_ |
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,16 @@ | ||
// 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() { | ||
std::vector<std::string> languages = {l10n_util::GetApplicationLocale("")}; | ||
return languages; | ||
} | ||
|
||
} // 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
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() { | ||
std::vector<std::string> languages; | ||
[[NSLocale preferredLanguages] | ||
enumerateObjectsUsingBlock:^(NSString* language, NSUInteger i, | ||
BOOL* stop) { | ||
languages.push_back(base::SysNSStringToUTF8(language)); | ||
}]; | ||
return languages; | ||
} | ||
|
||
} // 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
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 |
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