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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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) || _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. /* Pseudo function calls, taking a MSGCTXT and a MSGID instead of just a
  85. MSGID. MSGCTXT and MSGID must be string literals. MSGCTXT should be
  86. short and rarely need to change.
  87. The letter 'p' stands for 'particular' or 'special'. */
  88. #ifdef DEFAULT_TEXT_DOMAIN
  89. # define pgettext(Msgctxt, Msgid) \
  90. pgettext_aux (DEFAULT_TEXT_DOMAIN, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)
  91. #else
  92. # define pgettext(Msgctxt, Msgid) \
  93. pgettext_aux (NULL, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)
  94. #endif
  95. #define dpgettext(Domainname, Msgctxt, Msgid) \
  96. pgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)
  97. #define dcpgettext(Domainname, Msgctxt, Msgid, Category) \
  98. pgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, Category)
  99. #ifdef DEFAULT_TEXT_DOMAIN
  100. # define npgettext(Msgctxt, Msgid, MsgidPlural, N) \
  101. npgettext_aux (DEFAULT_TEXT_DOMAIN, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES)
  102. #else
  103. # define npgettext(Msgctxt, Msgid, MsgidPlural, N) \
  104. npgettext_aux (NULL, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES)
  105. #endif
  106. #define dnpgettext(Domainname, Msgctxt, Msgid, MsgidPlural, N) \
  107. npgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES)
  108. #define dcnpgettext(Domainname, Msgctxt, Msgid, MsgidPlural, N, Category) \
  109. npgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, Category)
  110. #ifdef __GNUC__
  111. __inline
  112. #else
  113. #ifdef __cplusplus
  114. inline
  115. #endif
  116. #endif
  117. static const char *
  118. pgettext_aux (const char *domain,
  119. const char *msg_ctxt_id, const char *msgid,
  120. int category)
  121. {
  122. const char *translation = dcgettext (domain, msg_ctxt_id, category);
  123. if (translation == msg_ctxt_id)
  124. return msgid;
  125. else
  126. return translation;
  127. }
  128. #ifdef __GNUC__
  129. __inline
  130. #else
  131. #ifdef __cplusplus
  132. inline
  133. #endif
  134. #endif
  135. static const char *
  136. npgettext_aux (const char *domain,
  137. const char *msg_ctxt_id, const char *msgid,
  138. const char *msgid_plural, unsigned long int n,
  139. int category)
  140. {
  141. const char *translation =
  142. dcngettext (domain, msg_ctxt_id, msgid_plural, n, category);
  143. if (translation == msg_ctxt_id || translation == msgid_plural)
  144. return (n == 1 ? msgid : msgid_plural);
  145. else
  146. return translation;
  147. }
  148. /* The same thing extended for non-constant arguments. Here MSGCTXT and MSGID
  149. can be arbitrary expressions. But for string literals these macros are
  150. less efficient than those above. */
  151. #include <string.h>
  152. #define _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS \
  153. (((__GNUC__ >= 3 || __GNUG__ >= 2) && !__STRICT_ANSI__) \
  154. /* || __STDC_VERSION__ >= 199901L */ )
  155. #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
  156. #include <stdlib.h>
  157. #endif
  158. #define pgettext_expr(Msgctxt, Msgid) \
  159. dcpgettext_expr (NULL, Msgctxt, Msgid, LC_MESSAGES)
  160. #define dpgettext_expr(Domainname, Msgctxt, Msgid) \
  161. dcpgettext_expr (Domainname, Msgctxt, Msgid, LC_MESSAGES)
  162. #ifdef __GNUC__
  163. __inline
  164. #else
  165. #ifdef __cplusplus
  166. inline
  167. #endif
  168. #endif
  169. static const char *
  170. dcpgettext_expr (const char *domain,
  171. const char *msgctxt, const char *msgid,
  172. int category)
  173. {
  174. size_t msgctxt_len = strlen (msgctxt) + 1;
  175. size_t msgid_len = strlen (msgid) + 1;
  176. const char *translation;
  177. #if _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
  178. char msg_ctxt_id[msgctxt_len + msgid_len];
  179. #else
  180. char buf[1024];
  181. char *msg_ctxt_id =
  182. (msgctxt_len + msgid_len <= sizeof (buf)
  183. ? buf
  184. : (char *) malloc (msgctxt_len + msgid_len));
  185. if (msg_ctxt_id != NULL)
  186. #endif
  187. {
  188. memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1);
  189. msg_ctxt_id[msgctxt_len - 1] = '\004';
  190. memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len);
  191. translation = dcgettext (domain, msg_ctxt_id, category);
  192. #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
  193. if (msg_ctxt_id != buf)
  194. free (msg_ctxt_id);
  195. #endif
  196. if (translation != msg_ctxt_id)
  197. return translation;
  198. }
  199. return msgid;
  200. }
  201. #define npgettext_expr(Msgctxt, Msgid, MsgidPlural, N) \
  202. dcnpgettext_expr (NULL, Msgctxt, Msgid, MsgidPlural, N, LC_MESSAGES)
  203. #define dnpgettext_expr(Domainname, Msgctxt, Msgid, MsgidPlural, N) \
  204. dcnpgettext_expr (Domainname, Msgctxt, Msgid, MsgidPlural, N, LC_MESSAGES)
  205. #ifdef __GNUC__
  206. __inline
  207. #else
  208. #ifdef __cplusplus
  209. inline
  210. #endif
  211. #endif
  212. static const char *
  213. dcnpgettext_expr (const char *domain,
  214. const char *msgctxt, const char *msgid,
  215. const char *msgid_plural, unsigned long int n,
  216. int category)
  217. {
  218. size_t msgctxt_len = strlen (msgctxt) + 1;
  219. size_t msgid_len = strlen (msgid) + 1;
  220. const char *translation;
  221. #if _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
  222. char msg_ctxt_id[msgctxt_len + msgid_len];
  223. #else
  224. char buf[1024];
  225. char *msg_ctxt_id =
  226. (msgctxt_len + msgid_len <= sizeof (buf)
  227. ? buf
  228. : (char *) malloc (msgctxt_len + msgid_len));
  229. if (msg_ctxt_id != NULL)
  230. #endif
  231. {
  232. memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1);
  233. msg_ctxt_id[msgctxt_len - 1] = '\004';
  234. memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len);
  235. translation = dcngettext (domain, msg_ctxt_id, msgid_plural, n, category);
  236. #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
  237. if (msg_ctxt_id != buf)
  238. free (msg_ctxt_id);
  239. #endif
  240. if (!(translation == msg_ctxt_id || translation == msgid_plural))
  241. return translation;
  242. }
  243. return (n == 1 ? msgid : msgid_plural);
  244. }
  245. #endif /* _LIBGETTEXT_H */