/* this must be first: */
 #include "postgres.h"
+
 #include "fmgr.h"
 #include "utils/builtins.h"
 #include "utils/bytea.h"       /* for byteain & byteaout */
 
-/* Defined by Perl */
-#undef _
-
 /* perl stuff */
 #define PG_NEED_PERL_XSUB_H
 #include "plperl.h"
 
 #undef vprintf
 #undef printf
 
+/*
+ * Perl scribbles on the "_" macro too.
+ */
+#undef _
+
 /*
  * ActivePerl 5.18 and later are MinGW-built, and their headers use GCC's
  * __inline__.  Translate to something MSVC recognizes. Also, perl.h sometimes
 #define vprintf                        pg_vprintf
 #define printf(...)            pg_printf(__VA_ARGS__)
 
+/*
+ * Put back "_" too; but rather than making it just gettext() as the core
+ * code does, make it dgettext() so that the right things will happen in
+ * loadable modules (if they've set up TEXTDOMAIN correctly).  Note that
+ * we can't just set TEXTDOMAIN here, because this file is used by more
+ * extensions than just PL/Perl itself.
+ */
+#undef _
+#define _(x) dgettext(TEXTDOMAIN, x)
+
 /* put back the definition of isnan if needed */
 #ifdef _MSC_VER
 #ifndef isnan