|
| 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 | +} |
0 commit comments