]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
Add pgettext variants that accept expressions.
authorBruno Haible <bruno@clisp.org>
Mon, 3 Apr 2006 11:36:51 +0000 (11:36 +0000)
committerBruno Haible <bruno@clisp.org>
Tue, 23 Jun 2009 10:13:07 +0000 (12:13 +0200)
gettext-tools/doc/ChangeLog
gettext-tools/doc/gettext.texi
gettext-tools/lib/ChangeLog
gettext-tools/lib/gettext.h

index a6432b1bafbc525dc98f12da746f166713264678..ca5dce84c6ce2753c8b4464351aaa30ef5885a3b 100644 (file)
@@ -1,3 +1,8 @@
+2006-04-02  Bruno Haible  <bruno@clisp.org>
+
+       * gettext.texi (Contexts): Document the macros pgettext_expr,
+       dpgettext_expr, dcpgettext_expr.
+
 2006-04-02  Bruno Haible  <bruno@clisp.org>
 
        * gettext.texi (Plural Forms): More details about the expression
index fb8ed36ba984be2c8c29db24344be65d7be6bfb9..28b1b1019b2a6fb0f4df51013653de1ca6fee84f 100644 (file)
@@ -5036,6 +5036,26 @@ pgettext ("Menu|Printer|", "Connect")
 Whether or not to use the @samp{|} character at the end of the context is a
 matter of style.
 
+For more complex cases, where the @var{msgctxt} or @var{msgid} are not
+string literals, more general macros are available:
+
+@findex pgettext_expr
+@findex dpgettext_expr
+@findex dcpgettext_expr
+@example
+const char *pgettext_expr (const char *msgctxt, const char *msgid);
+const char *dpgettext_expr (const char *domain_name,
+                            const char *msgctxt, const char *msgid);
+const char *dcpgettext_expr (const char *domain_name,
+                             const char *msgctxt, const char *msgid,
+                             int category);
+@end example
+
+Here @var{msgctxt} and @var{msgid} can be arbitrary string-valued expressions.
+These macros are more general.  But in the case that both argument expressions
+are string literals, the macros without the @samp{_expr} suffix are more
+efficient.
+
 @node Plural forms, Optimized gettext, Contexts, gettext
 @subsection Additional functions for plural forms
 @cindex plural forms
index 4b85c79c7a3dbecedfc5e6d34f1868f702f32785..dc23ac1074c72aeef4ba3e6bc995b972e7dfe371 100644 (file)
@@ -1,3 +1,10 @@
+2006-04-02  Bruno Haible  <bruno@clisp.org>
+
+       * gettext.h (_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS, pgettext_expr,
+       dpgettext_expr, npgettext_expr, dnpgettext_expr): New macros.
+       (dcpgettext_expr, dcnpgettext_expr): New inline functions.
+       Reported by Chusslove Illich <caslav.ilic@gmx.net>.
+
 2006-04-02  Bruno Haible  <bruno@clisp.org>
 
        Support for non-ASCII author names.
index 9f4b123c364ed34863caf4abceda6a6fb080558c..830f2fcf6962a06dfcb7c12bc95e5a8497577e36 100644 (file)
@@ -1,5 +1,5 @@
 /* Convenience header for conditional use of GNU <libintl.h>.
-   Copyright (C) 1995-1998, 2000-2002, 2004-2005 Free Software Foundation, Inc.
+   Copyright (C) 1995-1998, 2000-2002, 2004-2006 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published
@@ -136,4 +136,104 @@ npgettext_aux (const char *domain,
     return translation;
 }
 
+/* The same thing extended for non-constant arguments.  Here MSGCTXT and MSGID
+   can be arbitrary expressions.  But for string literals these macros are
+   less efficient than those above.  */
+
+#include <string.h>
+
+#define _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS \
+  __GNUC__ >= 3 || defined __cplusplus
+
+#define pgettext_expr(Msgctxt, Msgid) \
+  dcpgettext_expr (NULL, Msgctxt, Msgid, LC_MESSAGES)
+#define dpgettext_expr(Domainname, Msgctxt, Msgid) \
+  dcpgettext_expr (Domainname, Msgctxt, Msgid, LC_MESSAGES)
+
+#ifdef __GNUC__
+__inline
+#else
+#ifdef __cplusplus
+inline
+#endif
+#endif
+static const char *
+dcpgettext_expr (const char *domain,
+                const char *msgctxt, const char *msgid,
+                int category)
+{
+  size_t msgctxt_len = strlen (msgctxt) + 1;
+  size_t msgid_len = strlen (msgid) + 1;
+  const char *translation;
+#if _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
+  char msg_ctxt_id[msgctxt_len + msgid_len];
+#else
+  char buf[1024];
+  char *msg_ctxt_id =
+    (msgctxt_len + msgid_len <= sizeof (buf)
+     ? buf
+     : (char *) malloc (msgctxt_len + msgid_len));
+  if (msg_ctxt_id != NULL)
+#endif
+    {
+      memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1);
+      msg_ctxt_id[msgctxt_len - 1] = '\004';
+      memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len);
+      translation = dcgettext (domain, msg_ctxt_id, category);
+#if !(_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS)
+      if (msg_ctxt_id != buf)
+       free (msg_ctxt_id);
+#endif
+      if (translation != msg_ctxt_id)
+       return translation;
+    }
+  return msgid;
+}
+
+#define npgettext_expr(Msgctxt, Msgid, MsgidPlural, N) \
+  dcnpgettext_expr (NULL, Msgctxt, Msgid, MsgidPlural, N, LC_MESSAGES)
+#define dnpgettext_expr(Domainname, Msgctxt, Msgid, MsgidPlural, N) \
+  dcnpgettext_expr (Domainname, Msgctxt, Msgid, MsgidPlural, N, LC_MESSAGES)
+
+#ifdef __GNUC__
+__inline
+#else
+#ifdef __cplusplus
+inline
+#endif
+#endif
+static const char *
+dcnpgettext_expr (const char *domain,
+                 const char *msgctxt, const char *msgid,
+                 const char *msgid_plural, unsigned long int n,
+                 int category)
+{
+  size_t msgctxt_len = strlen (msgctxt) + 1;
+  size_t msgid_len = strlen (msgid) + 1;
+  const char *translation;
+#if _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
+  char msg_ctxt_id[msgctxt_len + msgid_len];
+#else
+  char buf[1024];
+  char *msg_ctxt_id =
+    (msgctxt_len + msgid_len <= sizeof (buf)
+     ? buf
+     : (char *) malloc (msgctxt_len + msgid_len));
+  if (msg_ctxt_id != NULL)
+#endif
+    {
+      memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1);
+      msg_ctxt_id[msgctxt_len - 1] = '\004';
+      memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len);
+      translation = dcngettext (domain, msg_ctxt_id, msgid_plural, n, category);
+#if !(_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS)
+      if (msg_ctxt_id != buf)
+       free (msg_ctxt_id);
+#endif
+      if (!(translation == msg_ctxt_id || translation == msgid_plural))
+       return translation;
+    }
+  return (n == 1 ? msgid : msgid_plural);
+}
+
 #endif /* _LIBGETTEXT_H */