KDECore
gettext.h
Go to the documentation of this file.
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 Library 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 Library 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) ((const char *) (Msgid)) 00069 # define dcgettext(Domainname, Msgid, Category) ((const char *) (Msgid)) 00070 # define ngettext(Msgid1, Msgid2, N) \ 00071 ((N) == 1 ? (const char *) (Msgid1) : (const char *) (Msgid2)) 00072 # define dngettext(Domainname, Msgid1, Msgid2, N) \ 00073 ((N) == 1 ? (const char *) (Msgid1) : (const char *) (Msgid2)) 00074 # define dcngettext(Domainname, Msgid1, Msgid2, N, Category) \ 00075 ((N) == 1 ? (const char *) (Msgid1) : (const char *) (Msgid2)) 00076 # define textdomain(Domainname) ((const char *) (Domainname)) 00077 # define bindtextdomain(Domainname, Dirname) ((const char *) (Dirname)) 00078 # define bind_textdomain_codeset(Domainname, Codeset) ((const char *) (Codeset)) 00079 00080 #endif 00081 00082 /* A pseudo function call that serves as a marker for the automated 00083 extraction of messages, but does not call gettext(). The run-time 00084 translation is done at a different place in the code. 00085 The argument, String, should be a literal string. Concatenated strings 00086 and other string expressions won't work. 00087 The macro's expansion is not parenthesized, so that it is suitable as 00088 initializer for static 'char[]' or 'const char[]' variables. */ 00089 #define gettext_noop(String) String 00090 00091 /* The separator between msgctxt and msgid in a .mo file. */ 00092 #define GETTEXT_CONTEXT_GLUE "\004" 00093 00094 /* Pseudo function calls, taking a MSGCTXT and a MSGID instead of just a 00095 MSGID. MSGCTXT and MSGID must be string literals. MSGCTXT should be 00096 short and rarely need to change. 00097 The letter 'p' stands for 'particular' or 'special'. */ 00098 #ifdef DEFAULT_TEXT_DOMAIN 00099 # define pgettext(Msgctxt, Msgid) \ 00100 pgettext_aux (DEFAULT_TEXT_DOMAIN, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES) 00101 #else 00102 # define pgettext(Msgctxt, Msgid) \ 00103 pgettext_aux (NULL, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES) 00104 #endif 00105 #define dpgettext(Domainname, Msgctxt, Msgid) \ 00106 pgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES) 00107 #define dcpgettext(Domainname, Msgctxt, Msgid, Category) \ 00108 pgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, Category) 00109 #ifdef DEFAULT_TEXT_DOMAIN 00110 # define npgettext(Msgctxt, Msgid, MsgidPlural, N) \ 00111 npgettext_aux (DEFAULT_TEXT_DOMAIN, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES) 00112 #else 00113 # define npgettext(Msgctxt, Msgid, MsgidPlural, N) \ 00114 npgettext_aux (NULL, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES) 00115 #endif 00116 #define dnpgettext(Domainname, Msgctxt, Msgid, MsgidPlural, N) \ 00117 npgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES) 00118 #define dcnpgettext(Domainname, Msgctxt, Msgid, MsgidPlural, N, Category) \ 00119 npgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, Category) 00120 00121 #ifdef __GNUC__ 00122 __inline 00123 #else 00124 #ifdef __cplusplus 00125 inline 00126 #endif 00127 #endif 00128 static const char * 00129 pgettext_aux (const char *domain, 00130 const char *msg_ctxt_id, const char *msgid, 00131 int category) 00132 { 00133 const char *translation = dcgettext (domain, msg_ctxt_id, category); 00134 if (translation == msg_ctxt_id) 00135 return msgid; 00136 else 00137 return translation; 00138 } 00139 00140 #ifdef __GNUC__ 00141 __inline 00142 #else 00143 #ifdef __cplusplus 00144 inline 00145 #endif 00146 #endif 00147 static const char * 00148 npgettext_aux (const char *domain, 00149 const char *msg_ctxt_id, const char *msgid, 00150 const char *msgid_plural, unsigned long int n, 00151 int category) 00152 { 00153 const char *translation = 00154 dcngettext (domain, msg_ctxt_id, msgid_plural, n, category); 00155 if (translation == msg_ctxt_id || translation == msgid_plural) 00156 return (n == 1 ? msgid : msgid_plural); 00157 else 00158 return translation; 00159 } 00160 00161 /* The same thing extended for non-constant arguments. Here MSGCTXT and MSGID 00162 can be arbitrary expressions. But for string literals these macros are 00163 less efficient than those above. */ 00164 00165 #include <string.h> 00166 00167 #define _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS \ 00168 (__STRICT_ANSI__ - 0 == 0) && (__GNUC__ >= 3 || __GNUG__ >= 2 /* || __STDC_VERSION__ >= 199901L */ ) 00169 00170 #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS 00171 #include <stdlib.h> 00172 #endif 00173 00174 #define pgettext_expr(Msgctxt, Msgid) \ 00175 dcpgettext_expr (NULL, Msgctxt, Msgid, LC_MESSAGES) 00176 #define dpgettext_expr(Domainname, Msgctxt, Msgid) \ 00177 dcpgettext_expr (Domainname, Msgctxt, Msgid, LC_MESSAGES) 00178 00179 #ifdef __GNUC__ 00180 __inline 00181 #else 00182 #ifdef __cplusplus 00183 inline 00184 #endif 00185 #endif 00186 static const char * 00187 dcpgettext_expr (const char *domain, 00188 const char *msgctxt, const char *msgid, 00189 int category) 00190 { 00191 size_t msgctxt_len = strlen (msgctxt) + 1; 00192 size_t msgid_len = strlen (msgid) + 1; 00193 const char *translation; 00194 #if _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS 00195 char msg_ctxt_id[msgctxt_len + msgid_len]; 00196 #else 00197 char buf[1024]; 00198 char *msg_ctxt_id = 00199 (msgctxt_len + msgid_len <= sizeof (buf) 00200 ? buf 00201 : (char *) malloc (msgctxt_len + msgid_len)); 00202 if (msg_ctxt_id != NULL) 00203 #endif 00204 { 00205 memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1); 00206 msg_ctxt_id[msgctxt_len - 1] = '\004'; 00207 memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len); 00208 translation = dcgettext (domain, msg_ctxt_id, category); 00209 #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS 00210 if (msg_ctxt_id != buf) 00211 free (msg_ctxt_id); 00212 #endif 00213 if (translation != msg_ctxt_id) 00214 return translation; 00215 } 00216 return msgid; 00217 } 00218 00219 #define npgettext_expr(Msgctxt, Msgid, MsgidPlural, N) \ 00220 dcnpgettext_expr (NULL, Msgctxt, Msgid, MsgidPlural, N, LC_MESSAGES) 00221 #define dnpgettext_expr(Domainname, Msgctxt, Msgid, MsgidPlural, N) \ 00222 dcnpgettext_expr (Domainname, Msgctxt, Msgid, MsgidPlural, N, LC_MESSAGES) 00223 00224 #ifdef __GNUC__ 00225 __inline 00226 #else 00227 #ifdef __cplusplus 00228 inline 00229 #endif 00230 #endif 00231 static const char * 00232 dcnpgettext_expr (const char *domain, 00233 const char *msgctxt, const char *msgid, 00234 const char *msgid_plural, unsigned long int n, 00235 int category) 00236 { 00237 size_t msgctxt_len = strlen (msgctxt) + 1; 00238 size_t msgid_len = strlen (msgid) + 1; 00239 const char *translation; 00240 #if _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS 00241 char msg_ctxt_id[msgctxt_len + msgid_len]; 00242 #else 00243 char buf[1024]; 00244 char *msg_ctxt_id = 00245 (msgctxt_len + msgid_len <= sizeof (buf) 00246 ? buf 00247 : (char *) malloc (msgctxt_len + msgid_len)); 00248 if (msg_ctxt_id != NULL) 00249 #endif 00250 { 00251 memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1); 00252 msg_ctxt_id[msgctxt_len - 1] = '\004'; 00253 memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len); 00254 translation = dcngettext (domain, msg_ctxt_id, msgid_plural, n, category); 00255 #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS 00256 if (msg_ctxt_id != buf) 00257 free (msg_ctxt_id); 00258 #endif 00259 if (!(translation == msg_ctxt_id || translation == msgid_plural)) 00260 return translation; 00261 } 00262 return (n == 1 ? msgid : msgid_plural); 00263 } 00264 00265 #endif /* _LIBGETTEXT_H */
KDE 4.6 API Reference