1
0

gettext.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /* Convenience header for conditional use of GNU <libintl.h>.
  2. Copyright (C) 1995-1998, 2000-2002, 2004-2006, 2009-2018 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 <https://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. or through "#define ENABLE NLS 0" before including this file. */
  18. #if defined ENABLE_NLS && 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 && !defined __UCLIBC__) || _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. # undef gettext
  57. # define gettext(Msgid) ((const char *) (Msgid))
  58. # undef dgettext
  59. # define dgettext(Domainname, Msgid) ((void) (Domainname), gettext (Msgid))
  60. # undef dcgettext
  61. # define dcgettext(Domainname, Msgid, Category) \
  62. ((void) (Category), dgettext (Domainname, Msgid))
  63. # undef ngettext
  64. # define ngettext(Msgid1, Msgid2, N) \
  65. ((N) == 1 \
  66. ? ((void) (Msgid2), (const char *) (Msgid1)) \
  67. : ((void) (Msgid1), (const char *) (Msgid2)))
  68. # undef dngettext
  69. # define dngettext(Domainname, Msgid1, Msgid2, N) \
  70. ((void) (Domainname), ngettext (Msgid1, Msgid2, N))
  71. # undef dcngettext
  72. # define dcngettext(Domainname, Msgid1, Msgid2, N, Category) \
  73. ((void) (Category), dngettext (Domainname, Msgid1, Msgid2, N))
  74. # undef textdomain
  75. # define textdomain(Domainname) ((const char *) (Domainname))
  76. # undef bindtextdomain
  77. # define bindtextdomain(Domainname, Dirname) \
  78. ((void) (Domainname), (const char *) (Dirname))
  79. # undef bind_textdomain_codeset
  80. # define bind_textdomain_codeset(Domainname, Codeset) \
  81. ((void) (Domainname), (const char *) (Codeset))
  82. #endif
  83. /* Prefer gnulib's setlocale override over libintl's setlocale override. */
  84. #ifdef GNULIB_defined_setlocale
  85. # undef setlocale
  86. # define setlocale rpl_setlocale
  87. #endif
  88. /* A pseudo function call that serves as a marker for the automated
  89. extraction of messages, but does not call gettext(). The run-time
  90. translation is done at a different place in the code.
  91. The argument, String, should be a literal string. Concatenated strings
  92. and other string expressions won't work.
  93. The macro's expansion is not parenthesized, so that it is suitable as
  94. initializer for static 'char[]' or 'const char[]' variables. */
  95. #define gettext_noop(String) String
  96. /* The separator between msgctxt and msgid in a .mo file. */
  97. #define GETTEXT_CONTEXT_GLUE "\004"
  98. /* Pseudo function calls, taking a MSGCTXT and a MSGID instead of just a
  99. MSGID. MSGCTXT and MSGID must be string literals. MSGCTXT should be
  100. short and rarely need to change.
  101. The letter 'p' stands for 'particular' or 'special'. */
  102. #ifdef DEFAULT_TEXT_DOMAIN
  103. # define pgettext(Msgctxt, Msgid) \
  104. pgettext_aux (DEFAULT_TEXT_DOMAIN, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)
  105. #else
  106. # define pgettext(Msgctxt, Msgid) \
  107. pgettext_aux (NULL, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)
  108. #endif
  109. #define dpgettext(Domainname, Msgctxt, Msgid) \
  110. pgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)
  111. #define dcpgettext(Domainname, Msgctxt, Msgid, Category) \
  112. pgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, Category)
  113. #ifdef DEFAULT_TEXT_DOMAIN
  114. # define npgettext(Msgctxt, Msgid, MsgidPlural, N) \
  115. npgettext_aux (DEFAULT_TEXT_DOMAIN, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES)
  116. #else
  117. # define npgettext(Msgctxt, Msgid, MsgidPlural, N) \
  118. npgettext_aux (NULL, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES)
  119. #endif
  120. #define dnpgettext(Domainname, Msgctxt, Msgid, MsgidPlural, N) \
  121. npgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES)
  122. #define dcnpgettext(Domainname, Msgctxt, Msgid, MsgidPlural, N, Category) \
  123. npgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, Category)
  124. #ifdef __GNUC__
  125. __inline
  126. #else
  127. #ifdef __cplusplus
  128. inline
  129. #endif
  130. #endif
  131. static const char *
  132. pgettext_aux (const char *domain,
  133. const char *msg_ctxt_id, const char *msgid,
  134. int category)
  135. {
  136. const char *translation = dcgettext (domain, msg_ctxt_id, category);
  137. if (translation == msg_ctxt_id)
  138. return msgid;
  139. else
  140. return translation;
  141. }
  142. #ifdef __GNUC__
  143. __inline
  144. #else
  145. #ifdef __cplusplus
  146. inline
  147. #endif
  148. #endif
  149. static const char *
  150. npgettext_aux (const char *domain,
  151. const char *msg_ctxt_id, const char *msgid,
  152. const char *msgid_plural, unsigned long int n,
  153. int category)
  154. {
  155. const char *translation =
  156. dcngettext (domain, msg_ctxt_id, msgid_plural, n, category);
  157. if (translation == msg_ctxt_id || translation == msgid_plural)
  158. return (n == 1 ? msgid : msgid_plural);
  159. else
  160. return translation;
  161. }
  162. /* The same thing extended for non-constant arguments. Here MSGCTXT and MSGID
  163. can be arbitrary expressions. But for string literals these macros are
  164. less efficient than those above. */
  165. #include <string.h>
  166. #if (((__GNUC__ >= 3 || __GNUG__ >= 2) && !defined __STRICT_ANSI__) \
  167. /* || __STDC_VERSION__ == 199901L
  168. || (__STDC_VERSION__ >= 201112L && !defined __STDC_NO_VLA__) */ )
  169. # define _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS 1
  170. #else
  171. # define _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS 0
  172. #endif
  173. #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
  174. #include <stdlib.h>
  175. #endif
  176. #define pgettext_expr(Msgctxt, Msgid) \
  177. dcpgettext_expr (NULL, Msgctxt, Msgid, LC_MESSAGES)
  178. #define dpgettext_expr(Domainname, Msgctxt, Msgid) \
  179. dcpgettext_expr (Domainname, Msgctxt, Msgid, LC_MESSAGES)
  180. #ifdef __GNUC__
  181. __inline
  182. #else
  183. #ifdef __cplusplus
  184. inline
  185. #endif
  186. #endif
  187. static const char *
  188. dcpgettext_expr (const char *domain,
  189. const char *msgctxt, const char *msgid,
  190. int category)
  191. {
  192. size_t msgctxt_len = strlen (msgctxt) + 1;
  193. size_t msgid_len = strlen (msgid) + 1;
  194. const char *translation;
  195. #if _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
  196. char msg_ctxt_id[msgctxt_len + msgid_len];
  197. #else
  198. char buf[1024];
  199. char *msg_ctxt_id =
  200. (msgctxt_len + msgid_len <= sizeof (buf)
  201. ? buf
  202. : (char *) malloc (msgctxt_len + msgid_len));
  203. if (msg_ctxt_id != NULL)
  204. #endif
  205. {
  206. int found_translation;
  207. memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1);
  208. msg_ctxt_id[msgctxt_len - 1] = '\004';
  209. memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len);
  210. translation = dcgettext (domain, msg_ctxt_id, category);
  211. found_translation = (translation != msg_ctxt_id);
  212. #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
  213. if (msg_ctxt_id != buf)
  214. free (msg_ctxt_id);
  215. #endif
  216. if (found_translation)
  217. return translation;
  218. }
  219. return msgid;
  220. }
  221. #define npgettext_expr(Msgctxt, Msgid, MsgidPlural, N) \
  222. dcnpgettext_expr (NULL, Msgctxt, Msgid, MsgidPlural, N, LC_MESSAGES)
  223. #define dnpgettext_expr(Domainname, Msgctxt, Msgid, MsgidPlural, N) \
  224. dcnpgettext_expr (Domainname, Msgctxt, Msgid, MsgidPlural, N, LC_MESSAGES)
  225. #ifdef __GNUC__
  226. __inline
  227. #else
  228. #ifdef __cplusplus
  229. inline
  230. #endif
  231. #endif
  232. static const char *
  233. dcnpgettext_expr (const char *domain,
  234. const char *msgctxt, const char *msgid,
  235. const char *msgid_plural, unsigned long int n,
  236. int category)
  237. {
  238. size_t msgctxt_len = strlen (msgctxt) + 1;
  239. size_t msgid_len = strlen (msgid) + 1;
  240. const char *translation;
  241. #if _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
  242. char msg_ctxt_id[msgctxt_len + msgid_len];
  243. #else
  244. char buf[1024];
  245. char *msg_ctxt_id =
  246. (msgctxt_len + msgid_len <= sizeof (buf)
  247. ? buf
  248. : (char *) malloc (msgctxt_len + msgid_len));
  249. if (msg_ctxt_id != NULL)
  250. #endif
  251. {
  252. int found_translation;
  253. memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1);
  254. msg_ctxt_id[msgctxt_len - 1] = '\004';
  255. memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len);
  256. translation = dcngettext (domain, msg_ctxt_id, msgid_plural, n, category);
  257. found_translation = !(translation == msg_ctxt_id || translation == msgid_plural);
  258. #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
  259. if (msg_ctxt_id != buf)
  260. free (msg_ctxt_id);
  261. #endif
  262. if (found_translation)
  263. return translation;
  264. }
  265. return (n == 1 ? msgid : msgid_plural);
  266. }
  267. #endif /* _LIBGETTEXT_H */