gettext.h

00001 /* Convenience header for conditional use of GNU <libintl.h>.
00002    Copyright (C) 1995-1998, 2000-2002, 2004-2006 Free Software Foundation, Inc.
00003 
00004    This program is free software; you can redistribute it and/or modify it
00005    under the terms of the GNU General Public License as published
00006    by the Free Software Foundation; either version 2, or (at your option)
00007    any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU General Public
00015    License along with this program; if not, write to the Free Software
00016    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00017    USA.  */
00018 
00019 #ifndef _LIBGETTEXT_H
00020 #define _LIBGETTEXT_H 1
00021 
00022 /* NLS can be disabled through the configure --disable-nls option.  */
00023 #if ENABLE_NLS
00024 
00025 /* Get declarations of GNU message catalog functions.  */
00026 # include <libintl.h>
00027 
00028 /* You can set the DEFAULT_TEXT_DOMAIN macro to specify the domain used by
00029    the gettext() and ngettext() macros.  This is an alternative to calling
00030    textdomain(), and is useful for libraries.  */
00031 # ifdef DEFAULT_TEXT_DOMAIN
00032 #  undef gettext
00033 #  define gettext(Msgid) \
00034      dgettext (DEFAULT_TEXT_DOMAIN, Msgid)
00035 #  undef ngettext
00036 #  define ngettext(Msgid1, Msgid2, N) \
00037      dngettext (DEFAULT_TEXT_DOMAIN, Msgid1, Msgid2, N)
00038 # endif
00039 
00040 #else
00041 
00042 /* Solaris /usr/include/locale.h includes /usr/include/libintl.h, which
00043    chokes if dcgettext is defined as a macro.  So include it now, to make
00044    later inclusions of <locale.h> a NOP.  We don't include <libintl.h>
00045    as well because people using "gettext.h" will not include <libintl.h>,
00046    and also including <libintl.h> would fail on SunOS 4, whereas <locale.h>
00047    is OK.  */
00048 #if defined(__sun)
00049 # include <locale.h>
00050 #endif
00051 
00052 /* Many header files from the libstdc++ coming with g++ 3.3 or newer include
00053    <libintl.h>, which chokes if dcgettext is defined as a macro.  So include
00054    it now, to make later inclusions of <libintl.h> a NOP.  */
00055 #if defined(__cplusplus) && defined(__GNUG__) && (__GNUC__ >= 3)
00056 # include <cstdlib>
00057 # if (__GLIBC__ >= 2) || _GLIBCXX_HAVE_LIBINTL_H
00058 #  include <libintl.h>
00059 # endif
00060 #endif
00061 
00062 /* Disabled NLS.
00063    The casts to 'const char *' serve the purpose of producing warnings
00064    for invalid uses of the value returned from these functions.
00065    On pre-ANSI systems without 'const', the config.h file is supposed to
00066    contain "#define const".  */
00067 # define gettext(Msgid) ((const char *) (Msgid))
00068 # define dgettext(Domainname, Msgid) ((void) (Domainname), gettext (Msgid))
00069 # define dcgettext(Domainname, Msgid, Category) \
00070     ((void) (Category), dgettext (Domainname, Msgid))
00071 # define ngettext(Msgid1, Msgid2, N) \
00072     ((N) == 1 \
00073      ? ((void) (Msgid2), (const char *) (Msgid1)) \
00074      : ((void) (Msgid1), (const char *) (Msgid2)))
00075 # define dngettext(Domainname, Msgid1, Msgid2, N) \
00076     ((void) (Domainname), ngettext (Msgid1, Msgid2, N))
00077 # define dcngettext(Domainname, Msgid1, Msgid2, N, Category) \
00078     ((void) (Category), dngettext(Domainname, Msgid1, Msgid2, N))
00079 # define textdomain(Domainname) ((const char *) (Domainname))
00080 # define bindtextdomain(Domainname, Dirname) \
00081     ((void) (Domainname), (const char *) (Dirname))
00082 # define bind_textdomain_codeset(Domainname, Codeset) \
00083     ((void) (Domainname), (const char *) (Codeset))
00084 
00085 #endif
00086 
00087 /* A pseudo function call that serves as a marker for the automated
00088    extraction of messages, but does not call gettext().  The run-time
00089    translation is done at a different place in the code.
00090    The argument, String, should be a literal string.  Concatenated strings
00091    and other string expressions won't work.
00092    The macro's expansion is not parenthesized, so that it is suitable as
00093    initializer for static 'char[]' or 'const char[]' variables.  */
00094 #define gettext_noop(String) String
00095 
00096 /* The separator between msgctxt and msgid in a .mo file.  */
00097 #define GETTEXT_CONTEXT_GLUE "\004"
00098 
00099 /* Pseudo function calls, taking a MSGCTXT and a MSGID instead of just a
00100    MSGID.  MSGCTXT and MSGID must be string literals.  MSGCTXT should be
00101    short and rarely need to change.
00102    The letter 'p' stands for 'particular' or 'special'.  */
00103 #ifdef DEFAULT_TEXT_DOMAIN
00104 # define pgettext(Msgctxt, Msgid) \
00105    pgettext_aux (DEFAULT_TEXT_DOMAIN, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)
00106 #else
00107 # define pgettext(Msgctxt, Msgid) \
00108    pgettext_aux (NULL, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)
00109 #endif
00110 #define dpgettext(Domainname, Msgctxt, Msgid) \
00111   pgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)
00112 #define dcpgettext(Domainname, Msgctxt, Msgid, Category) \
00113   pgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, Category)
00114 #ifdef DEFAULT_TEXT_DOMAIN
00115 # define npgettext(Msgctxt, Msgid, MsgidPlural, N) \
00116    npgettext_aux (DEFAULT_TEXT_DOMAIN, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES)
00117 #else
00118 # define npgettext(Msgctxt, Msgid, MsgidPlural, N) \
00119    npgettext_aux (NULL, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES)
00120 #endif
00121 #define dnpgettext(Domainname, Msgctxt, Msgid, MsgidPlural, N) \
00122   npgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES)
00123 #define dcnpgettext(Domainname, Msgctxt, Msgid, MsgidPlural, N, Category) \
00124   npgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, Category)
00125 
00126 #ifdef __GNUC__
00127 __inline
00128 #else
00129 #ifdef __cplusplus
00130 inline
00131 #endif
00132 #endif
00133 static const char *
00134 pgettext_aux (const char *domain,
00135               const char *msg_ctxt_id, const char *msgid,
00136               int category)
00137 {
00138   const char *translation = dcgettext (domain, msg_ctxt_id, category);
00139   if (translation == msg_ctxt_id)
00140     return msgid;
00141   else
00142     return translation;
00143 }
00144 
00145 #ifdef __GNUC__
00146 __inline
00147 #else
00148 #ifdef __cplusplus
00149 inline
00150 #endif
00151 #endif
00152 static const char *
00153 npgettext_aux (const char *domain,
00154                const char *msg_ctxt_id, const char *msgid,
00155                const char *msgid_plural, unsigned long int n,
00156                int category)
00157 {
00158   const char *translation =
00159     dcngettext (domain, msg_ctxt_id, msgid_plural, n, category);
00160   if (translation == msg_ctxt_id || translation == msgid_plural)
00161     return (n == 1 ? msgid : msgid_plural);
00162   else
00163     return translation;
00164 }
00165 
00166 /* The same thing extended for non-constant arguments.  Here MSGCTXT and MSGID
00167    can be arbitrary expressions.  But for string literals these macros are
00168    less efficient than those above.  */
00169 
00170 #include <string.h>
00171 
00172 #define _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS \
00173   (((__GNUC__ >= 3 || __GNUG__ >= 2) && !__STRICT_ANSI__) \
00174    /* || __STDC_VERSION__ >= 199901L */ )
00175 
00176 #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
00177 #include <stdlib.h>
00178 #endif
00179 
00180 #define pgettext_expr(Msgctxt, Msgid) \
00181   dcpgettext_expr (NULL, Msgctxt, Msgid, LC_MESSAGES)
00182 #define dpgettext_expr(Domainname, Msgctxt, Msgid) \
00183   dcpgettext_expr (Domainname, Msgctxt, Msgid, LC_MESSAGES)
00184 
00185 #ifdef __GNUC__
00186 __inline
00187 #else
00188 #ifdef __cplusplus
00189 inline
00190 #endif
00191 #endif
00192 static const char *
00193 dcpgettext_expr (const char *domain,
00194                  const char *msgctxt, const char *msgid,
00195                  int category)
00196 {
00197   size_t msgctxt_len = strlen (msgctxt) + 1;
00198   size_t msgid_len = strlen (msgid) + 1;
00199   const char *translation;
00200 #if _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
00201   char msg_ctxt_id[msgctxt_len + msgid_len];
00202 #else
00203   char buf[1024];
00204   char *msg_ctxt_id =
00205     (msgctxt_len + msgid_len <= sizeof (buf)
00206      ? buf
00207      : (char *) malloc (msgctxt_len + msgid_len));
00208   if (msg_ctxt_id != NULL)
00209 #endif
00210     {
00211       memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1);
00212       msg_ctxt_id[msgctxt_len - 1] = '\004';
00213       memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len);
00214       translation = dcgettext (domain, msg_ctxt_id, category);
00215 #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
00216       if (msg_ctxt_id != buf)
00217         free (msg_ctxt_id);
00218 #endif
00219       if (translation != msg_ctxt_id)
00220         return translation;
00221     }
00222   return msgid;
00223 }
00224 
00225 #define npgettext_expr(Msgctxt, Msgid, MsgidPlural, N) \
00226   dcnpgettext_expr (NULL, Msgctxt, Msgid, MsgidPlural, N, LC_MESSAGES)
00227 #define dnpgettext_expr(Domainname, Msgctxt, Msgid, MsgidPlural, N) \
00228   dcnpgettext_expr (Domainname, Msgctxt, Msgid, MsgidPlural, N, LC_MESSAGES)
00229 
00230 #ifdef __GNUC__
00231 __inline
00232 #else
00233 #ifdef __cplusplus
00234 inline
00235 #endif
00236 #endif
00237 static const char *
00238 dcnpgettext_expr (const char *domain,
00239                   const char *msgctxt, const char *msgid,
00240                   const char *msgid_plural, unsigned long int n,
00241                   int category)
00242 {
00243   size_t msgctxt_len = strlen (msgctxt) + 1;
00244   size_t msgid_len = strlen (msgid) + 1;
00245   const char *translation;
00246 #if _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
00247   char msg_ctxt_id[msgctxt_len + msgid_len];
00248 #else
00249   char buf[1024];
00250   char *msg_ctxt_id =
00251     (msgctxt_len + msgid_len <= sizeof (buf)
00252      ? buf
00253      : (char *) malloc (msgctxt_len + msgid_len));
00254   if (msg_ctxt_id != NULL)
00255 #endif
00256     {
00257       memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1);
00258       msg_ctxt_id[msgctxt_len - 1] = '\004';
00259       memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len);
00260       translation = dcngettext (domain, msg_ctxt_id, msgid_plural, n, category);
00261 #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
00262       if (msg_ctxt_id != buf)
00263         free (msg_ctxt_id);
00264 #endif
00265       if (!(translation == msg_ctxt_id || translation == msgid_plural))
00266         return translation;
00267     }
00268   return (n == 1 ? msgid : msgid_plural);
00269 }
00270 
00271 #endif /* _LIBGETTEXT_H */