gettext.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /* Convenience header for conditional use of GNU <libintl.h>.
  2. Copyright (C) 1995-1998, 2000-2002, 2004-2006 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU General Public License as published
  5. by the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU General Public
  12. License along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  14. 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 && !defined __UCLIBC__) || _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. # define gettext(Msgid) ((const char *) (Msgid))
  57. # define dgettext(Domainname, Msgid) ((void) (Domainname), gettext (Msgid))
  58. # define dcgettext(Domainname, Msgid, Category) \
  59. ((void) (Category), dgettext (Domainname, Msgid))
  60. # define ngettext(Msgid1, Msgid2, N) \
  61. ((N) == 1 \
  62. ? ((void) (Msgid2), (const char *) (Msgid1)) \
  63. : ((void) (Msgid1), (const char *) (Msgid2)))
  64. # define dngettext(Domainname, Msgid1, Msgid2, N) \
  65. ((void) (Domainname), ngettext (Msgid1, Msgid2, N))
  66. # define dcngettext(Domainname, Msgid1, Msgid2, N, Category) \
  67. ((void) (Category), dngettext(Domainname, Msgid1, Msgid2, N))
  68. # define textdomain(Domainname) ((const char *) (Domainname))
  69. # define bindtextdomain(Domainname, Dirname) \
  70. ((void) (Domainname), (const char *) (Dirname))
  71. # define bind_textdomain_codeset(Domainname, Codeset) \
  72. ((void) (Domainname), (const char *) (Codeset))
  73. #endif
  74. /* A pseudo function call that serves as a marker for the automated
  75. extraction of messages, but does not call gettext(). The run-time
  76. translation is done at a different place in the code.
  77. The argument, String, should be a literal string. Concatenated strings
  78. and other string expressions won't work.
  79. The macro's expansion is not parenthesized, so that it is suitable as
  80. initializer for static 'char[]' or 'const char[]' variables. */
  81. #define gettext_noop(String) String
  82. /* The separator between msgctxt and msgid in a .mo file. */
  83. #define GETTEXT_CONTEXT_GLUE "\004"
  84. #if ENABLE_NLS
  85. /* Pseudo function calls, taking a MSGCTXT and a MSGID instead of just a
  86. MSGID. MSGCTXT and MSGID must be string literals. MSGCTXT should be
  87. short and rarely need to change.
  88. The letter 'p' stands for 'particular' or 'special'. */
  89. #ifdef DEFAULT_TEXT_DOMAIN
  90. # define pgettext(Msgctxt, Msgid) \
  91. pgettext_aux (DEFAULT_TEXT_DOMAIN, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)
  92. #else
  93. # define pgettext(Msgctxt, Msgid) \
  94. pgettext_aux (NULL, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)
  95. #endif
  96. #define dpgettext(Domainname, Msgctxt, Msgid) \
  97. pgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)
  98. #define dcpgettext(Domainname, Msgctxt, Msgid, Category) \
  99. pgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, Category)
  100. #ifdef DEFAULT_TEXT_DOMAIN
  101. # define npgettext(Msgctxt, Msgid, MsgidPlural, N) \
  102. npgettext_aux (DEFAULT_TEXT_DOMAIN, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES)
  103. #else
  104. # define npgettext(Msgctxt, Msgid, MsgidPlural, N) \
  105. npgettext_aux (NULL, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES)
  106. #endif
  107. #define dnpgettext(Domainname, Msgctxt, Msgid, MsgidPlural, N) \
  108. npgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES)
  109. #define dcnpgettext(Domainname, Msgctxt, Msgid, MsgidPlural, N, Category) \
  110. npgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, Category)
  111. #ifdef __GNUC__
  112. __inline
  113. #else
  114. #ifdef __cplusplus
  115. inline
  116. #endif
  117. #endif
  118. static const char *
  119. pgettext_aux (const char *domain,
  120. const char *msg_ctxt_id, const char *msgid,
  121. int category)
  122. {
  123. const char *translation = dcgettext (domain, msg_ctxt_id, category);
  124. if (translation == msg_ctxt_id)
  125. return msgid;
  126. else
  127. return translation;
  128. }
  129. #ifdef __GNUC__
  130. __inline
  131. #else
  132. #ifdef __cplusplus
  133. inline
  134. #endif
  135. #endif
  136. static const char *
  137. npgettext_aux (const char *domain,
  138. const char *msg_ctxt_id, const char *msgid,
  139. const char *msgid_plural, unsigned long int n,
  140. int category)
  141. {
  142. const char *translation =
  143. dcngettext (domain, msg_ctxt_id, msgid_plural, n, category);
  144. if (translation == msg_ctxt_id || translation == msgid_plural)
  145. return (n == 1 ? msgid : msgid_plural);
  146. else
  147. return translation;
  148. }
  149. /* The same thing extended for non-constant arguments. Here MSGCTXT and MSGID
  150. can be arbitrary expressions. But for string literals these macros are
  151. less efficient than those above. */
  152. #include <string.h>
  153. #define _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS \
  154. (((__GNUC__ >= 3 || __GNUG__ >= 2) && !__STRICT_ANSI__) \
  155. /* || __STDC_VERSION__ >= 199901L */ )
  156. #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
  157. #include <stdlib.h>
  158. #endif
  159. #define pgettext_expr(Msgctxt, Msgid) \
  160. dcpgettext_expr (NULL, Msgctxt, Msgid, LC_MESSAGES)
  161. #define dpgettext_expr(Domainname, Msgctxt, Msgid) \
  162. dcpgettext_expr (Domainname, Msgctxt, Msgid, LC_MESSAGES)
  163. #ifdef __GNUC__
  164. __inline
  165. #else
  166. #ifdef __cplusplus
  167. inline
  168. #endif
  169. #endif
  170. static const char *
  171. dcpgettext_expr (const char *domain,
  172. const char *msgctxt, const char *msgid,
  173. int category)
  174. {
  175. size_t msgctxt_len = strlen (msgctxt) + 1;
  176. size_t msgid_len = strlen (msgid) + 1;
  177. const char *translation;
  178. #if _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
  179. char msg_ctxt_id[msgctxt_len + msgid_len];
  180. #else
  181. char buf[1024];
  182. char *msg_ctxt_id =
  183. (msgctxt_len + msgid_len <= sizeof (buf)
  184. ? buf
  185. : (char *) malloc (msgctxt_len + msgid_len));
  186. if (msg_ctxt_id != NULL)
  187. #endif
  188. {
  189. memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1);
  190. msg_ctxt_id[msgctxt_len - 1] = '\004';
  191. memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len);
  192. translation = dcgettext (domain, msg_ctxt_id, category);
  193. #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
  194. if (msg_ctxt_id != buf)
  195. free (msg_ctxt_id);
  196. #endif
  197. if (translation != msg_ctxt_id)
  198. return translation;
  199. }
  200. return msgid;
  201. }
  202. #define npgettext_expr(Msgctxt, Msgid, MsgidPlural, N) \
  203. dcnpgettext_expr (NULL, Msgctxt, Msgid, MsgidPlural, N, LC_MESSAGES)
  204. #define dnpgettext_expr(Domainname, Msgctxt, Msgid, MsgidPlural, N) \
  205. dcnpgettext_expr (Domainname, Msgctxt, Msgid, MsgidPlural, N, LC_MESSAGES)
  206. #ifdef __GNUC__
  207. __inline
  208. #else
  209. #ifdef __cplusplus
  210. inline
  211. #endif
  212. #endif
  213. static const char *
  214. dcnpgettext_expr (const char *domain,
  215. const char *msgctxt, const char *msgid,
  216. const char *msgid_plural, unsigned long int n,
  217. int category)
  218. {
  219. size_t msgctxt_len = strlen (msgctxt) + 1;
  220. size_t msgid_len = strlen (msgid) + 1;
  221. const char *translation;
  222. #if _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
  223. char msg_ctxt_id[msgctxt_len + msgid_len];
  224. #else
  225. char buf[1024];
  226. char *msg_ctxt_id =
  227. (msgctxt_len + msgid_len <= sizeof (buf)
  228. ? buf
  229. : (char *) malloc (msgctxt_len + msgid_len));
  230. if (msg_ctxt_id != NULL)
  231. #endif
  232. {
  233. memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1);
  234. msg_ctxt_id[msgctxt_len - 1] = '\004';
  235. memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len);
  236. translation = dcngettext (domain, msg_ctxt_id, msgid_plural, n, category);
  237. #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
  238. if (msg_ctxt_id != buf)
  239. free (msg_ctxt_id);
  240. #endif
  241. if (!(translation == msg_ctxt_id || translation == msgid_plural))
  242. return translation;
  243. }
  244. return (n == 1 ? msgid : msgid_plural);
  245. }
  246. #else /* ENABLE_NLS */
  247. #define pgettext(Msgctxt, Msgid) ((void) (Msgctxt), gettext (Msgid))
  248. #define dpgettext(Domainname, Msgctxt, Msgid) \
  249. ((void) (Domainname), pgettext (Msgctxt, Msgid))
  250. #define dcpgettext(Domainname, Msgctxt, Msgid, Category) \
  251. ((void) (Category), dpgettext (Domainname, Msgctxt, Msgid))
  252. #define npgettext(Msgctxt, Msgid, MsgidPlural, N) \
  253. ((void) (Msgctxt), ngettext (Msgid, MsgidPlural, N))
  254. #define dnpgettext(Domainname, Msgctxt, Msgid, MsgidPlural, N) \
  255. ((void) (Domainname), npgettext (Msgctxt, Msgid, MsgidPlural, N)
  256. #define dcnpgettext(Domainname, Msgctxt, Msgid, MsgidPlural, N, Category) \
  257. ((void) (Category), dnpgettext (Domainname, Msgctxt, Msgid, MsgidPlural, N)
  258. #define pgettext_expr(Msgctxt, Msgid) pgettext (Msgctxt, Msgid)
  259. #define dpgettext_expr(Domainname, Msgctxt, Msgid) \
  260. dpgettext (Domainname, Msgctxt, Msgid)
  261. #define dcpgettext_expr(Domainname, Msgctxt, Msgid, Category) \
  262. dcpgettext (Domainname, Msgctxt, Msgid, Category)
  263. #define npgettext_expr(Msgctxt, Msgid, MsgidPlural, N) \
  264. npgettext (Msgctxt, Msgid, MsgidPlural, N)
  265. #define dnpgettext_expr(Domainname, Msgctxt, Msgid, MsgidPlural, N) \
  266. dnpgettext (Domainname, Msgctxt, Msgid, MsgidPlural, N)
  267. #define dcnpgettext_expr(Domainname, Msgctxt, Msgid, MsgidPlural, N, Category) \
  268. dcnpgettext (Domainname, Msgctxt, Msgid, MsgidPlural, N, Category)
  269. #endif /* ENABLE_NLS */
  270. #endif /* _LIBGETTEXT_H */