php-src/ext/gettext/gettext.c

322 lines
7.8 KiB
C
Raw Normal View History

1999-04-21 17:26:16 +00:00
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
1999-04-21 17:26:16 +00:00
+----------------------------------------------------------------------+
2006-01-01 12:51:34 +00:00
| This source file is subject to version 3.01 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 through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.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> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
1999-04-21 17:26:16 +00:00
#include "php.h"
#ifdef HAVE_LIBINTL
1999-04-21 17:26:16 +00:00
#include <stdio.h>
#include <locale.h>
#include "ext/standard/info.h"
#include "php_gettext.h"
#include "gettext_arginfo.h"
2006-06-14 21:36:10 +00:00
#include <libintl.h>
1999-12-17 21:50:07 +00:00
zend_module_entry php_gettext_module_entry = {
STANDARD_MODULE_HEADER,
2001-08-27 22:26:49 +00:00
"gettext",
ext_functions,
2001-08-27 22:26:49 +00:00
NULL,
NULL,
NULL,
NULL,
PHP_MINFO(php_gettext),
PHP_GETTEXT_VERSION,
2001-08-27 22:26:49 +00:00
STANDARD_MODULE_PROPERTIES
1999-04-21 17:26:16 +00:00
};
#ifdef COMPILE_DL_GETTEXT
ZEND_GET_MODULE(php_gettext)
#endif
#define PHP_GETTEXT_MAX_DOMAIN_LENGTH 1024
2008-10-30 14:22:50 +00:00
#define PHP_GETTEXT_MAX_MSGID_LENGTH 4096
#define PHP_GETTEXT_DOMAIN_LENGTH_CHECK(_arg_num, domain_len) \
2015-05-15 14:57:17 +00:00
if (UNEXPECTED(domain_len > PHP_GETTEXT_MAX_DOMAIN_LENGTH)) { \
zend_argument_value_error(_arg_num, "is too long"); \
RETURN_THROWS(); \
} else if (domain_len == 0) { \
zend_argument_must_not_be_empty_error(_arg_num); \
RETURN_THROWS(); \
}
#define PHP_GETTEXT_LENGTH_CHECK(_arg_num, check_len) \
2015-05-15 14:57:17 +00:00
if (UNEXPECTED(check_len > PHP_GETTEXT_MAX_MSGID_LENGTH)) { \
zend_argument_value_error(_arg_num, "is too long"); \
RETURN_THROWS(); \
2008-10-30 14:22:50 +00:00
}
#define PHP_DCGETTEXT_CATEGORY_CHECK(_arg_num, category) \
if (category == LC_ALL) { \
zend_argument_value_error(_arg_num, "cannot be LC_ALL"); \
RETURN_THROWS(); \
}
PHP_MINFO_FUNCTION(php_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
}
/* {{{ Set the textdomain to "domain". Returns the current domain */
PHP_FUNCTION(textdomain)
1999-04-21 17:26:16 +00:00
{
char *domain_name = NULL, *retval = NULL;
zend_string *domain = NULL;
1999-04-21 17:26:16 +00:00
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_STR_OR_NULL(domain)
ZEND_PARSE_PARAMETERS_END();
1999-04-21 17:26:16 +00:00
if (domain != NULL) {
PHP_GETTEXT_DOMAIN_LENGTH_CHECK(1, ZSTR_LEN(domain))
if (zend_string_equals_literal(domain, "0")) {
zend_argument_value_error(1, "cannot be zero");
RETURN_THROWS();
}
domain_name = ZSTR_VAL(domain);
1999-04-21 17:26:16 +00:00
}
retval = textdomain(domain_name);
2014-05-05 06:32:32 +00:00
RETURN_STRING(retval);
1999-04-21 17:26:16 +00:00
}
2000-03-19 23:39:05 +00:00
/* }}} */
1999-04-21 17:26:16 +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
{
char *msgstr = NULL;
zend_string *msgid;
1999-04-21 17:26:16 +00:00
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STR(msgid)
ZEND_PARSE_PARAMETERS_END();
1999-04-21 17:26:16 +00:00
PHP_GETTEXT_LENGTH_CHECK(1, ZSTR_LEN(msgid))
msgstr = gettext(ZSTR_VAL(msgid));
1999-04-21 17:26:16 +00:00
if (msgstr != ZSTR_VAL(msgid)) {
RETURN_STRING(msgstr);
} else {
RETURN_STR_COPY(msgid);
}
1999-04-21 17:26:16 +00:00
}
2000-03-19 23:39:05 +00:00
/* }}} */
1999-04-21 17:26:16 +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
{
char *msgstr = NULL;
zend_string *domain, *msgid;
1999-04-21 17:26:16 +00:00
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_STR(domain)
Z_PARAM_STR(msgid)
ZEND_PARSE_PARAMETERS_END();
1999-04-21 17:26:16 +00:00
PHP_GETTEXT_DOMAIN_LENGTH_CHECK(1, ZSTR_LEN(domain))
PHP_GETTEXT_LENGTH_CHECK(2, ZSTR_LEN(msgid))
msgstr = dgettext(ZSTR_VAL(domain), ZSTR_VAL(msgid));
1999-04-21 17:26:16 +00:00
if (msgstr != ZSTR_VAL(msgid)) {
RETURN_STRING(msgstr);
} else {
RETURN_STR_COPY(msgid);
}
1999-04-21 17:26:16 +00:00
}
2000-03-19 23:39:05 +00:00
/* }}} */
1999-04-21 17:26:16 +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
{
char *msgstr = NULL;
zend_string *domain, *msgid;
2014-08-25 17:24:55 +00:00
zend_long category;
1999-04-21 17:26:16 +00:00
ZEND_PARSE_PARAMETERS_START(3, 3)
Z_PARAM_STR(domain)
Z_PARAM_STR(msgid)
Z_PARAM_LONG(category)
ZEND_PARSE_PARAMETERS_END();
1999-04-21 17:26:16 +00:00
PHP_GETTEXT_DOMAIN_LENGTH_CHECK(1, ZSTR_LEN(domain))
PHP_GETTEXT_LENGTH_CHECK(2, ZSTR_LEN(msgid))
PHP_DCGETTEXT_CATEGORY_CHECK(3, category)
msgstr = dcgettext(ZSTR_VAL(domain), ZSTR_VAL(msgid), category);
1999-04-21 17:26:16 +00:00
if (msgstr != ZSTR_VAL(msgid)) {
RETURN_STRING(msgstr);
} else {
RETURN_STR_COPY(msgid);
}
1999-04-21 17:26:16 +00:00
}
2000-03-19 23:39:05 +00:00
/* }}} */
1999-04-21 17:26:16 +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
{
zend_string *domain, *dir = NULL;
2000-11-27 18:21:48 +00:00
char *retval, dir_name[MAXPATHLEN];
1999-04-21 17:26:16 +00:00
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_STR(domain)
Z_PARAM_OPTIONAL
Z_PARAM_STR_OR_NULL(dir)
ZEND_PARSE_PARAMETERS_END();
PHP_GETTEXT_DOMAIN_LENGTH_CHECK(1, ZSTR_LEN(domain))
if (dir == NULL) {
RETURN_STRING(bindtextdomain(ZSTR_VAL(domain), NULL));
}
if (ZSTR_LEN(dir) != 0 && !zend_string_equals_literal(dir, "0")) {
if (!VCWD_REALPATH(ZSTR_VAL(dir), dir_name)) {
RETURN_FALSE;
}
} else if (!VCWD_GETCWD(dir_name, MAXPATHLEN)) {
RETURN_FALSE;
1999-04-21 17:26:16 +00:00
}
retval = bindtextdomain(ZSTR_VAL(domain), dir_name);
1999-04-21 17:26:16 +00:00
2014-05-05 06:32:32 +00:00
RETURN_STRING(retval);
1999-04-21 17:26:16 +00:00
}
2000-03-19 23:39:05 +00:00
/* }}} */
1999-04-21 17:26:16 +00:00
#ifdef HAVE_NGETTEXT
/* {{{ Plural version of gettext() */
PHP_FUNCTION(ngettext)
{
char *msgstr = NULL;
zend_string *msgid1, *msgid2;
2014-08-25 17:24:55 +00:00
zend_long count;
ZEND_PARSE_PARAMETERS_START(3, 3)
Z_PARAM_STR(msgid1)
Z_PARAM_STR(msgid2)
Z_PARAM_LONG(count)
ZEND_PARSE_PARAMETERS_END();
PHP_GETTEXT_LENGTH_CHECK(1, ZSTR_LEN(msgid1))
PHP_GETTEXT_LENGTH_CHECK(2, ZSTR_LEN(msgid2))
2008-10-30 14:22:50 +00:00
msgstr = ngettext(ZSTR_VAL(msgid1), ZSTR_VAL(msgid2), count);
ZEND_ASSERT(msgstr);
RETURN_STRING(msgstr);
}
/* }}} */
2001-08-29 13:13:24 +00:00
#endif
#ifdef HAVE_DNGETTEXT
/* {{{ Plural version of dgettext() */
PHP_FUNCTION(dngettext)
{
char *msgstr = NULL;
zend_string *domain, *msgid1, *msgid2;
2014-08-25 17:24:55 +00:00
zend_long count;
2008-10-30 14:22:50 +00:00
ZEND_PARSE_PARAMETERS_START(4, 4)
Z_PARAM_STR(domain)
Z_PARAM_STR(msgid1)
Z_PARAM_STR(msgid2)
Z_PARAM_LONG(count)
ZEND_PARSE_PARAMETERS_END();
PHP_GETTEXT_DOMAIN_LENGTH_CHECK(1, ZSTR_LEN(domain))
PHP_GETTEXT_LENGTH_CHECK(2, ZSTR_LEN(msgid1))
PHP_GETTEXT_LENGTH_CHECK(3, ZSTR_LEN(msgid2))
msgstr = dngettext(ZSTR_VAL(domain), ZSTR_VAL(msgid1), ZSTR_VAL(msgid2), count);
ZEND_ASSERT(msgstr);
RETURN_STRING(msgstr);
}
/* }}} */
2001-08-29 13:13:24 +00:00
#endif
#ifdef HAVE_DCNGETTEXT
/* {{{ Plural version of dcgettext() */
PHP_FUNCTION(dcngettext)
{
char *msgstr = NULL;
zend_string *domain, *msgid1, *msgid2;
2014-08-25 17:24:55 +00:00
zend_long count, category;
RETVAL_FALSE;
ZEND_PARSE_PARAMETERS_START(5, 5)
Z_PARAM_STR(domain)
Z_PARAM_STR(msgid1)
Z_PARAM_STR(msgid2)
Z_PARAM_LONG(count)
Z_PARAM_LONG(category)
ZEND_PARSE_PARAMETERS_END();
PHP_GETTEXT_DOMAIN_LENGTH_CHECK(1, ZSTR_LEN(domain))
PHP_GETTEXT_LENGTH_CHECK(2, ZSTR_LEN(msgid1))
PHP_GETTEXT_LENGTH_CHECK(3, ZSTR_LEN(msgid2))
PHP_DCGETTEXT_CATEGORY_CHECK(5, category)
msgstr = dcngettext(ZSTR_VAL(domain), ZSTR_VAL(msgid1), ZSTR_VAL(msgid2), count, category);
ZEND_ASSERT(msgstr);
RETURN_STRING(msgstr);
}
2001-08-29 13:13:24 +00:00
/* }}} */
#endif
#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
/* {{{ Specify the character encoding in which the messages from the DOMAIN message catalog will be returned. */
PHP_FUNCTION(bind_textdomain_codeset)
{
char *retval = NULL;
zend_string *domain, *codeset = NULL;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_STR(domain)
Z_PARAM_OPTIONAL
Z_PARAM_STR_OR_NULL(codeset)
ZEND_PARSE_PARAMETERS_END();
PHP_GETTEXT_DOMAIN_LENGTH_CHECK(1, ZSTR_LEN(domain))
retval = bind_textdomain_codeset(ZSTR_VAL(domain), codeset ? ZSTR_VAL(codeset) : NULL);
2008-06-20 18:00:10 +00:00
if (!retval) {
RETURN_FALSE;
}
2014-05-05 06:32:32 +00:00
RETURN_STRING(retval);
}
/* }}} */
#endif
1999-04-21 17:26:16 +00:00
#endif /* HAVE_LIBINTL */