You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gettext.h 9.8KB

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