xref: /freebsd/contrib/ntp/sntp/libopts/gettext.h (revision 2b15cb3d0922bd70ea592f0da9b4a5b167f4d53f)
1*2b15cb3dSCy Schubert /* Convenience header for conditional use of GNU <libintl.h>.
2*2b15cb3dSCy Schubert    Copyright (C) 1995-1998, 2000-2002, 2004-2006, 2009-2014 Free Software
3*2b15cb3dSCy Schubert    Foundation, Inc.
4*2b15cb3dSCy Schubert 
5*2b15cb3dSCy Schubert    This program is free software; you can redistribute it and/or modify
6*2b15cb3dSCy Schubert    it under the terms of the GNU Lesser General Public License as published by
7*2b15cb3dSCy Schubert    the Free Software Foundation; either version 2.1, or (at your option)
8*2b15cb3dSCy Schubert    any later version.
9*2b15cb3dSCy Schubert 
10*2b15cb3dSCy Schubert    This program is distributed in the hope that it will be useful,
11*2b15cb3dSCy Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
12*2b15cb3dSCy Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*2b15cb3dSCy Schubert    GNU Lesser General Public License for more details.
14*2b15cb3dSCy Schubert 
15*2b15cb3dSCy Schubert    You should have received a copy of the GNU Lesser General Public License along
16*2b15cb3dSCy Schubert    with this program; if not, see <http://www.gnu.org/licenses/>.  */
17*2b15cb3dSCy Schubert 
18*2b15cb3dSCy Schubert #ifndef _LIBGETTEXT_H
19*2b15cb3dSCy Schubert #define _LIBGETTEXT_H 1
20*2b15cb3dSCy Schubert 
21*2b15cb3dSCy Schubert /* NLS can be disabled through the configure --disable-nls option.  */
22*2b15cb3dSCy Schubert #if ENABLE_NLS
23*2b15cb3dSCy Schubert 
24*2b15cb3dSCy Schubert /* Get declarations of GNU message catalog functions.  */
25*2b15cb3dSCy Schubert # include <libintl.h>
26*2b15cb3dSCy Schubert 
27*2b15cb3dSCy Schubert /* You can set the DEFAULT_TEXT_DOMAIN macro to specify the domain used by
28*2b15cb3dSCy Schubert    the gettext() and ngettext() macros.  This is an alternative to calling
29*2b15cb3dSCy Schubert    textdomain(), and is useful for libraries.  */
30*2b15cb3dSCy Schubert # ifdef DEFAULT_TEXT_DOMAIN
31*2b15cb3dSCy Schubert #  undef gettext
32*2b15cb3dSCy Schubert #  define gettext(Msgid) \
33*2b15cb3dSCy Schubert      dgettext (DEFAULT_TEXT_DOMAIN, Msgid)
34*2b15cb3dSCy Schubert #  undef ngettext
35*2b15cb3dSCy Schubert #  define ngettext(Msgid1, Msgid2, N) \
36*2b15cb3dSCy Schubert      dngettext (DEFAULT_TEXT_DOMAIN, Msgid1, Msgid2, N)
37*2b15cb3dSCy Schubert # endif
38*2b15cb3dSCy Schubert 
39*2b15cb3dSCy Schubert #else
40*2b15cb3dSCy Schubert 
41*2b15cb3dSCy Schubert /* Solaris /usr/include/locale.h includes /usr/include/libintl.h, which
42*2b15cb3dSCy Schubert    chokes if dcgettext is defined as a macro.  So include it now, to make
43*2b15cb3dSCy Schubert    later inclusions of <locale.h> a NOP.  We don't include <libintl.h>
44*2b15cb3dSCy Schubert    as well because people using "gettext.h" will not include <libintl.h>,
45*2b15cb3dSCy Schubert    and also including <libintl.h> would fail on SunOS 4, whereas <locale.h>
46*2b15cb3dSCy Schubert    is OK.  */
47*2b15cb3dSCy Schubert #if defined(__sun)
48*2b15cb3dSCy Schubert # include <locale.h>
49*2b15cb3dSCy Schubert #endif
50*2b15cb3dSCy Schubert 
51*2b15cb3dSCy Schubert /* Many header files from the libstdc++ coming with g++ 3.3 or newer include
52*2b15cb3dSCy Schubert    <libintl.h>, which chokes if dcgettext is defined as a macro.  So include
53*2b15cb3dSCy Schubert    it now, to make later inclusions of <libintl.h> a NOP.  */
54*2b15cb3dSCy Schubert #if defined(__cplusplus) && defined(__GNUG__) && (__GNUC__ >= 3)
55*2b15cb3dSCy Schubert # include <cstdlib>
56*2b15cb3dSCy Schubert # if (__GLIBC__ >= 2 && !defined __UCLIBC__) || _GLIBCXX_HAVE_LIBINTL_H
57*2b15cb3dSCy Schubert #  include <libintl.h>
58*2b15cb3dSCy Schubert # endif
59*2b15cb3dSCy Schubert #endif
60*2b15cb3dSCy Schubert 
61*2b15cb3dSCy Schubert /* Disabled NLS.
62*2b15cb3dSCy Schubert    The casts to 'const char *' serve the purpose of producing warnings
63*2b15cb3dSCy Schubert    for invalid uses of the value returned from these functions.
64*2b15cb3dSCy Schubert    On pre-ANSI systems without 'const', the config.h file is supposed to
65*2b15cb3dSCy Schubert    contain "#define const".  */
66*2b15cb3dSCy Schubert # undef gettext
67*2b15cb3dSCy Schubert # define gettext(Msgid) ((const char *) (Msgid))
68*2b15cb3dSCy Schubert # undef dgettext
69*2b15cb3dSCy Schubert # define dgettext(Domainname, Msgid) ((void) (Domainname), gettext (Msgid))
70*2b15cb3dSCy Schubert # undef dcgettext
71*2b15cb3dSCy Schubert # define dcgettext(Domainname, Msgid, Category) \
72*2b15cb3dSCy Schubert     ((void) (Category), dgettext (Domainname, Msgid))
73*2b15cb3dSCy Schubert # undef ngettext
74*2b15cb3dSCy Schubert # define ngettext(Msgid1, Msgid2, N) \
75*2b15cb3dSCy Schubert     ((N) == 1 \
76*2b15cb3dSCy Schubert      ? ((void) (Msgid2), (const char *) (Msgid1)) \
77*2b15cb3dSCy Schubert      : ((void) (Msgid1), (const char *) (Msgid2)))
78*2b15cb3dSCy Schubert # undef dngettext
79*2b15cb3dSCy Schubert # define dngettext(Domainname, Msgid1, Msgid2, N) \
80*2b15cb3dSCy Schubert     ((void) (Domainname), ngettext (Msgid1, Msgid2, N))
81*2b15cb3dSCy Schubert # undef dcngettext
82*2b15cb3dSCy Schubert # define dcngettext(Domainname, Msgid1, Msgid2, N, Category) \
83*2b15cb3dSCy Schubert     ((void) (Category), dngettext (Domainname, Msgid1, Msgid2, N))
84*2b15cb3dSCy Schubert # undef textdomain
85*2b15cb3dSCy Schubert # define textdomain(Domainname) ((const char *) (Domainname))
86*2b15cb3dSCy Schubert # undef bindtextdomain
87*2b15cb3dSCy Schubert # define bindtextdomain(Domainname, Dirname) \
88*2b15cb3dSCy Schubert     ((void) (Domainname), (const char *) (Dirname))
89*2b15cb3dSCy Schubert # undef bind_textdomain_codeset
90*2b15cb3dSCy Schubert # define bind_textdomain_codeset(Domainname, Codeset) \
91*2b15cb3dSCy Schubert     ((void) (Domainname), (const char *) (Codeset))
92*2b15cb3dSCy Schubert 
93*2b15cb3dSCy Schubert #endif
94*2b15cb3dSCy Schubert 
95*2b15cb3dSCy Schubert /* Prefer gnulib's setlocale override over libintl's setlocale override.  */
96*2b15cb3dSCy Schubert #ifdef GNULIB_defined_setlocale
97*2b15cb3dSCy Schubert # undef setlocale
98*2b15cb3dSCy Schubert # define setlocale rpl_setlocale
99*2b15cb3dSCy Schubert #endif
100*2b15cb3dSCy Schubert 
101*2b15cb3dSCy Schubert /* A pseudo function call that serves as a marker for the automated
102*2b15cb3dSCy Schubert    extraction of messages, but does not call gettext().  The run-time
103*2b15cb3dSCy Schubert    translation is done at a different place in the code.
104*2b15cb3dSCy Schubert    The argument, String, should be a literal string.  Concatenated strings
105*2b15cb3dSCy Schubert    and other string expressions won't work.
106*2b15cb3dSCy Schubert    The macro's expansion is not parenthesized, so that it is suitable as
107*2b15cb3dSCy Schubert    initializer for static 'char[]' or 'const char[]' variables.  */
108*2b15cb3dSCy Schubert #define gettext_noop(String) String
109*2b15cb3dSCy Schubert 
110*2b15cb3dSCy Schubert /* The separator between msgctxt and msgid in a .mo file.  */
111*2b15cb3dSCy Schubert #define GETTEXT_CONTEXT_GLUE "\004"
112*2b15cb3dSCy Schubert 
113*2b15cb3dSCy Schubert /* Pseudo function calls, taking a MSGCTXT and a MSGID instead of just a
114*2b15cb3dSCy Schubert    MSGID.  MSGCTXT and MSGID must be string literals.  MSGCTXT should be
115*2b15cb3dSCy Schubert    short and rarely need to change.
116*2b15cb3dSCy Schubert    The letter 'p' stands for 'particular' or 'special'.  */
117*2b15cb3dSCy Schubert #ifdef DEFAULT_TEXT_DOMAIN
118*2b15cb3dSCy Schubert # define pgettext(Msgctxt, Msgid) \
119*2b15cb3dSCy Schubert    pgettext_aux (DEFAULT_TEXT_DOMAIN, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)
120*2b15cb3dSCy Schubert #else
121*2b15cb3dSCy Schubert # define pgettext(Msgctxt, Msgid) \
122*2b15cb3dSCy Schubert    pgettext_aux (NULL, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)
123*2b15cb3dSCy Schubert #endif
124*2b15cb3dSCy Schubert #define dpgettext(Domainname, Msgctxt, Msgid) \
125*2b15cb3dSCy Schubert   pgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)
126*2b15cb3dSCy Schubert #define dcpgettext(Domainname, Msgctxt, Msgid, Category) \
127*2b15cb3dSCy Schubert   pgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, Category)
128*2b15cb3dSCy Schubert #ifdef DEFAULT_TEXT_DOMAIN
129*2b15cb3dSCy Schubert # define npgettext(Msgctxt, Msgid, MsgidPlural, N) \
130*2b15cb3dSCy Schubert    npgettext_aux (DEFAULT_TEXT_DOMAIN, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES)
131*2b15cb3dSCy Schubert #else
132*2b15cb3dSCy Schubert # define npgettext(Msgctxt, Msgid, MsgidPlural, N) \
133*2b15cb3dSCy Schubert    npgettext_aux (NULL, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES)
134*2b15cb3dSCy Schubert #endif
135*2b15cb3dSCy Schubert #define dnpgettext(Domainname, Msgctxt, Msgid, MsgidPlural, N) \
136*2b15cb3dSCy Schubert   npgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES)
137*2b15cb3dSCy Schubert #define dcnpgettext(Domainname, Msgctxt, Msgid, MsgidPlural, N, Category) \
138*2b15cb3dSCy Schubert   npgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, Category)
139*2b15cb3dSCy Schubert 
140*2b15cb3dSCy Schubert #ifdef __GNUC__
141*2b15cb3dSCy Schubert __inline
142*2b15cb3dSCy Schubert #else
143*2b15cb3dSCy Schubert #ifdef __cplusplus
144*2b15cb3dSCy Schubert inline
145*2b15cb3dSCy Schubert #endif
146*2b15cb3dSCy Schubert #endif
147*2b15cb3dSCy Schubert static const char *
148*2b15cb3dSCy Schubert pgettext_aux (const char *domain,
149*2b15cb3dSCy Schubert               const char *msg_ctxt_id, const char *msgid,
150*2b15cb3dSCy Schubert               int category)
151*2b15cb3dSCy Schubert {
152*2b15cb3dSCy Schubert   const char *translation = dcgettext (domain, msg_ctxt_id, category);
153*2b15cb3dSCy Schubert   if (translation == msg_ctxt_id)
154*2b15cb3dSCy Schubert     return msgid;
155*2b15cb3dSCy Schubert   else
156*2b15cb3dSCy Schubert     return translation;
157*2b15cb3dSCy Schubert }
158*2b15cb3dSCy Schubert 
159*2b15cb3dSCy Schubert #ifdef __GNUC__
160*2b15cb3dSCy Schubert __inline
161*2b15cb3dSCy Schubert #else
162*2b15cb3dSCy Schubert #ifdef __cplusplus
163*2b15cb3dSCy Schubert inline
164*2b15cb3dSCy Schubert #endif
165*2b15cb3dSCy Schubert #endif
166*2b15cb3dSCy Schubert static const char *
167*2b15cb3dSCy Schubert npgettext_aux (const char *domain,
168*2b15cb3dSCy Schubert                const char *msg_ctxt_id, const char *msgid,
169*2b15cb3dSCy Schubert                const char *msgid_plural, unsigned long int n,
170*2b15cb3dSCy Schubert                int category)
171*2b15cb3dSCy Schubert {
172*2b15cb3dSCy Schubert   const char *translation =
173*2b15cb3dSCy Schubert     dcngettext (domain, msg_ctxt_id, msgid_plural, n, category);
174*2b15cb3dSCy Schubert   if (translation == msg_ctxt_id || translation == msgid_plural)
175*2b15cb3dSCy Schubert     return (n == 1 ? msgid : msgid_plural);
176*2b15cb3dSCy Schubert   else
177*2b15cb3dSCy Schubert     return translation;
178*2b15cb3dSCy Schubert }
179*2b15cb3dSCy Schubert 
180*2b15cb3dSCy Schubert /* The same thing extended for non-constant arguments.  Here MSGCTXT and MSGID
181*2b15cb3dSCy Schubert    can be arbitrary expressions.  But for string literals these macros are
182*2b15cb3dSCy Schubert    less efficient than those above.  */
183*2b15cb3dSCy Schubert 
184*2b15cb3dSCy Schubert #include <string.h>
185*2b15cb3dSCy Schubert 
186*2b15cb3dSCy Schubert #if (((__GNUC__ >= 3 || __GNUG__ >= 2) && !defined __STRICT_ANSI__) \
187*2b15cb3dSCy Schubert      /* || __STDC_VERSION__ >= 199901L */ )
188*2b15cb3dSCy Schubert # define _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS 1
189*2b15cb3dSCy Schubert #else
190*2b15cb3dSCy Schubert # define _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS 0
191*2b15cb3dSCy Schubert #endif
192*2b15cb3dSCy Schubert 
193*2b15cb3dSCy Schubert #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
194*2b15cb3dSCy Schubert #include <stdlib.h>
195*2b15cb3dSCy Schubert #endif
196*2b15cb3dSCy Schubert 
197*2b15cb3dSCy Schubert #define pgettext_expr(Msgctxt, Msgid) \
198*2b15cb3dSCy Schubert   dcpgettext_expr (NULL, Msgctxt, Msgid, LC_MESSAGES)
199*2b15cb3dSCy Schubert #define dpgettext_expr(Domainname, Msgctxt, Msgid) \
200*2b15cb3dSCy Schubert   dcpgettext_expr (Domainname, Msgctxt, Msgid, LC_MESSAGES)
201*2b15cb3dSCy Schubert 
202*2b15cb3dSCy Schubert #ifdef __GNUC__
203*2b15cb3dSCy Schubert __inline
204*2b15cb3dSCy Schubert #else
205*2b15cb3dSCy Schubert #ifdef __cplusplus
206*2b15cb3dSCy Schubert inline
207*2b15cb3dSCy Schubert #endif
208*2b15cb3dSCy Schubert #endif
209*2b15cb3dSCy Schubert static const char *
210*2b15cb3dSCy Schubert dcpgettext_expr (const char *domain,
211*2b15cb3dSCy Schubert                  const char *msgctxt, const char *msgid,
212*2b15cb3dSCy Schubert                  int category)
213*2b15cb3dSCy Schubert {
214*2b15cb3dSCy Schubert   size_t msgctxt_len = strlen (msgctxt) + 1;
215*2b15cb3dSCy Schubert   size_t msgid_len = strlen (msgid) + 1;
216*2b15cb3dSCy Schubert   const char *translation;
217*2b15cb3dSCy Schubert #if _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
218*2b15cb3dSCy Schubert   char msg_ctxt_id[msgctxt_len + msgid_len];
219*2b15cb3dSCy Schubert #else
220*2b15cb3dSCy Schubert   char buf[1024];
221*2b15cb3dSCy Schubert   char *msg_ctxt_id =
222*2b15cb3dSCy Schubert     (msgctxt_len + msgid_len <= sizeof (buf)
223*2b15cb3dSCy Schubert      ? buf
224*2b15cb3dSCy Schubert      : (char *) malloc (msgctxt_len + msgid_len));
225*2b15cb3dSCy Schubert   if (msg_ctxt_id != NULL)
226*2b15cb3dSCy Schubert #endif
227*2b15cb3dSCy Schubert     {
228*2b15cb3dSCy Schubert       memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1);
229*2b15cb3dSCy Schubert       msg_ctxt_id[msgctxt_len - 1] = '\004';
230*2b15cb3dSCy Schubert       memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len);
231*2b15cb3dSCy Schubert       translation = dcgettext (domain, msg_ctxt_id, category);
232*2b15cb3dSCy Schubert #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
233*2b15cb3dSCy Schubert       if (msg_ctxt_id != buf)
234*2b15cb3dSCy Schubert         free (msg_ctxt_id);
235*2b15cb3dSCy Schubert #endif
236*2b15cb3dSCy Schubert       if (translation != msg_ctxt_id)
237*2b15cb3dSCy Schubert         return translation;
238*2b15cb3dSCy Schubert     }
239*2b15cb3dSCy Schubert   return msgid;
240*2b15cb3dSCy Schubert }
241*2b15cb3dSCy Schubert 
242*2b15cb3dSCy Schubert #define npgettext_expr(Msgctxt, Msgid, MsgidPlural, N) \
243*2b15cb3dSCy Schubert   dcnpgettext_expr (NULL, Msgctxt, Msgid, MsgidPlural, N, LC_MESSAGES)
244*2b15cb3dSCy Schubert #define dnpgettext_expr(Domainname, Msgctxt, Msgid, MsgidPlural, N) \
245*2b15cb3dSCy Schubert   dcnpgettext_expr (Domainname, Msgctxt, Msgid, MsgidPlural, N, LC_MESSAGES)
246*2b15cb3dSCy Schubert 
247*2b15cb3dSCy Schubert #ifdef __GNUC__
248*2b15cb3dSCy Schubert __inline
249*2b15cb3dSCy Schubert #else
250*2b15cb3dSCy Schubert #ifdef __cplusplus
251*2b15cb3dSCy Schubert inline
252*2b15cb3dSCy Schubert #endif
253*2b15cb3dSCy Schubert #endif
254*2b15cb3dSCy Schubert static const char *
255*2b15cb3dSCy Schubert dcnpgettext_expr (const char *domain,
256*2b15cb3dSCy Schubert                   const char *msgctxt, const char *msgid,
257*2b15cb3dSCy Schubert                   const char *msgid_plural, unsigned long int n,
258*2b15cb3dSCy Schubert                   int category)
259*2b15cb3dSCy Schubert {
260*2b15cb3dSCy Schubert   size_t msgctxt_len = strlen (msgctxt) + 1;
261*2b15cb3dSCy Schubert   size_t msgid_len = strlen (msgid) + 1;
262*2b15cb3dSCy Schubert   const char *translation;
263*2b15cb3dSCy Schubert #if _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
264*2b15cb3dSCy Schubert   char msg_ctxt_id[msgctxt_len + msgid_len];
265*2b15cb3dSCy Schubert #else
266*2b15cb3dSCy Schubert   char buf[1024];
267*2b15cb3dSCy Schubert   char *msg_ctxt_id =
268*2b15cb3dSCy Schubert     (msgctxt_len + msgid_len <= sizeof (buf)
269*2b15cb3dSCy Schubert      ? buf
270*2b15cb3dSCy Schubert      : (char *) malloc (msgctxt_len + msgid_len));
271*2b15cb3dSCy Schubert   if (msg_ctxt_id != NULL)
272*2b15cb3dSCy Schubert #endif
273*2b15cb3dSCy Schubert     {
274*2b15cb3dSCy Schubert       memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1);
275*2b15cb3dSCy Schubert       msg_ctxt_id[msgctxt_len - 1] = '\004';
276*2b15cb3dSCy Schubert       memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len);
277*2b15cb3dSCy Schubert       translation = dcngettext (domain, msg_ctxt_id, msgid_plural, n, category);
278*2b15cb3dSCy Schubert #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
279*2b15cb3dSCy Schubert       if (msg_ctxt_id != buf)
280*2b15cb3dSCy Schubert         free (msg_ctxt_id);
281*2b15cb3dSCy Schubert #endif
282*2b15cb3dSCy Schubert       if (!(translation == msg_ctxt_id || translation == msgid_plural))
283*2b15cb3dSCy Schubert         return translation;
284*2b15cb3dSCy Schubert     }
285*2b15cb3dSCy Schubert   return (n == 1 ? msgid : msgid_plural);
286*2b15cb3dSCy Schubert }
287*2b15cb3dSCy Schubert 
288*2b15cb3dSCy Schubert #endif /* _LIBGETTEXT_H */
289