gettext.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2.1, 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 Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser 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. # define gettext(Msgid) ((const char *) (Msgid))
  56. # define dgettext(Domainname, Msgid) ((void) (Domainname), gettext (Msgid))
  57. # define dcgettext(Domainname, Msgid, Category) \
  58. ((void) (Category), dgettext (Domainname, Msgid))
  59. # define ngettext(Msgid1, Msgid2, N) \
  60. ((N) == 1 \
  61. ? ((void) (Msgid2), (const char *) (Msgid1)) \
  62. : ((void) (Msgid1), (const char *) (Msgid2)))
  63. # define dngettext(Domainname, Msgid1, Msgid2, N) \
  64. ((void) (Domainname), ngettext (Msgid1, Msgid2, N))
  65. # define dcngettext(Domainname, Msgid1, Msgid2, N, Category) \
  66. ((void) (Category), dngettext(Domainname, Msgid1, Msgid2, N))
  67. # define textdomain(Domainname) ((const char *) (Domainname))
  68. # define bindtextdomain(Domainname, Dirname) \
  69. ((void) (Domainname), (const char *) (Dirname))
  70. # define bind_textdomain_codeset(Domainname, Codeset) \
  71. ((void) (Domainname), (const char *) (Codeset))
  72. #endif
  73. /* A pseudo function call that serves as a marker for the automated
  74. extraction of messages, but does not call gettext(). The run-time
  75. translation is done at a different place in the code.
  76. The argument, String, should be a literal string. Concatenated strings
  77. and other string expressions won't work.
  78. The macro's expansion is not parenthesized, so that it is suitable as
  79. initializer for static 'char[]' or 'const char[]' variables. */
  80. #define gettext_noop(String) String
  81. /* The separator between msgctxt and msgid in a .mo file. */
  82. #define GETTEXT_CONTEXT_GLUE "\004"
  83. /* Pseudo function calls, taking a MSGCTXT and a MSGID instead of just a
  84. MSGID. MSGCTXT and MSGID must be string literals. MSGCTXT should be
  85. short and rarely need to change.
  86. The letter 'p' stands for 'particular' or 'special'. */
  87. #ifdef DEFAULT_TEXT_DOMAIN
  88. # define pgettext(Msgctxt, Msgid) \
  89. pgettext_aux (DEFAULT_TEXT_DOMAIN, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)
  90. #else
  91. # define pgettext(Msgctxt, Msgid) \
  92. pgettext_aux (NULL, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)
  93. #endif
  94. #define dpgettext(Domainname, Msgctxt, Msgid) \
  95. pgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)
  96. #define dcpgettext(Domainname, Msgctxt, Msgid, Category) \
  97. pgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, Category)
  98. #ifdef DEFAULT_TEXT_DOMAIN
  99. # define npgettext(Msgctxt, Msgid, MsgidPlural, N) \
  100. npgettext_aux (DEFAULT_TEXT_DOMAIN, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES)
  101. #else
  102. # define npgettext(Msgctxt, Msgid, MsgidPlural, N) \
  103. npgettext_aux (NULL, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES)
  104. #endif
  105. #define dnpgettext(Domainname, Msgctxt, Msgid, MsgidPlural, N) \
  106. npgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES)
  107. #define dcnpgettext(Domainname, Msgctxt, Msgid, MsgidPlural, N, Category) \
  108. npgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, Category)
  109. #ifdef __GNUC__
  110. __inline
  111. #else
  112. #ifdef __cplusplus
  113. inline
  114. #endif
  115. #endif
  116. static const char *
  117. pgettext_aux (const char *domain,
  118. const char *msg_ctxt_id, const char *msgid,
  119. int category)
  120. {
  121. const char *translation = dcgettext (domain, msg_ctxt_id, category);
  122. if (translation == msg_ctxt_id)
  123. return msgid;
  124. else
  125. return translation;
  126. }
  127. #ifdef __GNUC__
  128. __inline
  129. #else
  130. #ifdef __cplusplus
  131. inline
  132. #endif
  133. #endif
  134. static const char *
  135. npgettext_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. /* The same thing extended for non-constant arguments. Here MSGCTXT and MSGID
  148. can be arbitrary expressions. But for string literals these macros are
  149. less efficient than those above. */
  150. #include <string.h>
  151. #define _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS \
  152. (((__GNUC__ >= 3 || __GNUG__ >= 2) && !__STRICT_ANSI__) \
  153. /* || __STDC_VERSION__ >= 199901L */ )
  154. #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
  155. #include <stdlib.h>
  156. #endif
  157. #define pgettext_expr(Msgctxt, Msgid) \
  158. dcpgettext_expr (NULL, Msgctxt, Msgid, LC_MESSAGES)
  159. #define dpgettext_expr(Domainname, Msgctxt, Msgid) \
  160. dcpgettext_expr (Domainname, Msgctxt, Msgid, LC_MESSAGES)
  161. #ifdef __GNUC__
  162. __inline
  163. #else
  164. #ifdef __cplusplus
  165. inline
  166. #endif
  167. #endif
  168. static const char *
  169. dcpgettext_expr (const char *domain,
  170. const char *msgctxt, const char *msgid,
  171. int category)
  172. {
  173. size_t msgctxt_len = strlen (msgctxt) + 1;
  174. size_t msgid_len = strlen (msgid) + 1;
  175. const char *translation;
  176. #if _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
  177. char msg_ctxt_id[msgctxt_len + msgid_len];
  178. #else
  179. char buf[1024];
  180. char *msg_ctxt_id =
  181. (msgctxt_len + msgid_len <= sizeof (buf)
  182. ? buf
  183. : (char *) malloc (msgctxt_len + msgid_len));
  184. if (msg_ctxt_id != NULL)
  185. #endif
  186. {
  187. memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1);
  188. msg_ctxt_id[msgctxt_len - 1] = '\004';
  189. memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len);
  190. translation = dcgettext (domain, msg_ctxt_id, category);
  191. #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
  192. if (msg_ctxt_id != buf)
  193. free (msg_ctxt_id);
  194. #endif
  195. if (translation != msg_ctxt_id)
  196. return translation;
  197. }
  198. return msgid;
  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. #ifdef __GNUC__
  205. __inline
  206. #else
  207. #ifdef __cplusplus
  208. inline
  209. #endif
  210. #endif
  211. static const char *
  212. dcnpgettext_expr (const char *domain,
  213. const char *msgctxt, const char *msgid,
  214. const char *msgid_plural, unsigned long int n,
  215. int category)
  216. {
  217. size_t msgctxt_len = strlen (msgctxt) + 1;
  218. size_t msgid_len = strlen (msgid) + 1;
  219. const char *translation;
  220. #if _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
  221. char msg_ctxt_id[msgctxt_len + msgid_len];
  222. #else
  223. char buf[1024];
  224. char *msg_ctxt_id =
  225. (msgctxt_len + msgid_len <= sizeof (buf)
  226. ? buf
  227. : (char *) malloc (msgctxt_len + msgid_len));
  228. if (msg_ctxt_id != NULL)
  229. #endif
  230. {
  231. memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1);
  232. msg_ctxt_id[msgctxt_len - 1] = '\004';
  233. memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len);
  234. translation = dcngettext (domain, msg_ctxt_id, msgid_plural, n, category);
  235. #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
  236. if (msg_ctxt_id != buf)
  237. free (msg_ctxt_id);
  238. #endif
  239. if (!(translation == msg_ctxt_id || translation == msgid_plural))
  240. return translation;
  241. }
  242. return (n == 1 ? msgid : msgid_plural);
  243. }
  244. #endif /* _LIBGETTEXT_H */