Fra*_*eld 7 windows multilingual winapi translation internationalization
我试图从C ++ / Qt应用程序中检索用户首选语言的完整列表,如用户首选项的“区域和语言”页面中所配置的:
为此,我尝试在最新的Windows 10 Pro系统上使用WinAPI函数GetUserPreferredUILanguages()。
但是,该函数始终仅返回第一个条目(Windows主要显示语言)和“ en-US”。如果将英语配置为主语言,则仅返回“ en-US”。例如,如果我配置了(德语,法语,英语),则返回[“ de-de”,“ en-US”],则省略了法语。如果我将更多语言添加到列表中,它们也会被省略。我也查看了“ 用户界面语言管理”,但无济于事。例如,GetSystemPreferredUILanguages()仅返回“ en-US”。GetUILanguageFallbackList()返回[“ de-de”,“ de”,“ en-US”,“ en”]。
我使用的代码:
// calling GetUserPreferredUILanguages() twice, once to get number of
// languages and required buffer size, then to get the actual data
ULONG numberOfLanguages = 0;
DWORD bufferLength = 0;
const auto result1 = GetUserPreferredUILanguages(MUI_LANGUAGE_NAME,
&numberOfLanguages,
nullptr,
&bufferLength);
// result1 is true, numberOfLanguages=2
QVector<wchar_t> languagesBuffer(static_cast<int>(bufferLength));
const auto result2 = GetUserPreferredUILanguages(MUI_LANGUAGE_NAME,
&numberOfLanguages,
languagesBuffer.data(),
&bufferLength);
// result2 is true, languageBuffer contains "de-de", "en-US"
Run Code Online (Sandbox Code Playgroud)
这不是正确使用的功能,还是我对Windows 10中的语言配置有误解?如何获得首选语言的完整列表?我看到UWP API可能可以完成这项工作,但如果可能的话,我想使用C API,因为它可以更轻松地与手头的C ++代码库集成。(即非托管C ++)
GlobalizationPreferences.Languages可从非托管 C++ 中使用,因为GlobalizationPreferences具有DualApiPartitionAttribute. 这是使用的 C++/WinRT 示例GlobalizationPreferences.Languages:
#pragma once
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.System.UserProfile.h>
#include <iostream>
#pragma comment(lib, "windowsapp")
using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::System::UserProfile;
int main()
{
winrt::init_apartment();
for (const auto& lang : GlobalizationPreferences::Languages()) {
std::wcout << lang.c_str() << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
对于那些无法迁移到 C++ 17 的人,还有一个 WRL 示例:
#include <roapi.h>
#include <wrl.h>
#include <Windows.System.UserProfile.h>
#include <iostream>
#include <stdint.h>
#pragma comment(lib, "runtimeobject.lib")
using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;
using namespace ABI::Windows::Foundation::Collections;
using namespace ABI::Windows::System::UserProfile;
int main()
{
RoInitializeWrapper initialize(RO_INIT_MULTITHREADED);
if (FAILED(initialize)) {
std::cerr << "RoInitialize failed" << std::endl;
return 1;
}
ComPtr<IGlobalizationPreferencesStatics> gps;
HRESULT hr = RoGetActivationFactory(
HStringReference(
RuntimeClass_Windows_System_UserProfile_GlobalizationPreferences)
.Get(),
IID_PPV_ARGS(&gps));
if (FAILED(hr)) {
std::cerr << "RoGetActivationFactory failed" << std::endl;
return 1;
}
ComPtr<IVectorView<HSTRING>> langs;
hr = gps->get_Languages(&langs);
if (FAILED(hr)) {
std::cerr << "Could not get Languages" << std::endl;
return 1;
}
uint32_t size;
hr = langs->get_Size(&size);
if (FAILED(hr)) {
std::cerr << "Could not get Size" << std::endl;
return 1;
}
for (uint32_t i = 0; i < size; ++i) {
HString lang;
hr = langs->GetAt(i, lang.GetAddressOf());
if (FAILED(hr)) {
std::cerr << "Could not get Languages[" << i << "]" << std::endl;
continue;
}
std::wcout << lang.GetRawBuffer(nullptr) << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
280 次 |
| 最近记录: |