php-src/ext/gettext/gettext.c

281 lines
7.0 KiB
C
Raw Normal View History

1999-04-21 17:26:16 +00:00
/*
+----------------------------------------------------------------------+
1999-07-16 13:13:16 +00:00
| PHP version 4.0 |
1999-04-21 17:26:16 +00:00
+----------------------------------------------------------------------+
2001-02-26 06:11:02 +00:00
| Copyright (c) 1997-2001 The PHP Group |
1999-04-21 17:26:16 +00:00
+----------------------------------------------------------------------+
| This source file is subject to version 2.02 of the PHP license, |
1999-07-16 13:13:16 +00:00
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| http://www.php.net/license/2_02.txt. |
1999-07-16 13:13:16 +00:00
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
1999-04-21 17:26:16 +00:00
+----------------------------------------------------------------------+
| Author: Alex Plotnick <alex@wgate.com> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#include <stdio.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
1999-04-21 17:26:16 +00:00
#include "php.h"
#include "php_gettext.h"
1999-04-21 17:26:16 +00:00
#if HAVE_LIBINTL
#include <libintl.h>
#include "ext/standard/info.h"
1999-04-21 17:26:16 +00:00
/* {{{ php_gettext_functions[]
*/
1999-12-17 21:50:07 +00:00
function_entry php_gettext_functions[] = {
2000-12-08 16:25:38 +00:00
PHP_FE(textdomain, NULL)
PHP_FE(gettext, NULL)
PHP_FALIAS(_, gettext, NULL)
PHP_FE(dgettext, NULL)
PHP_FE(dcgettext, NULL)
PHP_FE(bindtextdomain, NULL)
2001-08-29 13:13:24 +00:00
#if HAVE_NGETTEXT
2001-08-27 22:26:49 +00:00
PHP_FE(ngettext, NULL)
2001-08-29 13:13:24 +00:00
#endif
#if HAVE_DNGETTEXT
2001-08-27 22:26:49 +00:00
PHP_FE(dngettext, NULL)
2001-08-29 13:13:24 +00:00
#endif
#if HAVE_DCNGETTEXT
2001-08-27 22:26:49 +00:00
PHP_FE(dcngettext, NULL)
2001-08-29 13:13:24 +00:00
#endif
1999-04-21 17:26:16 +00:00
{NULL, NULL, NULL}
};
/* }}} */
1999-04-21 17:26:16 +00:00
1999-12-17 21:50:07 +00:00
zend_module_entry php_gettext_module_entry = {
2001-08-27 22:26:49 +00:00
"gettext",
php_gettext_functions,
NULL,
NULL,
NULL,
NULL,
PHP_MINFO(gettext),
STANDARD_MODULE_PROPERTIES
1999-04-21 17:26:16 +00:00
};
#ifdef COMPILE_DL_GETTEXT
ZEND_GET_MODULE(php_gettext)
#endif
1999-07-27 19:44:46 +00:00
PHP_MINFO_FUNCTION(gettext)
1999-04-21 17:26:16 +00:00
{
php_info_print_table_start();
php_info_print_table_row(2, "GetText Support", "enabled");
php_info_print_table_end();
1999-04-21 17:26:16 +00:00
}
2000-03-19 23:39:05 +00:00
/* {{{ proto string textdomain(string domain)
2000-08-08 20:28:19 +00:00
Set the textdomain to "domain". Returns the current domain */
PHP_FUNCTION(textdomain)
1999-04-21 17:26:16 +00:00
{
zval **domain;
2000-12-08 16:25:38 +00:00
char *domain_name, *retval;
char *val;
1999-04-21 17:26:16 +00:00
2000-12-08 16:25:38 +00:00
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &domain) == FAILURE) {
1999-04-21 17:26:16 +00:00
WRONG_PARAM_COUNT;
2000-12-08 16:25:38 +00:00
}
convert_to_string_ex(domain);
1999-04-21 17:26:16 +00:00
val = Z_STRVAL_PP(domain);
if (strcmp(val, "") && strcmp(val, "0")) {
domain_name = val;
1999-04-21 17:26:16 +00:00
} else {
domain_name = NULL;
}
retval = textdomain(domain_name);
2000-12-08 16:25:38 +00:00
RETURN_STRING(retval, 1);
1999-04-21 17:26:16 +00:00
}
2000-03-19 23:39:05 +00:00
/* }}} */
1999-04-21 17:26:16 +00:00
2000-03-19 23:39:05 +00:00
/* {{{ proto string gettext(string msgid)
2000-08-08 20:28:19 +00:00
Return the translation of msgid for the current domain, or msgid unaltered if a translation does not exist */
PHP_FUNCTION(gettext)
1999-04-21 17:26:16 +00:00
{
zval **msgid;
2000-12-08 16:25:38 +00:00
char *msgstr;
1999-04-21 17:26:16 +00:00
2000-12-08 16:25:38 +00:00
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &msgid) == FAILURE) {
1999-04-21 17:26:16 +00:00
WRONG_PARAM_COUNT;
2000-12-08 16:25:38 +00:00
}
convert_to_string_ex(msgid);
1999-04-21 17:26:16 +00:00
msgstr = gettext(Z_STRVAL_PP(msgid));
1999-04-21 17:26:16 +00:00
2000-12-08 16:25:38 +00:00
RETURN_STRING(msgstr, 1);
1999-04-21 17:26:16 +00:00
}
2000-03-19 23:39:05 +00:00
/* }}} */
1999-04-21 17:26:16 +00:00
2000-03-19 23:39:05 +00:00
/* {{{ proto string dgettext(string domain_name, string msgid)
2000-08-08 20:28:19 +00:00
Return the translation of msgid for domain_name, or msgid unaltered if a translation does not exist */
PHP_FUNCTION(dgettext)
1999-04-21 17:26:16 +00:00
{
zval **domain_name, **msgid;
1999-04-21 17:26:16 +00:00
char *msgstr;
2000-12-08 16:25:38 +00:00
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &domain_name, &msgid) == FAILURE) {
1999-04-21 17:26:16 +00:00
WRONG_PARAM_COUNT;
}
convert_to_string_ex(domain_name);
convert_to_string_ex(msgid);
1999-04-21 17:26:16 +00:00
msgstr = dgettext(Z_STRVAL_PP(domain_name), Z_STRVAL_PP(msgid));
1999-04-21 17:26:16 +00:00
RETURN_STRING(msgstr, 1);
}
2000-03-19 23:39:05 +00:00
/* }}} */
1999-04-21 17:26:16 +00:00
2000-03-19 23:39:05 +00:00
/* {{{ proto string dcgettext(string domain_name, string msgid, long category)
2000-08-08 20:28:19 +00:00
Return the translation of msgid for domain_name and category, or msgid unaltered if a translation does not exist */
PHP_FUNCTION(dcgettext)
1999-04-21 17:26:16 +00:00
{
zval **domain_name, **msgid, **category;
1999-04-21 17:26:16 +00:00
char *msgstr;
2000-12-08 16:25:38 +00:00
if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &domain_name, &msgid, &category) == FAILURE) {
1999-04-21 17:26:16 +00:00
WRONG_PARAM_COUNT;
}
convert_to_string_ex(domain_name);
convert_to_string_ex(msgid);
convert_to_long_ex(category);
1999-04-21 17:26:16 +00:00
2001-08-27 22:26:49 +00:00
msgstr = dcgettext(Z_STRVAL_PP(domain_name), Z_STRVAL_PP(msgid), Z_LVAL_PP(category));
1999-04-21 17:26:16 +00:00
RETURN_STRING(msgstr, 1);
}
2000-03-19 23:39:05 +00:00
/* }}} */
1999-04-21 17:26:16 +00:00
2000-03-19 23:39:05 +00:00
/* {{{ proto string bindtextdomain(string domain_name, string dir)
2000-08-08 20:28:19 +00:00
Bind to the text domain domain_name, looking for translations in dir. Returns the current domain */
PHP_FUNCTION(bindtextdomain)
1999-04-21 17:26:16 +00:00
{
zval **domain_name, **dir;
2000-11-27 18:21:48 +00:00
char *retval, dir_name[MAXPATHLEN];
1999-04-21 17:26:16 +00:00
2000-12-08 16:25:38 +00:00
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &domain_name, &dir) == FAILURE) {
1999-04-21 17:26:16 +00:00
WRONG_PARAM_COUNT;
}
convert_to_string_ex(domain_name);
convert_to_string_ex(dir);
1999-04-21 17:26:16 +00:00
if (strcmp(Z_STRVAL_PP(dir), "") && strcmp(Z_STRVAL_PP(dir), "0")) {
VCWD_REALPATH(Z_STRVAL_PP(dir), dir_name);
1999-04-21 17:26:16 +00:00
} else {
VCWD_GETCWD(dir_name, MAXPATHLEN);
1999-04-21 17:26:16 +00:00
}
retval = bindtextdomain(Z_STRVAL_PP(domain_name), dir_name);
1999-04-21 17:26:16 +00:00
RETURN_STRING(retval, 1);
}
2000-03-19 23:39:05 +00:00
/* }}} */
1999-04-21 17:26:16 +00:00
2001-08-29 13:13:24 +00:00
#if HAVE_NGETTEXT
/* {{{ proto string ngettext(string MSGID1, string MSGID2, int N)
2001-08-27 22:26:49 +00:00
Plural version of gettext() */
PHP_FUNCTION(ngettext)
{
zval **msgid1, **msgid2, **count;
char *msgstr;
RETVAL_FALSE;
2001-08-27 22:26:49 +00:00
if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &msgid1, &msgid2, &count) == FAILURE) {
WRONG_PARAM_COUNT;
} else {
convert_to_string_ex(msgid1);
convert_to_string_ex(msgid2);
convert_to_long_ex(count);
2001-08-27 22:26:49 +00:00
msgstr = ngettext(Z_STRVAL_PP(msgid1), Z_STRVAL_PP(msgid2), Z_LVAL_PP(count));
if (msgstr) {
RETVAL_STRING (msgstr, 1);
}
}
}
/* }}} */
2001-08-29 13:13:24 +00:00
#endif
2001-08-29 13:13:24 +00:00
#if HAVE_DNGETTEXT
/* {{{ proto string dngettext (string domain, string msgid1, string msgid2, long count)
2001-08-27 22:26:49 +00:00
Plural version of dgettext() */
PHP_FUNCTION(dngettext)
{
zval **domain, **msgid1, **msgid2, **count;
RETVAL_FALSE;
2001-08-27 22:26:49 +00:00
if (ZEND_NUM_ARGS() != 4 || zend_get_parameters_ex(4, &domain, &msgid1, &msgid2, &count) == FAILURE) {
WRONG_PARAM_COUNT;
} else {
char *msgstr;
convert_to_string_ex(domain);
convert_to_string_ex(msgid1);
convert_to_string_ex(msgid2);
convert_to_long_ex(count);
2001-08-27 22:26:49 +00:00
msgstr = dngettext(Z_STRVAL_PP(domain), Z_STRVAL_PP(msgid1), Z_STRVAL_PP(msgid2), Z_LVAL_PP(count));
if (msgstr) {
RETVAL_STRING(msgstr, 1);
}
}
}
/* }}} */
2001-08-29 13:13:24 +00:00
#endif
2001-08-29 13:13:24 +00:00
#if HAVE_DCNGETTEXT
/* {{{ proto string dcngettext (string domain, string msgid1, string msgid2, long n, int category)
2001-08-27 22:26:49 +00:00
Plural version of dcgettext() */
PHP_FUNCTION(dcngettext)
{
zval **domain, **msgid1, **msgid2, **count, **category;
RETVAL_FALSE;
2001-08-27 22:26:49 +00:00
if (ZEND_NUM_ARGS() != 5 || zend_get_parameters_ex(4, &domain, &msgid1, &msgid2, &count, &category) == FAILURE) {
WRONG_PARAM_COUNT;
} else {
char* msgstr = NULL;
convert_to_string_ex(domain);
convert_to_string_ex(msgid1);
convert_to_string_ex(msgid2);
convert_to_long_ex(count);
convert_to_long_ex(category);
2001-08-27 22:26:49 +00:00
msgstr = dcngettext(Z_STRVAL_PP(domain), Z_STRVAL_PP(msgid1), Z_STRVAL_PP(msgid2), Z_LVAL_PP(count), Z_LVAL_PP(category));
if (msgstr) {
RETVAL_STRING(msgstr, 1);
}
}
}
2001-08-29 13:13:24 +00:00
/* }}} */
#endif
1999-04-21 17:26:16 +00:00
#endif /* HAVE_LIBINTL */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
1999-04-21 17:26:16 +00:00
*/
2001-08-29 13:13:24 +00:00