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