electron/shell/common/language_util_win.cc
Milan Burda eb291485bb
chore: drop support for Windows 7 / 8 / 8.1 (#36427)
* chore: drop support for Windows 7 & 8

* chore: remove disable-redraw-lock.patch

* chore: update patches

* Update docs/breaking-changes.md

Co-authored-by: Erick Zhao <erick@hotmail.ca>

* Update docs/breaking-changes.md

Co-authored-by: Keeley Hammond <vertedinde@electronjs.org>

* fix breaking-changes.md

* chore: note last supported version

Co-authored-by: Jeremy Rose <jeremya@chromium.org>

* chore: add link to deprecation policy

* Update docs/breaking-changes.md

Co-authored-by: Jeremy Rose <jeremya@chromium.org>

* update README.md

Co-authored-by: Milan Burda <miburda@microsoft.com>
Co-authored-by: PatchUp <73610968+patchup[bot]@users.noreply.github.com>
Co-authored-by: Erick Zhao <erick@hotmail.ca>
Co-authored-by: Keeley Hammond <vertedinde@electronjs.org>
Co-authored-by: Jeremy Rose <jeremya@chromium.org>
2022-11-30 17:13:29 -08:00

74 lines
2.1 KiB
C++

// 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 {
bool GetPreferredLanguagesUsingGlobalization(
std::vector<std::wstring>* languages) {
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;
}
std::vector<std::string> GetPreferredLanguages() {
std::vector<std::wstring> 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;
}
} // namespace electron