]> repos.lx-s.de Git - winmute.git/commitdiff
Fall back to english when string is not present. Added utilitys to translate dialog...
authorlx-s <lx-s@users.noreply.github.com>
Tue, 14 Nov 2023 19:27:26 +0000 (20:27 +0100)
committerlx-s <lx-s@users.noreply.github.com>
Tue, 14 Nov 2023 19:27:26 +0000 (20:27 +0100)
WinMute/WMi18n.cpp
WinMute/WMi18n.h

index 89a8439ba4c4361bd876f12160a042e6cc0308b0..477f9267b5d8e5636379ff9f6d7f958a192b80ef 100644 (file)
@@ -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<LPWSTR>(&strBuffer), 0);
+   if (strLength == 0 && langModule_ != nullptr) {
+      // Try to fall back to standard language
+      strLength = LoadStringW(hglobInstance, textId, reinterpret_cast<LPWSTR>(&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<LPSTR>(&strBuffer), 0);
+   if (strLength == 0 && langModule_ != nullptr) {
+      // Try to fall back to standard language
+      strLength = LoadStringA(hglobInstance, textId, reinterpret_cast<LPSTR>(&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;
 }
index 6e22ce223aa070815d1c8402fc0af1c28a1178b1..8bdd5e431fd89602a99378fc4f262a8da6f9f99f 100644 (file)
@@ -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;