@gjsify/node-gi 0.64.0 → 0.64.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/private.cc CHANGED
@@ -25,6 +25,10 @@
25
25
 
26
26
  #include "common.h"
27
27
 
28
+ #ifdef _WIN32
29
+ #include <windows.h>
30
+ #endif
31
+
28
32
  namespace nodegi {
29
33
 
30
34
  // ---- GLib.log_set_writer_func ----------------------------------------------
@@ -557,10 +561,39 @@ Napi::Value Bindtextdomain(const Napi::CallbackInfo& info) {
557
561
  }
558
562
  location = info[1].As<Napi::String>().Utf8Value();
559
563
  }
564
+ #ifdef _WIN32
565
+ // bindtextdomain() reads its path in the ANSI code page, so a UTF-8 JS string with
566
+ // a non-ASCII directory (".../Steuererklärung/share/locale") binds a path that
567
+ // does not exist and every lookup silently answers the msgid (measured in a Windows
568
+ // 11 VM: the same catalog under an ASCII path translated). GNU gettext >= 0.21
569
+ // ships wbindtextdomain() for exactly this; both directions go through UTF-16.
570
+ std::wstring wlocation;
571
+ if (!query) {
572
+ int n = MultiByteToWideChar(CP_UTF8, 0, location.c_str(), -1, nullptr, 0);
573
+ if (n <= 0) {
574
+ Napi::TypeError::New(env, "bindtextdomain: location is not valid UTF-8")
575
+ .ThrowAsJavaScriptException();
576
+ return env.Null();
577
+ }
578
+ wlocation.resize(n);
579
+ MultiByteToWideChar(CP_UTF8, 0, location.c_str(), -1, wlocation.data(), n);
580
+ wlocation.resize(n - 1);
581
+ }
582
+ const wchar_t* wresult = wbindtextdomain(domain.c_str(), query ? nullptr : wlocation.c_str());
583
+ if (!query) bind_textdomain_codeset(domain.c_str(), "UTF-8");
584
+ if (wresult == nullptr) return env.Null();
585
+ int len = WideCharToMultiByte(CP_UTF8, 0, wresult, -1, nullptr, 0, nullptr, nullptr);
586
+ if (len <= 0) return env.Null();
587
+ std::string utf8(len, '\0');
588
+ WideCharToMultiByte(CP_UTF8, 0, wresult, -1, utf8.data(), len, nullptr, nullptr);
589
+ utf8.resize(len - 1);
590
+ return Napi::String::New(env, utf8);
591
+ #else
560
592
  const char* result = bindtextdomain(domain.c_str(), query ? nullptr : location.c_str());
561
593
  if (!query) bind_textdomain_codeset(domain.c_str(), "UTF-8");
562
594
  if (result == nullptr) return env.Null();
563
595
  return Napi::String::New(env, result);
596
+ #endif
564
597
  }
565
598
 
566
599
  // localeCategories() -> { ALL, COLLATE, CTYPE, MESSAGES, MONETARY, NUMERIC, TIME }