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