gettext.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* Convenience header for conditional use of GNU <libintl.h>.
  2. Copyright (C) 1995-1998, 2000-2002, 2004-2006, 2009-2010 Free Software
  3. Foundation, Inc.
  4. 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 2, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with this program; if not, write to the Free Software Foundation,
  14. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  15. #ifndef _LIBGETTEXT_H
  16. #define _LIBGETTEXT_H 1
  17. /* NLS can be disabled through the configure --disable-nls option. */
  18. #if ENABLE_NLS
  19. /* Get declarations of GNU message catalog functions. */
  20. # include <libintl.h>
  21. /* You can set the DEFAULT_TEXT_DOMAIN macro to specify the domain used by
  22. the gettext() and ngettext() macros. This is an alternative to calling
  23. textdomain(), and is useful for libraries. */
  24. # ifdef DEFAULT_TEXT_DOMAIN
  25. # undef gettext
  26. # define gettext(Msgid) \
  27. dgettext (DEFAULT_TEXT_DOMAIN, Msgid)
  28. # undef ngettext
  29. # define ngettext(Msgid1, Msgid2, N) \
  30. dngettext (DEFAULT_TEXT_DOMAIN, Msgid1, Msgid2, N)
  31. # endif
  32. #else
  33. /* Solaris /usr/include/locale.h includes /usr/include/libintl.h, which
  34. chokes if dcgettext is defined as a macro. So include it now, to make
  35. later inclusions of <locale.h> a NOP. We don't include <libintl.h>
  36. as well because people using "gettext.h" will not include <libintl.h>,
  37. and also including <libintl.h> would fail on SunOS 4, whereas <locale.h>
  38. is OK. */
  39. #if defined(__sun)
  40. # include <locale.h>
  41. #endif
  42. /* Many header files from the libstdc++ coming with g++ 3.3 or newer include
  43. <libintl.h>, which chokes if dcgettext is defined as a macro. So include
  44. it now, to make later inclusions of <libintl.h> a NOP. */
  45. #if defined(__cplusplus) && defined(__GNUG__) && (__GNUC__ >= 3)
  46. # include <cstdlib>
  47. # if (__GLIBC__ >= 2) || _GLIBCXX_HAVE_LIBINTL_H
  48. # include <libintl.h>
  49. # endif
  50. #endif
  51. /* Disabled NLS.
  52. The casts to 'const char *' serve the purpose of producing warnings
  53. for invalid uses of the value returned from these functions.
  54. On pre-ANSI systems without 'const', the config.h file is supposed to
  55. contain "#define const". */
  56. # undef gettext
  57. # define gettext(Msgid) ((const char *) (Msgid))
  58. # undef dgettext
  59. # define dgettext(Domainname, Msgid) ((void) (Domainname), gettext (Msgid))
  60. # undef dcgettext
  61. # define dcgettext(Domainname, Msgid, Category) \
  62. ((void) (Category), dgettext (Domainname, Msgid))
  63. # undef ngettext
  64. # define ngettext(Msgid1, Msgid2, N) \
  65. ((N) == 1 \
  66. ? ((void) (Msgid2), (const char *) (Msgid1)) \
  67. : ((void) (Msgid1), (const char *) (Msgid2)))
  68. # undef dngettext
  69. # define dngettext(Domainname, Msgid1, Msgid2, N) \
  70. ((void) (Domainname), ngettext (Msgid1, Msgid2, N))
  71. # undef dcngettext
  72. # define dcngettext(Domainname, Msgid1, Msgid2, N, Category) \
  73. ((void) (Category), dngettext (Domainname, Msgid1, Msgid2, N))
  74. # undef textdomain
  75. # define textdomain(Domainname) ((const char *) (Domainname))
  76. # undef bindtextdomain
  77. # define bindtextdomain(Domainname, Dirname) \
  78. ((void) (Domainname), (const char *) (Dirname))
  79. # undef bind_textdomain_codeset
  80. # define bind_textdomain_codeset(Domainname, Codeset) \
  81. ((void) (Domainname), (const char *) (Codeset))
  82. #endif
  83. /* A pseudo function call that serves as a marker for the automated
  84. extraction of messages, but does not call gettext(). The run-time
  85. translation is done at a different place in the code.
  86. The argument, String, should be a literal string. Concatenated strings
  87. and other string expressions won't work.
  88. The macro's expansion is not parenthesized, so that it is suitable as
  89. initializer for static 'char[]' or 'const char[]' variables. */
  90. #define gettext_noop(String) String
  91. /* The separator between msgctxt and msgid in a .mo file. */
  92. #define GETTEXT_CONTEXT_GLUE "\004"
  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. #ifdef __GNUC__
  120. __inline
  121. #else
  122. #ifdef __cplusplus
  123. inline
  124. #endif
  125. #endif
  126. static const char *
  127. pgettext_aux (const char *domain,
  128. const char *msg_ctxt_id, const char *msgid,
  129. int category)
  130. {
  131. const char *translation = dcgettext (domain, msg_ctxt_id, category);
  132. if (translation == msg_ctxt_id)
  133. return msgid;
  134. else
  135. return translation;
  136. }
  137. #ifdef __GNUC__
  138. __inline
  139. #else
  140. #ifdef __cplusplus
  141. inline
  142. #endif
  143. #endif
  144. static const char *
  145. npgettext_aux (const char *domain,
  146. const char *msg_ctxt_id, const char *msgid,
  147. const char *msgid_plural, unsigned long int n,
  148. int category)
  149. {
  150. const char *translation =
  151. dcngettext (domain, msg_ctxt_id, msgid_plural, n, category);
  152. if (translation == msg_ctxt_id || translation == msgid_plural)
  153. return (n == 1 ? msgid : msgid_plural);
  154. else
  155. return translation;
  156. }
  157. /* The same thing extended for non-constant arguments. Here MSGCTXT and MSGID
  158. can be arbitrary expressions. But for string literals these macros are
  159. less efficient than those above. */
  160. #include <string.h>
  161. #define _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS \
  162. (((__GNUC__ >= 3 || __GNUG__ >= 2) && !__STRICT_ANSI__) \
  163. /* || __STDC_VERSION__ >= 199901L */ )
  164. #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
  165. #include <stdlib.h>
  166. #endif
  167. #define pgettext_expr(Msgctxt, Msgid) \
  168. dcpgettext_expr (NULL, Msgctxt, Msgid, LC_MESSAGES)
  169. #define dpgettext_expr(Domainname, Msgctxt, Msgid) \
  170. dcpgettext_expr (Domainname, Msgctxt, Msgid, LC_MESSAGES)
  171. #ifdef __GNUC__
  172. __inline
  173. #else
  174. #ifdef __cplusplus
  175. inline
  176. #endif
  177. #endif
  178. static const char *
  179. dcpgettext_expr (const char *domain,
  180. const char *msgctxt, const char *msgid,
  181. int category)
  182. {
  183. size_t msgctxt_len = strlen (msgctxt) + 1;
  184. size_t msgid_len = strlen (msgid) + 1;
  185. const char *translation;
  186. #if _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
  187. char msg_ctxt_id[msgctxt_len + msgid_len];
  188. #else
  189. char buf[1024];
  190. char *msg_ctxt_id =
  191. (msgctxt_len + msgid_len <= sizeof (buf)
  192. ? buf
  193. : (char *) malloc (msgctxt_len + msgid_len));
  194. if (msg_ctxt_id != NULL)
  195. #endif
  196. {
  197. memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1);
  198. msg_ctxt_id[msgctxt_len - 1] = '\004';
  199. memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len);
  200. translation = dcgettext (domain, msg_ctxt_id, category);
  201. #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
  202. if (msg_ctxt_id != buf)
  203. free (msg_ctxt_id);
  204. #endif
  205. if (translation != msg_ctxt_id)
  206. return translation;
  207. }
  208. return msgid;
  209. }
  210. #define npgettext_expr(Msgctxt, Msgid, MsgidPlural, N) \
  211. dcnpgettext_expr (NULL, Msgctxt, Msgid, MsgidPlural, N, LC_MESSAGES)
  212. #define dnpgettext_expr(Domainname, Msgctxt, Msgid, MsgidPlural, N) \
  213. dcnpgettext_expr (Domainname, Msgctxt, Msgid, MsgidPlural, N, LC_MESSAGES)
  214. #ifdef __GNUC__
  215. __inline
  216. #else
  217. #ifdef __cplusplus
  218. inline
  219. #endif
  220. #endif
  221. static const char *
  222. dcnpgettext_expr (const char *domain,
  223. const char *msgctxt, const char *msgid,
  224. const char *msgid_plural, unsigned long int n,
  225. int category)
  226. {
  227. size_t msgctxt_len = strlen (msgctxt) + 1;
  228. size_t msgid_len = strlen (msgid) + 1;
  229. const char *translation;
  230. #if _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
  231. char msg_ctxt_id[msgctxt_len + msgid_len];
  232. #else
  233. char buf[1024];
  234. char *msg_ctxt_id =
  235. (msgctxt_len + msgid_len <= sizeof (buf)
  236. ? buf
  237. : (char *) malloc (msgctxt_len + msgid_len));
  238. if (msg_ctxt_id != NULL)
  239. #endif
  240. {
  241. memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1);
  242. msg_ctxt_id[msgctxt_len - 1] = '\004';
  243. memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len);
  244. translation = dcngettext (domain, msg_ctxt_id, msgid_plural, n, category);
  245. #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
  246. if (msg_ctxt_id != buf)
  247. free (msg_ctxt_id);
  248. #endif
  249. if (!(translation == msg_ctxt_id || translation == msgid_plural))
  250. return translation;
  251. }
  252. return (n == 1 ? msgid : msgid_plural);
  253. }
  254. #endif /* _LIBGETTEXT_H */