On 5/13/21 6:13 PM, Jakub Jelinek wrote:
On Thu, May 13, 2021 at 06:01:39PM -0400, Jason Merrill via Gcc wrote:
I understand that the difference between the _ macro and the N_ macro is
that the former is used to force a gettext call on the argument and the
latter is used for strings for which gettext will be called later.  But I
don't see any documentation about why/when we should use the G_ macro
instead of one of the other two.  It seems to be used for diagnostic
messages, for which gettext will be called in the diagnostic machinery; why
use G_ instead of N_ in such cases?

See gcc/po/exgettext for details.
The two macros are:
# define N_(msgid) msgid
# define G_(gmsgid) gmsgid
and for exgettext the prefixes of the arguments determine the the kind
of format string:
     if (args ~ /g$/)
         format="gcc-internal-format"
     else if (args ~ /noc$/)
         format="no-c-format"
     else if (args ~ /c$/)
         format="c-format"
So, G_("...") results in the string being collected as
#, gcc-internal-format
while with N_("...") it is
", c-format
gettext has support for gcc-internal-format (though I wonder when that has
been updated last time) and for gcc diagnostic format specifiers will make
sure the translations handle that correctly.

Ah, I looked at exgettext but hadn't read it closely enough to understand that bit. So does this look good to you?
>From 8718fbbf83978bd8ec4bf0a0e4164459158df182 Mon Sep 17 00:00:00 2001
From: Jason Merrill <ja...@redhat.com>
Date: Thu, 13 May 2021 20:53:50 -0400
Subject: [PATCH] intl: add comments to _, N_, and G_
To: gcc-patc...@gcc.gnu.org

gcc/ChangeLog:

	* intl.h: Add comments.
---
 gcc/intl.h | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/gcc/intl.h b/gcc/intl.h
index 829364ae905..f21ec11ada0 100644
--- a/gcc/intl.h
+++ b/gcc/intl.h
@@ -47,14 +47,19 @@ extern const char *fake_ngettext (const char *singular, const char *plural,
 
 #endif
 
+/* Used to immediately translate the argument.  */
 #ifndef _
 # define _(msgid) gettext (msgid)
 #endif
 
+/* Used to mark strings that will be translated later.  */
 #ifndef N_
 # define N_(msgid) msgid
 #endif
 
+/* Like N_, but used for diagnostic strings, to tell gettext that this is a
+   gcc-internal-format string rather than standard c-format.  exgettext gets
+   this from the 'g' in "gmsgid", which is also used in diagnostic.c.  */
 #ifndef G_
 # define G_(gmsgid) gmsgid
 #endif
-- 
2.27.0

Reply via email to