Skip to content

Commit 2ee788b

Browse files
Impelement dangling win32api to get language name
1 parent 78d9818 commit 2ee788b

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

src/Windows/Locale.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <QString>
2+
#include <QVector>
3+
#include <QByteArray>
4+
#include <windows.h>
5+
6+
#include "../StringExt.hpp"
7+
#include "Locale.hpp"
8+
9+
namespace Windows
10+
{
11+
/// @brief Returns full locale name by the given partial name (e.g. "en" -> "en-US")
12+
QString Locale::GetFullLocaleName(const QString& partial)
13+
{
14+
std::wstring wpartial = partial.toStdWString();
15+
WCHAR name[LOCALE_NAME_MAX_LENGTH] = {};
16+
17+
// Try as-is first
18+
if (GetLocaleInfoEx(wpartial.c_str(), LOCALE_SNAME, name, LOCALE_NAME_MAX_LENGTH))
19+
return partial;
20+
21+
// Collect all locales
22+
std::vector<std::wstring> locales;
23+
EnumSystemLocalesEx([](LPWSTR lpLocaleString, DWORD, LPARAM lParam) -> BOOL {
24+
auto* locales = reinterpret_cast<std::vector<std::wstring>*>(lParam);
25+
locales->emplace_back(lpLocaleString);
26+
return TRUE;
27+
}, LOCALE_ALL, reinterpret_cast<LPARAM>(&locales), nullptr);
28+
29+
// Find one that starts with the partial name
30+
for (const auto& loc : locales) {
31+
if (_wcsnicmp(loc.c_str(), wpartial.c_str(), wpartial.size()) == 0)
32+
return QString::fromStdWString(loc);
33+
}
34+
35+
return StringExt::EmptyString;
36+
}
37+
38+
/// @brief Returns English language name (e.g. "en-US" -> "English")
39+
QString Locale::LanguageName(const QString& partial)
40+
{
41+
QString locale = GetFullLocaleName(partial);
42+
43+
if (locale.isEmpty())
44+
return StringExt::EmptyString;
45+
46+
std::wstring wlocale = locale.toStdWString();
47+
WCHAR buffer[100] = {};
48+
49+
if (GetLocaleInfoEx(wlocale.c_str(), LOCALE_SENGLISHLANGUAGENAME,
50+
buffer, static_cast<int>(std::size(buffer))))
51+
{
52+
return QString::fromWCharArray(buffer);
53+
}
54+
55+
return StringExt::EmptyString;
56+
}
57+
}

src/Windows/Locale.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
#include <QString>
3+
4+
namespace Windows
5+
{
6+
class Locale final
7+
{
8+
public:
9+
static QString GetFullLocaleName(const QString& partial);
10+
static QString LanguageName(const QString& part);
11+
};
12+
}

0 commit comments

Comments
 (0)