Test for Solaris native gettext in libc, seeing if it supports GNU catalog
[platform/upstream/glib.git] / m4macros / glib-gettext.m4
1 # Copyright (C) 1995-2002 Free Software Foundation, Inc.
2 # Copyright (C) 2001-2003 Red Hat, Inc.
3 #
4 # This file is free software, distributed under the terms of the GNU
5 # General Public License.  As a special exception to the GNU General
6 # Public License, this file may be distributed as part of a program
7 # that contains a configuration script generated by Autoconf, under
8 # the same distribution terms as the rest of that program.
9 #
10 # This file can be copied and used freely without restrictions.  It can
11 # be used in projects which are not available under the GNU Public License
12 # but which still want to provide support for the GNU gettext functionality.
13 #
14 # Macro to add for using GNU gettext.
15 # Ulrich Drepper <drepper@cygnus.com>, 1995, 1996
16 #
17 # Modified to never use included libintl. 
18 # Owen Taylor <otaylor@redhat.com>, 12/15/1998
19 #
20 # Major rework to remove unused code
21 # Owen Taylor <otaylor@redhat.com>, 12/11/2002
22 #
23 # Added better handling of ALL_LINGUAS from GNU gettext version 
24 # written by Bruno Haible, Owen Taylor <otaylor.redhat.com> 5/30/3002
25
26 #
27 # We need this here as well, since someone might use autoconf-2.5x
28 # to configure GLib then an older version to configure a package
29 # using AM_GLIB_GNU_GETTEXT
30 AC_PREREQ(2.53)
31
32 dnl
33 dnl We go to great lengths to make sure that aclocal won't 
34 dnl try to pull in the installed version of these macros
35 dnl when running aclocal in the glib directory.
36 dnl
37 m4_copy([AC_DEFUN],[glib_DEFUN])
38 m4_copy([AC_REQUIRE],[glib_REQUIRE])
39 dnl
40 dnl At the end, if we're not within glib, we'll define the public
41 dnl definitions in terms of our private definitions.
42 dnl
43
44 # GLIB_LC_MESSAGES
45 #--------------------
46 glib_DEFUN([GLIB_LC_MESSAGES],
47   [AC_CHECK_HEADERS([locale.h])
48     if test $ac_cv_header_locale_h = yes; then
49     AC_CACHE_CHECK([for LC_MESSAGES], am_cv_val_LC_MESSAGES,
50       [AC_TRY_LINK([#include <locale.h>], [return LC_MESSAGES],
51        am_cv_val_LC_MESSAGES=yes, am_cv_val_LC_MESSAGES=no)])
52     if test $am_cv_val_LC_MESSAGES = yes; then
53       AC_DEFINE(HAVE_LC_MESSAGES, 1,
54         [Define if your <locale.h> file defines LC_MESSAGES.])
55     fi
56   fi])
57
58 # GLIB_PATH_PROG_WITH_TEST
59 #----------------------------
60 dnl GLIB_PATH_PROG_WITH_TEST(VARIABLE, PROG-TO-CHECK-FOR,
61 dnl   TEST-PERFORMED-ON-FOUND_PROGRAM [, VALUE-IF-NOT-FOUND [, PATH]])
62 glib_DEFUN([GLIB_PATH_PROG_WITH_TEST],
63 [# Extract the first word of "$2", so it can be a program name with args.
64 set dummy $2; ac_word=[$]2
65 AC_MSG_CHECKING([for $ac_word])
66 AC_CACHE_VAL(ac_cv_path_$1,
67 [case "[$]$1" in
68   /*)
69   ac_cv_path_$1="[$]$1" # Let the user override the test with a path.
70   ;;
71   *)
72   IFS="${IFS=   }"; ac_save_ifs="$IFS"; IFS="${IFS}:"
73   for ac_dir in ifelse([$5], , $PATH, [$5]); do
74     test -z "$ac_dir" && ac_dir=.
75     if test -f $ac_dir/$ac_word; then
76       if [$3]; then
77         ac_cv_path_$1="$ac_dir/$ac_word"
78         break
79       fi
80     fi
81   done
82   IFS="$ac_save_ifs"
83 dnl If no 4th arg is given, leave the cache variable unset,
84 dnl so AC_PATH_PROGS will keep looking.
85 ifelse([$4], , , [  test -z "[$]ac_cv_path_$1" && ac_cv_path_$1="$4"
86 ])dnl
87   ;;
88 esac])dnl
89 $1="$ac_cv_path_$1"
90 if test ifelse([$4], , [-n "[$]$1"], ["[$]$1" != "$4"]); then
91   AC_MSG_RESULT([$]$1)
92 else
93   AC_MSG_RESULT(no)
94 fi
95 AC_SUBST($1)dnl
96 ])
97
98 # GLIB_WITH_NLS
99 #-----------------
100 glib_DEFUN([GLIB_WITH_NLS],
101   dnl NLS is obligatory
102   [USE_NLS=yes
103     AC_SUBST(USE_NLS)
104
105     gt_cv_have_gettext=no
106
107     CATOBJEXT=NONE
108     XGETTEXT=:
109     INTLLIBS=
110
111     AC_CHECK_HEADER(libintl.h,
112      [gt_cv_func_dgettext_libintl="no"
113       libintl_extra_libs=""
114
115       #
116       # First check in libc
117       #
118       AC_CACHE_CHECK([for dgettext in libc], gt_cv_func_dgettext_libc,
119         [AC_TRY_LINK([
120 #include <libintl.h>
121 ],
122           [return (int) dgettext ("","")],
123           gt_cv_func_dgettext_libc=yes,
124           gt_cv_func_dgettext_libc=no)
125         ])
126   
127       if test "$gt_cv_func_dgettext_libc" = "yes" ; then
128         AC_CHECK_FUNCS(bind_textdomain_codeset)
129       fi
130
131       #
132       # If we don't have everything we want, check in libintl
133       #
134       if test "$gt_cv_func_dgettext_libc" != "yes" \
135          || test "$ac_cv_func_bind_textdomain_codeset" != "yes" ; then
136         
137         AC_CHECK_LIB(intl, bindtextdomain,
138             [AC_CHECK_LIB(intl, dgettext,
139                           gt_cv_func_dgettext_libintl=yes)])
140
141         if test "$gt_cv_func_dgettext_libintl" != "yes" ; then
142           AC_MSG_CHECKING([if -liconv is needed to use gettext])
143           AC_MSG_RESULT([])
144           AC_CHECK_LIB(intl, dcgettext,
145                        [gt_cv_func_dgettext_libintl=yes
146                         libintl_extra_libs=-liconv],
147                         :,-liconv)
148         fi
149
150         #
151         # If we found libintl, then check in it for bind_textdomain_codeset();
152         # we'll prefer libc if neither have bind_textdomain_codeset(),
153         # and both have dgettext
154         #
155         if test "$gt_cv_func_dgettext_libintl" = "yes" ; then
156           glib_save_LIBS="$LIBS"
157           LIBS="$LIBS -lintl $libintl_extra_libs"
158           unset ac_cv_func_bind_textdomain_codeset
159           AC_CHECK_FUNCS(bind_textdomain_codeset)
160           LIBS="$glib_save_LIBS"
161
162           if test "$ac_cv_func_bind_textdomain_codeset" = "yes" ; then
163             gt_cv_func_dgettext_libc=no
164           else
165             if test "$gt_cv_func_dgettext_libc" = "yes"; then
166               gt_cv_func_dgettext_libintl=no
167             fi
168           fi
169         fi
170       fi
171
172       if test "$gt_cv_func_dgettext_libc" = "yes" \
173         || test "$gt_cv_func_dgettext_libintl" = "yes"; then
174         gt_cv_have_gettext=yes
175       fi
176   
177       if test "$gt_cv_func_dgettext_libintl" = "yes"; then
178         INTLLIBS="-lintl $libintl_extra_libs"
179       fi
180   
181       if test "$gt_cv_have_gettext" = "yes"; then
182         AC_DEFINE(HAVE_GETTEXT,1,
183           [Define if the GNU gettext() function is already present or preinstalled.])
184         GLIB_PATH_PROG_WITH_TEST(MSGFMT, msgfmt,
185           [test -z "`$ac_dir/$ac_word -h 2>&1 | grep 'dv '`"], no)dnl
186         if test "$MSGFMT" != "no"; then
187           AC_CHECK_FUNCS(dcgettext)
188           AC_PATH_PROG(GMSGFMT, gmsgfmt, $MSGFMT)
189           GLIB_PATH_PROG_WITH_TEST(XGETTEXT, xgettext,
190             [test -z "`$ac_dir/$ac_word -h 2>&1 | grep '(HELP)'`"], :)
191           AC_TRY_LINK(, [extern int _nl_msg_cat_cntr;
192                          return _nl_msg_cat_cntr],
193             [CATOBJEXT=.gmo 
194              DATADIRNAME=share],
195             [case $host in
196             *-*-solaris*)
197             dnl On Solaris, if bind_textdomain_codeset is in libc,
198             dnl GNU format message catalog is always supported,
199             dnl since both are added to the libc all together.
200             dnl Hence, we'd like to go with DATADIRNAME=share and
201             dnl and CATOBJEXT=.gmo in this case.
202             AC_CHECK_FUNC(bind_textdomain_codeset,
203               [CATOBJEXT=.gmo 
204                DATADIRNAME=share],
205               [CATOBJEXT=.mo
206                DATADIRNAME=lib])
207             ;;
208             *)
209             CATOBJEXT=.mo
210             DATADIRNAME=lib
211             ;;
212             esac])
213           INSTOBJEXT=.mo
214         else
215           gt_cv_have_gettext=no
216         fi
217       fi
218     ])
219
220     if test "$gt_cv_have_gettext" = "yes" ; then
221       AC_DEFINE(ENABLE_NLS, 1,
222         [always defined to indicate that i18n is enabled])
223     fi
224
225     dnl Test whether we really found GNU xgettext.
226     if test "$XGETTEXT" != ":"; then
227       dnl If it is not GNU xgettext we define it as : so that the
228       dnl Makefiles still can work.
229       if $XGETTEXT --omit-header /dev/null 2> /dev/null; then
230         : ;
231       else
232         AC_MSG_RESULT(
233           [found xgettext program is not GNU xgettext; ignore it])
234         XGETTEXT=":"
235       fi
236     fi
237
238     # We need to process the po/ directory.
239     POSUB=po
240
241     AC_OUTPUT_COMMANDS(
242       [case "$CONFIG_FILES" in *po/Makefile.in*)
243         sed -e "/POTFILES =/r po/POTFILES" po/Makefile.in > po/Makefile
244       esac])
245
246     dnl These rules are solely for the distribution goal.  While doing this
247     dnl we only have to keep exactly one list of the available catalogs
248     dnl in configure.in.
249     for lang in $ALL_LINGUAS; do
250       GMOFILES="$GMOFILES $lang.gmo"
251       POFILES="$POFILES $lang.po"
252     done
253
254     dnl Make all variables we use known to autoconf.
255     AC_SUBST(CATALOGS)
256     AC_SUBST(CATOBJEXT)
257     AC_SUBST(DATADIRNAME)
258     AC_SUBST(GMOFILES)
259     AC_SUBST(INSTOBJEXT)
260     AC_SUBST(INTLLIBS)
261     AC_SUBST(PO_IN_DATADIR_TRUE)
262     AC_SUBST(PO_IN_DATADIR_FALSE)
263     AC_SUBST(POFILES)
264     AC_SUBST(POSUB)
265   ])
266
267 # AM_GLIB_GNU_GETTEXT
268 # -------------------
269 # Do checks necessary for use of gettext. If a suitable implementation 
270 # of gettext is found in either in libintl or in the C library,
271 # it will set INTLLIBS to the libraries needed for use of gettext
272 # and AC_DEFINE() HAVE_GETTEXT and ENABLE_NLS. (The shell variable
273 # gt_cv_have_gettext will be set to "yes".) It will also call AC_SUBST()
274 # on various variables needed by the Makefile.in.in installed by 
275 # glib-gettextize.
276 dnl
277 glib_DEFUN(GLIB_GNU_GETTEXT,
278   [AC_REQUIRE([AC_PROG_CC])dnl
279    AC_REQUIRE([AC_HEADER_STDC])dnl
280    
281    GLIB_LC_MESSAGES
282    GLIB_WITH_NLS
283
284    if test "$gt_cv_have_gettext" = "yes"; then
285      if test "x$ALL_LINGUAS" = "x"; then
286        LINGUAS=
287      else
288        AC_MSG_CHECKING(for catalogs to be installed)
289        NEW_LINGUAS=
290        for presentlang in $ALL_LINGUAS; do
291          useit=no
292          if test "%UNSET%" != "${LINGUAS-%UNSET%}"; then
293            desiredlanguages="$LINGUAS"
294          else
295            desiredlanguages="$ALL_LINGUAS"
296          fi
297          for desiredlang in $desiredlanguages; do
298            # Use the presentlang catalog if desiredlang is
299            #   a. equal to presentlang, or
300            #   b. a variant of presentlang (because in this case,
301            #      presentlang can be used as a fallback for messages
302            #      which are not translated in the desiredlang catalog).
303            case "$desiredlang" in
304              "$presentlang"*) useit=yes;;
305            esac
306          done
307          if test $useit = yes; then
308            NEW_LINGUAS="$NEW_LINGUAS $presentlang"
309          fi
310        done
311        LINGUAS=$NEW_LINGUAS
312        AC_MSG_RESULT($LINGUAS)
313      fi
314
315      dnl Construct list of names of catalog files to be constructed.
316      if test -n "$LINGUAS"; then
317        for lang in $LINGUAS; do CATALOGS="$CATALOGS $lang$CATOBJEXT"; done
318      fi
319    fi
320
321    dnl If the AC_CONFIG_AUX_DIR macro for autoconf is used we possibly
322    dnl find the mkinstalldirs script in another subdir but ($top_srcdir).
323    dnl Try to locate is.
324    MKINSTALLDIRS=
325    if test -n "$ac_aux_dir"; then
326      MKINSTALLDIRS="$ac_aux_dir/mkinstalldirs"
327    fi
328    if test -z "$MKINSTALLDIRS"; then
329      MKINSTALLDIRS="\$(top_srcdir)/mkinstalldirs"
330    fi
331    AC_SUBST(MKINSTALLDIRS)
332
333    dnl Generate list of files to be processed by xgettext which will
334    dnl be included in po/Makefile.
335    test -d po || mkdir po
336    if test "x$srcdir" != "x."; then
337      if test "x`echo $srcdir | sed 's@/.*@@'`" = "x"; then
338        posrcprefix="$srcdir/"
339      else
340        posrcprefix="../$srcdir/"
341      fi
342    else
343      posrcprefix="../"
344    fi
345    rm -f po/POTFILES
346    sed -e "/^#/d" -e "/^\$/d" -e "s,.*, $posrcprefix& \\\\," -e "\$s/\(.*\) \\\\/\1/" \
347         < $srcdir/po/POTFILES.in > po/POTFILES
348   ])
349
350 # AM_GLIB_DEFINE_LOCALEDIR(VARIABLE)
351 # -------------------------------
352 # Define VARIABLE to the location where catalog files will
353 # be installed by po/Makefile.
354 glib_DEFUN(GLIB_DEFINE_LOCALEDIR,
355 [glib_REQUIRE([GLIB_GNU_GETTEXT])dnl
356 glib_save_prefix="$prefix"
357 glib_save_exec_prefix="$exec_prefix"
358 test "x$prefix" = xNONE && prefix=$ac_default_prefix
359 test "x$exec_prefix" = xNONE && exec_prefix=$prefix
360 if test "x$CATOBJEXT" = "x.mo" ; then
361   localedir=`eval echo "${libdir}/locale"`
362 else
363   localedir=`eval echo "${datadir}/locale"`
364 fi
365 prefix="$glib_save_prefix"
366 exec_prefix="$glib_save_exec_prefix"
367 AC_DEFINE_UNQUOTED($1, "$localedir",
368   [Define the location where the catalogs will be installed])
369 ])
370
371 dnl
372 dnl Now the definitions that aclocal will find
373 dnl
374 ifdef(glib_configure_in,[],[
375 AC_DEFUN(AM_GLIB_GNU_GETTEXT,[GLIB_GNU_GETTEXT($@)])
376 AC_DEFUN(AM_GLIB_DEFINE_LOCALEDIR,[GLIB_DEFINE_LOCALEDIR($@)])
377 ])dnl