git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2] i18n: add stub Q_() wrapper for ngettext

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Mar 10, 2011, 03:17 UTC
Message-ID
<20110310031734.GA24781@elie>
In-Reply-To
<7v7hc89fp7.fsf@alter.siamese.dyndns.org>
From: Junio C Hamano <gitster@pobox.com>
Date: Sun, 6 Mar 2011 14:40:05 -0800
Subject: i18n: add stub Q_() wrapper for ngettext

The Q_ function translates a string representing some pharse with an alternative plural form and uses the 'count' argument to choose which form to return. Use of Q_ solves the "%d noun(s)" problem in a way that is portable to languages outside the Germanic and Romance families.

In English, the semantics of Q_(sing, plur, count) are roughly equivalent to

	count == 1 ? _(sing) : _(plur)

while in other languages there can be more variants (count == 0; more random-looking rules based on the historical pronunciation of the number). Behind the scenes, the singular form is used to look up a family of translations and the plural form is ignored unless no translation is available.

Define such a Q_ in gettext.h with the English semantics so C code can start using it to mark phrases with a count for translation.

The name "Q_" is taken from subversion and stands for "quantity". Many projects just use ngettext directly without a wrapper analogous to _; we should not do so because git's gettext.h is meant not to conflict with system headers that might include libintl.h.

Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
Junio C Hamano wrote:
> Yeah, I am aware of that.  Is there a similar convention for [dn]gettext?
> Perhaps not....

subversion uses Q_. Though it does not seem to be standard --- e.g., glib uses:

 _() for gettext
 Q_() for a variant on gettext that allows a string of the form
  "context|message" as its argument;
 C_() for a nicer version of Q_() that takes the context and message
  as distinct arguments;
 N_() to mark a string for translation without translating it;
 NC_() to mark a string with context for translation without
  translating it.
I suppose Q_ is as good a name as any.

Hopefully veterans from glib would not be used to glib's alternative meaning of Q_, preferring to use C_ for messages with flags.

 gettext.h |   12 ++++++++++--
 1 files changed, 10 insertions(+), 2 deletions(-)
diff --git a/gettext.h b/gettext.h
index 04b5958..1b253b7 100644
--- a/gettext.h
+++ b/gettext.h
@@ -9,8 +9,8 @@
 #ifndef GETTEXT_H
 #define GETTEXT_H
 
-#ifdef _
-#error "namespace conflict: '_' is pre-defined?"
+#if defined(_) || defined(Q_)
+#error "namespace conflict: '_' or 'Q_' is pre-defined?"
 #endif
 
 #define FORMAT_PRESERVING(n) __attribute__((format_arg(n)))
@@ -26,6 +26,14 @@ static inline FORMAT_PRESERVING(1) const char *_(const char *msgid)
 	return use_gettext_poison() ? "# GETTEXT POISON #" : msgid;
 }
 
+static inline FORMAT_PRESERVING(1) FORMAT_PRESERVING(2)
+const char *Q_(const char *msgid, const char *plu, unsigned long n)
+{
+	if (use_gettext_poison())
+		return "# GETTEXT POISON #";
+	return n == 1 ? msgid : plu;
+}
+
 /* Mark msgid for translation but do not translate it. */
 #define N_(msgid) (msgid)
 
-- 
1.7.4.1
Previous: Junio C HamanoNext: Junio C Hamano
Message 31 of 34 in “[BUG] git-am silently applying patches incorrectly”
  1. Colin GuthrieMar 4, 2011
  2. Drew NorthupMar 4, 2011
  3. Colin GuthrieMar 4, 2011
  4. Junio C HamanoMar 4, 2011
  5. Junio C HamanoMar 4, 2011
  6. Junio C HamanoMar 4, 2011
  7. Junio C HamanoMar 4, 2011
  8. Linus TorvaldsMar 4, 2011
  9. Junio C HamanoMar 4, 2011
  10. Alexander MiselerMar 4, 2011
  11. Junio C HamanoMar 4, 2011
  12. Colin GuthrieMar 4, 2011
  13. Junio C HamanoMar 4, 2011
  14. Junio C HamanoMar 4, 2011
  15. Colin GuthrieMar 5, 2011
  16. Junio C HamanoMar 6, 2011
  17. Junio C HamanoMar 6, 2011
  18. Jonathan NiederMar 6, 2011
  19. Junio C HamanoMar 6, 2011
  20. Colin GuthrieMar 7, 2011
  21. Alexander MiselerMar 4, 2011
  22. Junio C HamanoMar 5, 2011
  23. Junio C HamanoMar 4, 2011
  24. Drew NorthupMar 4, 2011
  25. 0/2 i18n: add ngettext stubJonathan Nieder, Mar 9, 2011
  26. 1/2 i18n: add stub ngettext implementationJonathan Nieder, Mar 9, 2011
  27. 2/2 i18n: avoid conflict with ngettext from libintlJonathan Nieder, Mar 9, 2011
  28. Junio C HamanoMar 9, 2011
  29. Jonathan NiederMar 9, 2011
  30. Junio C HamanoMar 9, 2011
  31. i18n: add stub Q_() wrapper for ngettextJonathan Nieder, Mar 10, 2011
  32. Junio C HamanoMar 10, 2011
  33. Ævar Arnfjörð BjarmasonMar 10, 2011
  34. Ævar Arnfjörð BjarmasonMar 10, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.