drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
gettext.h
1/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 Convenience header for conditional use of GNU <libintl.h>.
4 Copyright (C) 1995-1998, 2000-2002, 2004-2006 Free Software Foundation, Inc.
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU General Public
17 License along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 USA. */
20
21#pragma once
22
23/* NLS can be disabled through the configure --disable-nls option. */
24#if defined(ENABLE_NLS)
25
26/* Get declarations of GNU message catalog functions. */
27# include <libintl.h>
28
29/* You can set the DEFAULT_TEXT_DOMAIN macro to specify the domain used by
30 the gettext() and ngettext() macros. This is an alternative to calling
31 textdomain(), and is useful for libraries. */
32# ifdef DEFAULT_TEXT_DOMAIN
33# undef gettext
34# define gettext(Msgid) \
35 dgettext (DEFAULT_TEXT_DOMAIN, Msgid)
36# undef ngettext
37# define ngettext(Msgid1, Msgid2, N) \
38 dngettext (DEFAULT_TEXT_DOMAIN, Msgid1, Msgid2, N)
39# endif
40
41#else
42
43/* Many C++ standard library headers include <libintl.h>, which chokes if
44 dcgettext is defined as a macro. So include it now, to make later
45 inclusions of <libintl.h> a NOP. */
46#if defined(__cplusplus)
47# include <cstdlib>
48# if defined(__GLIBC__)
49# if (__GLIBC__ >= 2) || defined(_GLIBCXX_HAVE_LIBINTL_H)
50# include <libintl.h>
51# endif
52# endif
53#endif
54
55#ifndef gettext
56/* Disabled NLS.
57 The casts to 'const char *' serve the purpose of producing warnings
58 for invalid uses of the value returned from these functions.
59 On pre-ANSI systems without 'const', the config.h file is supposed to
60 contain "#define const". */
61# define gettext(Msgid) ((const char *) (Msgid))
62# define dgettext(Domainname, Msgid) ((void) (Domainname), gettext (Msgid))
63# define dcgettext(Domainname, Msgid, Category) \
64 ((void) (Category), dgettext (Domainname, Msgid))
65# define ngettext(Msgid1, Msgid2, N) \
66 ((N) == 1 \
67 ? ((void) (Msgid2), (const char *) (Msgid1)) \
68 : ((void) (Msgid1), (const char *) (Msgid2)))
69# define dngettext(Domainname, Msgid1, Msgid2, N) \
70 ((void) (Domainname), ngettext (Msgid1, Msgid2, N))
71# define dcngettext(Domainname, Msgid1, Msgid2, N, Category) \
72 ((void) (Category), dngettext(Domainname, Msgid1, Msgid2, N))
73# define textdomain(Domainname) ((const char *) (Domainname))
74# define bindtextdomain(Domainname, Dirname) \
75 ((void) (Domainname), (const char *) (Dirname))
76# define bind_textdomain_codeset(Domainname, Codeset) \
77 ((void) (Domainname), (const char *) (Codeset))
78
79#endif
80#endif
81/* A pseudo function call that serves as a marker for the automated
82 extraction of messages, but does not call gettext(). The run-time
83 translation is done at a different place in the code.
84 The argument, String, should be a literal string. Concatenated strings
85 and other string expressions won't work.
86 The macro's expansion is not parenthesized, so that it is suitable as
87 initializer for static 'char[]' or 'const char[]' variables. */
88#define gettext_noop(String) String
89
90/* The separator between msgctxt and msgid in a .mo file. */
91#define GETTEXT_CONTEXT_GLUE "\004"
92
93/* Pseudo function calls, taking a MSGCTXT and a MSGID instead of just a
94 MSGID. MSGCTXT and MSGID must be string literals. MSGCTXT should be
95 short and rarely need to change.
96 The letter 'p' stands for 'particular' or 'special'. */
97#ifdef DEFAULT_TEXT_DOMAIN
98# define pgettext(Msgctxt, Msgid) \
99 pgettext_aux (DEFAULT_TEXT_DOMAIN, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)
100#else
101# define pgettext(Msgctxt, Msgid) \
102 pgettext_aux (NULL, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)
103#endif
104#define dpgettext(Domainname, Msgctxt, Msgid) \
105 pgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)
106#define dcpgettext(Domainname, Msgctxt, Msgid, Category) \
107 pgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, Category)
108#ifdef DEFAULT_TEXT_DOMAIN
109# define npgettext(Msgctxt, Msgid, MsgidPlural, N) \
110 npgettext_aux (DEFAULT_TEXT_DOMAIN, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES)
111#else
112# define npgettext(Msgctxt, Msgid, MsgidPlural, N) \
113 npgettext_aux (NULL, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES)
114#endif
115#define dnpgettext(Domainname, Msgctxt, Msgid, MsgidPlural, N) \
116 npgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES)
117#define dcnpgettext(Domainname, Msgctxt, Msgid, MsgidPlural, N, Category) \
118 npgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, Category)
119
120__inline
121static const char *
122pgettext_aux (const char *domain,
123 const char *msg_ctxt_id, const char *msgid,
124 int category)
125{
126 const char *translation = dcgettext (domain, msg_ctxt_id, category);
127 if (translation == msg_ctxt_id)
128 return msgid;
129 else
130 return translation;
131}
132
133__inline
134static const char *
135npgettext_aux (const char *domain,
136 const char *msg_ctxt_id, const char *msgid,
137 const char *msgid_plural, unsigned long int n,
138 int category)
139{
140 const char *translation =
141 dcngettext (domain, msg_ctxt_id, msgid_plural, n, category);
142 if (translation == msg_ctxt_id || translation == msgid_plural)
143 return (n == 1 ? msgid : msgid_plural);
144 else
145 return translation;
146}
147
148/* The same thing extended for non-constant arguments. Here MSGCTXT and MSGID
149 can be arbitrary expressions. But for string literals these macros are
150 less efficient than those above. */
151
152#include <string.h>
153
154/* We need to turn these off explicitly for drizzle */
155#define _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS 0
156
157#if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
158#include <stdlib.h>
159#endif
160
161#define pgettext_expr(Msgctxt, Msgid) \
162 dcpgettext_expr (NULL, Msgctxt, Msgid, LC_MESSAGES)
163#define dpgettext_expr(Domainname, Msgctxt, Msgid) \
164 dcpgettext_expr (Domainname, Msgctxt, Msgid, LC_MESSAGES)
165
166__inline
167static const char *
168dcpgettext_expr (const char *domain,
169 const char *msgctxt, const char *msgid,
170 int category)
171{
172 size_t msgctxt_len = strlen (msgctxt) + 1;
173 size_t msgid_len = strlen (msgid) + 1;
174 const char *translation;
175#if _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
176 char msg_ctxt_id[msgctxt_len + msgid_len];
177#else
178 char buf[1024];
179 char *msg_ctxt_id =
180 (msgctxt_len + msgid_len <= sizeof (buf)
181 ? buf
182 : (char *) malloc (msgctxt_len + msgid_len));
183 if (msg_ctxt_id != NULL)
184#endif
185 {
186 memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1);
187 msg_ctxt_id[msgctxt_len - 1] = '\004';
188 memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len);
189 translation = dcgettext (domain, msg_ctxt_id, category);
190#if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
191 if (msg_ctxt_id != buf)
192 free (msg_ctxt_id);
193#endif
194 if (translation != msg_ctxt_id)
195 return translation;
196 }
197 return msgid;
198}
199
200#define npgettext_expr(Msgctxt, Msgid, MsgidPlural, N) \
201 dcnpgettext_expr (NULL, Msgctxt, Msgid, MsgidPlural, N, LC_MESSAGES)
202#define dnpgettext_expr(Domainname, Msgctxt, Msgid, MsgidPlural, N) \
203 dcnpgettext_expr (Domainname, Msgctxt, Msgid, MsgidPlural, N, LC_MESSAGES)
204
205__inline
206static const char *
207dcnpgettext_expr (const char *domain,
208 const char *msgctxt, const char *msgid,
209 const char *msgid_plural, unsigned long int n,
210 int category)
211{
212 size_t msgctxt_len = strlen (msgctxt) + 1;
213 size_t msgid_len = strlen (msgid) + 1;
214 const char *translation;
215#if _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
216 char msg_ctxt_id[msgctxt_len + msgid_len];
217#else
218 char buf[1024];
219 char *msg_ctxt_id =
220 (msgctxt_len + msgid_len <= sizeof (buf)
221 ? buf
222 : (char *) malloc (msgctxt_len + msgid_len));
223 if (msg_ctxt_id != NULL)
224#endif
225 {
226 memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1);
227 msg_ctxt_id[msgctxt_len - 1] = '\004';
228 memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len);
229 translation = dcngettext (domain, msg_ctxt_id, msgid_plural, n, category);
230#if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
231 if (msg_ctxt_id != buf)
232 free (msg_ctxt_id);
233#endif
234 if (!(translation == msg_ctxt_id || translation == msgid_plural))
235 return translation;
236 }
237 return (n == 1 ? msgid : msgid_plural);
238}
239
240#define _(String) gettext(String)
241#define N_(String) gettext_noop(String)