[PHP-DEV] GNU gettext module From: Alex Plotnick (alex <email protected>)
Date: 01/10/99

I've written a small PHP module which wraps the GNU gettext library, libintl.
It provides wrappers for the most commonly used gettext functions, making
i18n of PHP3 scripts very easy.

If someone with access would like to put this in the CVS tree, that
would be great.

One final note: This is the first PHP module I've written, so there may
be problems. I've been using it for a while now with no problems, but
if someone with a few minutes and some more experience would like to look
through it to make sure I didn't do anything silly, that would also
be appreciated.

The patch is against the latest (10 Jan 1999) CVS version.

Thanks,

        Alex Plotnick <alex <email protected>>
        WorldGate Communications, Inc.

Index: Makefile.in
===================================================================
RCS file: /repository/php3/Makefile.in,v
retrieving revision 1.282
diff -u -r1.282 Makefile.in
--- Makefile.in 1999/01/06 01:32:52 1.282
+++ Makefile.in 1999/01/10 23:48:45
@@ -37,7 +37,7 @@
 AR = ar rc
 BINNAME =  <email protected>@
 INSTALL_IT =  <email protected>@
-INCLUDE = -I$(srcdir) -I.  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@
+INCLUDE = -I$(srcdir) -I.  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@
 PROF_CFLAGS =
 CFLAGS_SHLIB =  <email protected>@
 LDFLAGS_SHLIB =  <email protected>@
@@ -72,7 +72,8 @@
        functions/dbase.c functions/dir.c \
        functions/dl.c functions/dns.c functions/exec.c functions/file.c \
        functions/filepro.c functions/filestat.c \
- functions/formatted_print.c functions/fsock.c functions/gd.c \
+ functions/formatted_print.c functions/fsock.c \
+ functions/gd.c functions/gettext.c \
        functions/head.c functions/html.c functions/image.c functions/imap.c\
        functions/link.c functions/mail.c functions/math.c functions/iptc.c \
        functions/md5.c functions/microtime.c functions/mime.c \
@@ -96,7 +97,7 @@
 
 FUNCTIONS = $(FUNCTIONS_SOURCE:.c=.o)
 
-LIBS = $(PHPLIBS)  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@
+LIBS = $(PHPLIBS)  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@
 
 all: $(BINNAME)
 
@@ -426,6 +427,11 @@
  mod_php3.h stack.h operators.h token_cache.h variables.h constants.h \
  internal_functions.h modules.h php3_list.h functions/head.h \
  functions/php3_gd.h
+functions/gettext.o: functions/gettext.c php.h php_version.h config.h request_info.h \
+ php3_hash.h alloc.h safe_mode.h fopen-wrappers.h mod_php3.h \
+ stack.h operators.h token_cache.h variables.h constants.h \
+ internal_functions.h modules.h php3_list.h functions/head.h \
+ functions/php3_gettext.h
 functions/head.o: functions/head.c php.h php_version.h config.h request_info.h \
  php3_hash.h alloc.h safe_mode.h fopen-wrappers.h \
  mod_php3.h stack.h operators.h token_cache.h variables.h constants.h \
Index: acconfig.h
===================================================================
RCS file: /repository/php3/acconfig.h,v
retrieving revision 1.75
diff -u -r1.75 acconfig.h
--- acconfig.h 1998/12/27 10:16:21 1.75
+++ acconfig.h 1999/01/10 23:48:45
@@ -32,6 +32,9 @@
 /* Define if you have the gd library (-lgd). */
 #define HAVE_LIBGD 0
 
+/* Define if you have the GNU gettext library (-lintl). */
+#define HAVE_LIBINTL 0
+
 /* Define if you have the zlib library */
 #define HAVE_ZLIB 0
 
Index: config.h.in
===================================================================
RCS file: /repository/php3/config.h.in,v
retrieving revision 1.130
diff -u -r1.130 config.h.in
--- config.h.in 1998/12/27 17:20:30 1.130
+++ config.h.in 1999/01/10 23:48:45
@@ -95,6 +95,9 @@
 /* Define if you have the gd library (-lgd). */
 #define HAVE_LIBGD 0
 
+/* Define if you have the GNU gettext library (-lintl). */
+#define HAVE_LIBINTL 0
+
 /* Define if you have the zlib library */
 #define HAVE_ZLIB 0
 
Index: configure.in
===================================================================
RCS file: /repository/php3/configure.in,v
retrieving revision 1.298
diff -u -r1.298 configure.in
--- configure.in 1998/12/27 10:16:21 1.298
+++ configure.in 1999/01/10 23:48:45
@@ -592,7 +592,35 @@
   AC_CHECK_LIB(ttf, TT_Open_Face)
 fi
 
