gettext.h 9.4 KB

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