]> cygwin.com Git - cygwin-apps/setup.git/commitdiff
Don't localize mbox strings written to log
authorJon Turney <jon.turney@dronecode.org.uk>
Thu, 10 Sep 2020 16:31:56 +0000 (17:31 +0100)
committerJon Turney <jon.turney@dronecode.org.uk>
Sun, 19 Dec 2021 13:53:10 +0000 (13:53 +0000)
Add LoadStringWEx to access string resource for a specifc locale.
Use that in mbox when formatting string for log.

msg.cc
win32.cc
win32.h

diff --git a/msg.cc b/msg.cc
index 5badcc3c7c825a4af437e6550a4f8628bbd3c55b..00eaf5aad8e4eeb2d451bfe4dd118a780b0e6bb1 100644 (file)
--- a/msg.cc
+++ b/msg.cc
@@ -129,7 +129,7 @@ LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam) {
 int
 mbox(HWND owner, unsigned int format_id, int mb_type, ...)
 {
-  std::wstring fmt = LoadStringW(format_id);
+  std::wstring fmt = LoadStringWEx(format_id, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US));
   if (fmt.empty())
     fmt = L"Internal error: format string resource not found";
 
@@ -138,12 +138,20 @@ mbox(HWND owner, unsigned int format_id, int mb_type, ...)
   std::wstring buf = vformat(fmt, args);
   va_end(args);
 
-  // write to log as UTF8
+  // write unlocalized to log as UTF8
   Log (LOG_PLAIN) << "mbox " << ": " << wstring_to_string(buf) << endLog;
 
   if (unattended_mode)
     return unattended_result(mb_type);
 
+  fmt = LoadStringW(format_id);
+  if (fmt.empty())
+    fmt = L"Internal error: format string resource not found";
+
+  va_start(args, mb_type);
+  buf = vformat(fmt, args);
+  va_end(args);
+
   bool retry_continue = (mb_type & MB_TYPEMASK) == MB_RETRYCONTINUE;
   if (retry_continue) {
     mb_type &= ~MB_TYPEMASK;
index b6e7c947fb3afbfeff7adbe3de266011aefa51a5..be4a9acd72d147a245069da282f0818499b87219 100644 (file)
--- a/win32.cc
+++ b/win32.cc
@@ -462,6 +462,38 @@ WowNativeMachine ()
 #endif
 }
 
+const std::wstring
+LoadStringWEx(UINT uID, UINT langId)
+{
+  HINSTANCE hInstance = GetModuleHandle(NULL);
+
+  // Convert the string ID into a bundle number
+  LPCSTR bundle = MAKEINTRESOURCE(uID / 16 + 1);
+  HRSRC hRes = ::FindResourceEx(hInstance, RT_STRING, bundle, langId);
+  if (hRes)
+    {
+      HGLOBAL h = ::LoadResource(hInstance, hRes);
+      if (h)
+        {
+          HGLOBAL hGlob = ::LockResource(h);
+
+          // walk string bundle
+          wchar_t *buf = (wchar_t *)hGlob;
+          for (unsigned int i = 0; i < (uID & 15); i++)
+            {
+              buf += 1 + (UINT)*buf;
+            }
+
+          int len = *buf;
+          return std::wstring(buf + 1, len);
+        }
+    }
+  // N.B.: Due to the way string bundles are encoded, there's no difference
+  // between an absent string resource whose bundle is present, and a string
+  // resource containing the null string.
+  return L"";
+}
+
 const std::wstring
 LoadStringW(unsigned int uID)
 {
diff --git a/win32.h b/win32.h
index 05f931c8018875b9240552c19eb18e0ea4ad1804..dcaa25118848dc381da649898d6e000182a0100d 100644 (file)
--- a/win32.h
+++ b/win32.h
@@ -195,6 +195,8 @@ SetDlgItemRect (HWND h, int item, LPRECT r)
 }
 
 const std::wstring LoadStringW(unsigned int uID);
+const std::wstring LoadStringWEx(UINT uID, UINT langId);
+
 bool is_developer_mode(void);
 
 #endif /* SETUP_WIN32_H */
This page took 0.038174 seconds and 5 git commands to generate.