cmp: improve LC_MESSAGES test
authorPaul Eggert <[email protected]>
Tue, 9 Sep 2025 16:16:16 +0000 (9 09:16 -0700)
committerPaul Eggert <[email protected]>
Tue, 9 Sep 2025 17:23:01 +0000 (9 10:23 -0700)
* src/cmp.c (hard_locale_LC_MESSAGES): Use setlocale, not gettext,
to decide whether the messages might not be those of the C or
POSIX locale.  This is a more reliable way to test whether
the locale is something like en_US.utf8, a locale that does
not have a translation catalog but is not the C locale.

src/cmp.c

index 0c4c80c..b7bb204 100644 (file)
--- a/src/cmp.c
+++ b/src/cmp.c
@@ -45,12 +45,15 @@ static char const PROGRAM_NAME[] = "cmp";
   proper_name_lite ("Torbjorn Granlund", "Torbj\303\266rn Granlund"), \
   _("David MacKenzie")
 
+/* Return true if the locale's messages might not be those of C or POSIX.  */
 static bool
 hard_locale_LC_MESSAGES (void)
 {
-#if defined LC_MESSAGES && ENABLE_NLS
-  static char const copyright_string[] = "(C)";
-  return gettext (copyright_string) != copyright_string;
+#if ENABLE_NLS
+  /* GNU diff defines ENABLE_NLS only if gettext is preinstalled, and
+     on these platforms setlocale (LC_MESSAGES, nullptr) never returns nullptr
+     and always returns "C" when in the C or POSIX locales.  */
+  return !STREQ (setlocale (LC_MESSAGES, nullptr), "C");
 #else
   return false;
 #endif