From 0c17511d8febf06f4507bf065ae38677a485ab7c Mon Sep 17 00:00:00 2001 From: lx-s Date: Tue, 14 Nov 2023 20:27:26 +0100 Subject: [PATCH] Fall back to english when string is not present. Added utilitys to translate dialog items --- WinMute/WMi18n.cpp | 70 +++++++++++++++++++++++++--------------------- WinMute/WMi18n.h | 7 +++-- 2 files changed, 43 insertions(+), 34 deletions(-) diff --git a/WinMute/WMi18n.cpp b/WinMute/WMi18n.cpp index 89a8439..477f926 100644 --- a/WinMute/WMi18n.cpp +++ b/WinMute/WMi18n.cpp @@ -155,50 +155,56 @@ void WMi18n::UnloadLanguage() noexcept } } -std::wstring WMi18n::GetTextW(UINT strId) const +std::wstring WMi18n::GetTextW(UINT textId) const { HMODULE hMod = (langModule_ == nullptr) ? hglobInstance : langModule_; - constexpr size_t strBufferSize = 2048 + 1; - wchar_t strBuffer[strBufferSize]{ 0 }; - const int strLength = LoadStringW(hMod, strId, strBuffer, 0); + const wchar_t *strBuffer = nullptr; + int strLength = LoadStringW(hMod, textId, reinterpret_cast(&strBuffer), 0); + if (strLength == 0 && langModule_ != nullptr) { + // Try to fall back to standard language + strLength = LoadStringW(hglobInstance, textId, reinterpret_cast(&strBuffer), 0); + } if (strLength == 0) { - return L""; - } else if (strLength < strBufferSize) { - LoadStringW(hMod, strId, strBuffer, ARRAY_SIZE(strBuffer)); - return std::wstring(strBuffer, strBuffer + strLength); - } else if (strLength > 0) { - wchar_t *largeBuffer = new wchar_t[strLength + 1]; - LoadStringW(hMod, strId, largeBuffer, strLength + 1); - std::wstring ret(strBuffer, largeBuffer + strLength); - delete[] largeBuffer; - return ret; - } else { WMLog::GetInstance().WriteWindowsError(L"LoadStringW", GetLastError()); - std::wstring err = L"String resource " + std::to_wstring(strId) + L" not found"; + std::wstring err = L"String resource " + std::to_wstring(textId) + L" not found"; return err; } + return std::wstring(strBuffer, strBuffer + strLength); } -std::string WMi18n::GetTextA(UINT strId) const +std::string WMi18n::GetTextA(UINT textId) const { HMODULE hMod = (langModule_ == nullptr) ? hglobInstance : langModule_; - constexpr size_t strBufferSize = 2048 + 1; - char strBuffer[strBufferSize]{ 0 }; - const int strLength = LoadStringA(hMod, strId, strBuffer, 0); + const char *strBuffer = nullptr; + int strLength = LoadStringA(hMod, textId, reinterpret_cast(&strBuffer), 0); + if (strLength == 0 && langModule_ != nullptr) { + // Try to fall back to standard language + strLength = LoadStringA(hglobInstance, textId, reinterpret_cast(&strBuffer), 0); + } if (strLength == 0) { - return ""; - } else if (strLength < strBufferSize) { - LoadStringA(hMod, strId, strBuffer, ARRAY_SIZE(strBuffer)); - return std::string(strBuffer, strBuffer + strLength); - } else if (strLength > 0) { - char *largeBuffer = new char[strLength + 1]; - LoadStringA(hMod, strId, largeBuffer, strLength + 1); - std::string ret(strBuffer, largeBuffer + strLength); - delete[] largeBuffer; - return ret; - } else { WMLog::GetInstance().WriteWindowsError(L"LoadStringA", GetLastError()); - std::string err = "String resource " + std::to_string(strId) + " not found"; + std::string err = "String resource " + std::to_string(textId) + " not found"; return err; } + return std::string(strBuffer, strBuffer + strLength); +} + +bool WMi18n::SetItemText(HWND hWnd, int dlgItem, UINT textId) +{ + const auto text = GetTextW(textId); + if (!SetDlgItemTextW(hWnd, dlgItem, text.c_str())) { + WMLog::GetInstance().WriteWindowsError(L"SetDlgItemTextW", GetLastError()); + return false; + } + return true; +} + +bool WMi18n::SetItemText(HWND hItem, UINT textId) +{ + const auto text = GetTextW(textId); + if (!SetWindowTextW(hItem, text.c_str())) { + WMLog::GetInstance().WriteWindowsError(L"SetDlgItemTextW", GetLastError()); + return false; + } + return true; } diff --git a/WinMute/WMi18n.h b/WinMute/WMi18n.h index 6e22ce2..8bdd5e4 100644 --- a/WinMute/WMi18n.h +++ b/WinMute/WMi18n.h @@ -51,8 +51,11 @@ public: std::wstring GetCurrentLanguageModule() const; std::wstring GetCurrentLanguageName() const; - std::wstring GetTextW(UINT id) const; - std::string GetTextA(UINT id) const; + std::wstring GetTextW(UINT textId) const; + std::string GetTextA(UINT textId) const; + + bool SetItemText(HWND hWnd, int dlgItem, UINT textId); + bool SetItemText(HWND hItem, UINT textId); private: WMi18n() noexcept; -- 2.43.0