From: tokul Date: Sun, 31 Jul 2005 12:56:52 +0000 (+0000) Subject: adding dgettext and dngettext function support and additional $l10n array safety... X-Git-Url: https://vcs.fsf.org/?p=squirrelmail.git;a=commitdiff_plain;h=a9a10f57ab2fce55033ccef09a98d05919f8bbe7 adding dgettext and dngettext function support and additional $l10n array safety checks. git-svn-id: https://svn.code.sf.net/p/squirrelmail/code/trunk/squirrelmail@9853 7612ce4b-ef26-0410-bec9-ea0150e637f0 --- diff --git a/functions/gettext.php b/functions/gettext.php index 6fc7884d..e95e80ba 100644 --- a/functions/gettext.php +++ b/functions/gettext.php @@ -32,7 +32,10 @@ include_once(SM_PATH . 'functions/ngettext.php'); */ function _($str) { global $l10n, $gettext_domain; - if ($l10n[$gettext_domain]->error==1) return $str; + if (! isset($l10n[$gettext_domain]) || + ! is_object($l10n[$gettext_domain]) || + $l10n[$gettext_domain]->error==1) + return $str; return $l10n[$gettext_domain]->translate($str); } @@ -74,4 +77,28 @@ function textdomain($name = false) { if ($name) $gettext_domain=$name; return $gettext_domain; } + +/** + * Safety check. + * Setup where three standard gettext functions don't exist and dgettext() exists. + */ +if (! function_exists('dgettext')) { + /** + * Alternative php dgettext function + * + * @link http://www.php.net/function.dgettext + * @param string $domain Gettext domain + * @param string $str English string + * @return string translated string + * @since 1.5.1 + */ + function dgettext($domain, $str) { + global $l10n; + if (! isset($l10n[$domain]) || + ! is_object($l10n[$domain]) || + $l10n[$domain]->error==1) + return $str; + return $l10n[$domain]->translate($str); + } +} ?> \ No newline at end of file diff --git a/functions/i18n.php b/functions/i18n.php index a04aa6a1..be07df93 100644 --- a/functions/i18n.php +++ b/functions/i18n.php @@ -1297,5 +1297,23 @@ elseif ($gettext_flags == 0) { } } } + if (! function_exists('dgettext')) { + /** + * Replacement for broken setups. + * @ignore + */ + function dgettext($domain,$str) { + return $str; + } + } + if (! function_exists('dngettext')) { + /** + * Replacement for broken setups + * @ignore + */ + function dngettext($domain,$str1,$strn,$number) { + return ($number==1 ? $str1 : $strn); + } + } } ?> \ No newline at end of file diff --git a/functions/ngettext.php b/functions/ngettext.php index 0c7279a0..6bbecdcd 100644 --- a/functions/ngettext.php +++ b/functions/ngettext.php @@ -30,7 +30,43 @@ */ function ngettext($single, $plural, $number) { global $l10n, $gettext_domain; - if ($l10n[$gettext_domain]->error==1) return $single; + if (! isset($l10n[$gettext_domain]) || + ! is_object($l10n[$gettext_domain]) || + $l10n[$gettext_domain]->error==1) + return ($number==1 ? $single : $plural); return $l10n[$gettext_domain]->ngettext($single, $plural, $number); } + +/** + * safety check. + * freaky setup where ngettext is not available and dngettext is available. + */ +if (! function_exists('dngettext')) { + /** + * internal dngettext wrapper. + * + * provides dngettext support + * @since 1.5.1 + * @link http://www.php.net/function.dngettext + * @param string $domain Gettext domain + * @param string $single English string, singular form + * @param string $plural English string, plural form + * @param integer $number number that shows used quantity + * @return string translated string + */ + function dngettext($domain, $single, $plural, $number) { + global $l10n; + // Make sure that $number is integer + $number = (int) $number; + + // Make sure that domain is initialized + if (! isset($l10n[$domain]) || + ! is_object($l10n[$domain]) || + $l10n[$domain]->error==1) + return ($number==1 ? $single : $plural); + + // use ngettext class function + return $l10n[$domain]->ngettext($single, $plural, $number); + } +} ?> \ No newline at end of file