-
+AC_MSG_CHECKING(whether to include GNU gettext support)
+AC_ARG_WITH(gettext,
+[ --without-gettext Disable GNU gettext support.
+ --with-gettext[=DIR] Include GNU gettext support (DIR is gettext's install dir).],
+[
+ case "$withval" in
+ no)
+ AC_MSG_RESULT(no) ;;
+ yes)
+ GETTEXT_LIBS="-lintl"
+ AC_DEFINE(HAVE_LIBINTL)
+ AC_CHECK_LIB(intl, gettext, [ AC_DEFINE(HAVE_LIBINTL) ])
+ AC_MSG_RESULT(yes) ;;
+ *)
+ test -f $withval/include/libintl.h && GETTEXT_INCLUDE="-I$withval/include"
+ if test -n "$GETTEXT_INCLUDE" ; then
+ GETTEXT_LIBS="-L$withval/lib -lintl"
+ AC_DEFINE(HAVE_LIBINTL)
+ AC_CHECK_LIB(intl, gettext, [ AC_DEFINE(HAVE_LIBINTL) ])
+ AC_MSG_RESULT(yes)
+ else
+ AC_MSG_RESULT(no)
+ fi ;;
+ esac
+],[
+ AC_CHECK_LIB(intl, gettext, [ AC_DEFINE(HAVE_LIBINTL) ])
+])
+AC_SUBST(GETTEXT_LIBS)
+AC_SUBST(GETTEXT_INCLUDE)
 
 AC_MSG_CHECKING(for Oracle support)
 AC_ARG_WITH(oracle,
Index: internal_functions.c
===================================================================
RCS file: /repository/php3/internal_functions.c,v
retrieving revision 1.336
diff -u -r1.336 internal_functions.c
--- internal_functions.c 1999/01/06 01:32:52 1.336
+++ internal_functions.c 1999/01/10 23:48:46
@@ -96,6 +96,7 @@
 #include "functions/php3_sysvsem.h"
 #include "functions/php3_sysvshm.h"
 #include "functions/php3_dav.h"
+#include "functions/php3_gettext.h"
 #include "functions/php3_wddx.h"
 
 extern php3_ini_structure php3_ini;
@@ -119,6 +120,7 @@
         {"Directory", php3_dir_module_ptr},
         {"File statting", php3_filestat_module_ptr},
         {"File handling", php3_file_module_ptr},
+ {"GNU gettext", php3_gettext_module_ptr},
         {"HTTP Header", php3_header_module_ptr},
         {"Sendmail", mail_module_ptr},
         {"Debugger", debugger_module_ptr},
Index: libphp3.module.in
===================================================================
RCS file: /repository/php3/libphp3.module.in,v
retrieving revision 1.16
diff -u -r1.16 libphp3.module.in
--- libphp3.module.in 1998/11/15 05:33:10 1.16
+++ libphp3.module.in 1999/01/10 23:48:46
@@ -1,7 +1,7 @@
 Name: php3_module
 ConfigStart
         RULE_WANTHSREGEX= <email protected>@
- LIBS=" <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@ $LIBS"
+ LIBS=" <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@  <email protected>@ $LIBS"
         RULE_HIDE=yes
 ConfigEnd

/*
   +----------------------------------------------------------------------+
   | PHP HTML Embedded Scripting Language Version 3.0 |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997-1999 PHP Development Team (See Credits file) |
   +----------------------------------------------------------------------+
   | This program is free software; you can redistribute it and/or modify |
   | it under the terms of one of the following licenses: |
   | |
   | A) the GNU General Public License as published by the Free Software |
   | Foundation; either version 2 of the License, or (at your option) |
   | any later version. |
   | |
   | B) the PHP License as published by the PHP Development Team and |
   | included in the distribution in the file: LICENSE |
   | |
   | This program is distributed in the hope that it will be useful, |
   | but WITHOUT ANY WARRANTY; without even the implied warranty of |
   | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
   | GNU General Public License for more details. |
   | |
   | You should have received a copy of both licenses referred to here. |
   | If you did not, or have any questions about PHP licensing, please |
   | contact core <email protected> |
   +----------------------------------------------------------------------+
   | Author: Alex Plotnick <alex <email protected>> |
   +----------------------------------------------------------------------+
 */

/* $Id$ */

#ifndef _GETTEXT_H
#define _GETTEXT_H

#if HAVE_LIBINTL
#ifndef INIT_FUNC_ARGS
#include "modules.h"
#endif

extern php3_module_entry php3_gettext_module_entry;
#define php3_gettext_module_ptr &php3_gettext_module_entry

extern void php3_info_gettext(void);
extern void php3_textdomain(INTERNAL_FUNCTION_PARAMETERS);
extern void php3_gettext(INTERNAL_FUNCTION_PARAMETERS);
extern void php3_dgettext(INTERNAL_FUNCTION_PARAMETERS);
extern void php3_dcgettext(INTERNAL_FUNCTION_PARAMETERS);
extern void php3_bindtextdomain(INTERNAL_FUNCTION_PARAMETERS);

#else
#define php3_gettext_module_ptr NULL
#endif /* HAVE_LIBINTL */

#endif /* _GETTEXT_H */

/*
   +----------------------------------------------------------------------+
   | PHP HTML Embedded Scripting Language Version 3.0 |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997-1999 PHP Development Team (See Credits file) |
   +----------------------------------------------------------------------+
   | This program is free software; you can redistribute it and/or modify |
   | it under the terms of one of the following licenses: |
   | |
   | A) the GNU General Public License as published by the Free Software |
   | Foundation; either version 2 of the License, or (at your option) |
   | any later version. |
   | |
   | B) the PHP License as published by the PHP Development Team and |
   | included in the distribution in the file: LICENSE |
   | |
   | This program is distributed in the hope that it will be useful, |
   | but WITHOUT ANY WARRANTY; without even the implied warranty of |
   | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
   | GNU General Public License for more details. |
   | |
   | You should have received a copy of both licenses referred to here. |
   | If you did not, or have any questions about PHP licensing, please |
   | contact core <email protected> |
   +----------------------------------------------------------------------+
   | Author: Alex Plotnick <alex <email protected>> |
   +----------------------------------------------------------------------+
 */

/* $Id$ */

#include <stdio.h>
#include "php.h"
#include "internal_functions.h"
#include "php3_gettext.h"

#if HAVE_LIBINTL

#include <libintl.h>

function_entry php3_gettext_functions[] = {
    {"textdomain", php3_textdomain, NULL},
    {"gettext", php3_gettext, NULL},
    {"_", php3_gettext, NULL},
        {"dgettext", php3_dgettext, NULL},
        {"dcgettext", php3_dcgettext, NULL},
        {"bindtextdomain", php3_bindtextdomain, NULL},
    {NULL, NULL, NULL}
};

php3_module_entry php3_gettext_module_entry = {
        "gettext", php3_gettext_functions, NULL, NULL, NULL, NULL, php3_info_gettext, STANDARD_MODULE_PROPERTIES
};

void php3_info_gettext(void)
{
        php3_printf("GNU gettext support active.");
}

void php3_textdomain(INTERNAL_FUNCTION_PARAMETERS)
{
    pval *domain;
    char *domain_name, *retval;

    if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &domain) == FAILURE) {
                WRONG_PARAM_COUNT;
    }
    convert_to_string(domain);

        if (strcmp(domain->value.str.val, "")
                && strcmp(domain->value.str.val, "0"))
        {
                domain_name = domain->value.str.val;
        } else {
                domain_name = NULL;
        }

        retval = textdomain(domain_name);

    RETURN_STRING(retval, 1);
}

