gettext.h 9.9 KB

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