void php3_gettext(INTERNAL_FUNCTION_PARAMETERS)
{
    pval *msgid;
    char *msgstr;

    if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &msgid) == FAILURE) {
                WRONG_PARAM_COUNT;
    }
    convert_to_string(msgid);

    msgstr = gettext(msgid->value.str.val);

    RETURN_STRING(msgstr, 1);
}

void php3_dgettext(INTERNAL_FUNCTION_PARAMETERS)
{
        pval *domain_name, *msgid;
        char *msgstr;

        if (ARG_COUNT(ht) != 2
                || getParameters(ht, 2, &domain_name, &msgid) == FAILURE)
        {
                WRONG_PARAM_COUNT;
        }
        convert_to_string(domain_name);
        convert_to_string(msgid);

        msgstr = dgettext(domain_name->value.str.val, msgid->value.str.val);

        RETURN_STRING(msgstr, 1);
}

void php3_dcgettext(INTERNAL_FUNCTION_PARAMETERS)
{
        pval *domain_name, *msgid, *category;
        char *msgstr;

        if (ARG_COUNT(ht) != 3
                || getParameters(ht, 3, &domain_name, &msgid, &category) == FAILURE)
        {
                WRONG_PARAM_COUNT;
        }
        convert_to_string(domain_name);
        convert_to_string(msgid);
        convert_to_long(category);

        msgstr = dcgettext(domain_name->value.str.val,
                                           msgid->value.str.val,
                                           category->value.lval);

        RETURN_STRING(msgstr, 1);
}

void php3_bindtextdomain(INTERNAL_FUNCTION_PARAMETERS)
{
        pval *domain_name, *dir;
        char *retval, *dir_name;

        if (ARG_COUNT(ht) != 2
                || getParameters(ht, 2, &domain_name, &dir) == FAILURE)
        {
                WRONG_PARAM_COUNT;
        }
        convert_to_string(domain_name);
        convert_to_string(dir);

        if (strcmp(dir->value.str.val, "")
                && strcmp(dir->value.str.val, "0"))
        {
                dir_name = dir->value.str.val;
        } else {
                dir_name = NULL;
        }

        retval = bindtextdomain(domain_name->value.str.val, dir_name);

        RETURN_STRING(retval, 1);
}

#endif /* HAVE_LIBINTL */

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 */

--
PHP Development Mailing List   http://www.php.net/
To unsubscribe send an empty message to php-dev-unsubscribe <email protected>
For help: php-dev-help <email protected>