From: Tomas K. <to...@us...> - 2004-10-04 14:14:59
|
Update of /cvsroot/squirrelmail/squirrelmail/functions In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30792 Modified Files: i18n.php gettext.php Added Files: ngettext.php Log Message: changing SM internal gettext functions to use php-gettext classes. Fix for bug. 1019007. --- NEW FILE --- <?php /** * SquirrelMail internal ngettext functions * * Uses php-gettext classes * * Copyright (c) 2004 The SquirrelMail Project Team * Licensed under the GNU GPL. For full terms see the file COPYING. * * @copyright (c) 2004 The SquirrelMail Project Team * @license http://opensource.org/licenses/gpl-license.php GNU Public Licens * @link http://www.php.net/gettext Original php gettext manual * @link http://savannah.nongnu.org/projects/php-gettext php-gettext classes * @version $Id: ngettext.php,v 1.1 2004/10/04 14:14:39 tokul Exp $ * @package squirrelmail * @subpackage i18n */ /** * internal ngettext wrapper. * * provides ngettext support * @link http://www.php.net/function.ngettext * @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 ngettext($single, $plural, $number) { global $l10n, $gettext_domain; if ($l10n[$gettext_domain]->error==1) return $single; return $l10n[$gettext_domain]->ngettext($single, $plural, $number); } ?> Index: i18n.php =================================================================== RCS file: /cvsroot/squirrelmail/squirrelmail/functions/i18n.php,v retrieving revision 1.189 retrieving revision 1.190 diff -u -w -r1.189 -r1.190 --- i18n.php 28 Sep 2004 17:17:02 -0000 1.189 +++ i18n.php 4 Oct 2004 14:14:39 -0000 1.190 @@ -21,6 +21,44 @@ require_once(SM_PATH . 'functions/global.php'); /** + * Gettext bindtextdomain wrapper. + * + * Wrapper solves differences between php versions in order to provide + * ngettext support. Should be used if translation uses ngettext + * functions. + * @param string $domain gettext domain name + * @param string $dir directory that contains all translations + * @return string path to translation directory + */ +function sq_bindtextdomain($domain,$dir) { + global $l10n, $gettext_flags, $sm_notAlias; + + if ($gettext_flags==7) { + // gettext extension without ngettext + if (substr($dir, -1) != '/') $dir .= '/'; + $mofile=$dir . $sm_notAlias . '/LC_MESSAGES/' . $domain . '.mo'; + $input = new FileReader($mofile); + $l10n[$domain] = new gettext_reader($input); + } + + $dir=bindtextdomain($domain,$dir); + + return $dir; +} + +/** + * Gettext textdomain wrapper. + * Makes sure that gettext_domain global is modified. + * @param string $name gettext domain name + * @return string gettext domain name + */ +function sq_textdomain($domain) { + global $gettext_domain; + $gettext_domain=textdomain($domain); + return $gettext_domain; +} + +/** * Converts string from given charset to charset, that can be displayed by user translation. * * Function by default returns html encoded strings, if translation uses different encoding. @@ -231,8 +269,8 @@ $use_gettext && $sm_language != '' && isset($languages[$sm_notAlias]['CHARSET']) ) { - bindtextdomain( 'squirrelmail', SM_PATH . 'locale/' ); - textdomain( 'squirrelmail' ); + sq_bindtextdomain( 'squirrelmail', SM_PATH . 'locale/' ); + sq_textdomain( 'squirrelmail' ); if (function_exists('bind_textdomain_codeset')) { if ($sm_notAlias == 'ja_JP') { bind_textdomain_codeset ("squirrelmail", 'EUC-JP'); @@ -652,11 +690,23 @@ if (function_exists('textdomain')) { $gettext_flags += 4; } +if (function_exists('ngettext')) { + $gettext_flags += 8; +} /* If gettext is fully loaded, cool */ -if ($gettext_flags == 7) { +if ($gettext_flags == 15) { + $use_gettext = true; +} + +/* If ngettext support is missing, load it */ +elseif ($gettext_flags == 7) { $use_gettext = true; + // load internal ngettext functions + include_once(SM_PATH . 'class/l10n.class.php'); + include_once(SM_PATH . 'functions/ngettext.php'); } + /* If we can fake gettext, try that */ elseif ($gettext_flags == 0) { $use_gettext = true; @@ -690,6 +740,19 @@ return; } } + if (! $gettext_flags & 8) { + /** + * Function is used as replacemet in broken installs + * @ignore + */ + function ngettext($str,$str2,$number) { + if ($number>1) { + return $str2; + } else { + return $str; + } + } + } } Index: gettext.php =================================================================== RCS file: /cvsroot/squirrelmail/squirrelmail/functions/gettext.php,v retrieving revision 1.23 retrieving revision 1.24 diff -u -w -r1.23 -r1.24 --- gettext.php 12 Apr 2004 08:03:38 -0000 1.23 +++ gettext.php 4 Oct 2004 14:14:48 -0000 1.24 @@ -2,16 +2,11 @@ /** * SquirrelMail internal gettext functions * - * Copyright (c) 1999-2004 The SquirrelMail Project Team - * Licensed under the GNU GPL. For full terms see the file COPYING. - * - * Alternate to the system's built-in gettext. - * relies on .po files (can't read .mo easily). - * Uses the session for caching (speed increase) - * Possible use in other PHP scripts? The only SM-specific thing is - * $sm_language, I think - * + * Uses php-gettext classes + * @copyright (c) 1999-2004 The SquirrelMail Project Team + * @license http://opensource.org/licenses/gpl-license.php GNU Public Licens * @link http://www.php.net/gettext Original php gettext manual + * @link http://savannah.nongnu.org/projects/php-gettext php-gettext classes * @version $Id$ * @package squirrelmail * @subpackage i18n @@ -20,135 +15,9 @@ /** Almost everything requires global.php... */ require_once(SM_PATH . 'functions/global.php'); -global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded, - $gettext_php_translateStrings, $gettext_php_loaded_language, - $gettext_php_short_circuit; - -if (! isset($gettext_php_loaded)) { - $gettext_php_loaded = false; - sqsession_register($gettext_php_loaded, 'gettext_php_loaded'); -} -if (! isset($gettext_php_domain)) { - $gettext_php_domain = ''; - sqsession_register($gettext_php_domain, 'gettext_php_domain'); -} -if (! isset($gettext_php_dir)) { - $gettext_php_dir = ''; - sqsession_register($gettext_php_dir, 'gettext_php_dir'); -} -if (! isset($gettext_php_translateStrings)) { - $gettext_php_translateStrings = array(); - sqsession_register($gettext_php_translateStrings, 'gettext_php_translateStrings'); -} -if (! isset($gettext_php_loaded_language)) { - $gettext_php_loaded_language = ''; - sqsession_register($gettext_php_loaded_language, 'gettext_php_loaded_language'); -} -if (! isset($gettext_php_short_circuit)) { - $gettext_php_short_circuit = false; - sqsession_register($gettext_php_short_circuit, 'gettext_php_short_circuit'); -} - -/** - * Converts .po file into array and stores it in session. - * - * Used internally by _($str) function - * - * @internal function is used internally by functions/gettext.php code - */ -function gettext_php_load_strings() { - global $squirrelmail_language, $gettext_php_translateStrings, - $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded, - $gettext_php_loaded_language, $gettext_php_short_circuit; - - /* - * $squirrelmail_language gives 'en' for English, 'de' for German, - * etc. I didn't wanna use getenv or similar, but you easily could - * change my code to do that. - */ - - $gettext_php_translateStrings = array(); - - $gettext_php_short_circuit = false; /* initialization */ - - $filename = $gettext_php_dir; - if (substr($filename, -1) != '/') - $filename .= '/'; - $filename .= $squirrelmail_language . '/LC_MESSAGES/' . - $gettext_php_domain . '.po'; - - $file = @fopen($filename, 'r'); - if ($file == false) { - /* Uh-ho -- we can't load the file. Just fake it. :-) - This is also for English, which doesn't use translations */ - $gettext_php_loaded = true; - $gettext_php_loaded_language = $squirrelmail_language; - /* Avoid fuzzy matching when we didn't load strings */ - $gettext_php_short_circuit = true; - return; - } - - $key = ''; - $SkipRead = false; - while (! feof($file)) { - if (! $SkipRead) { - $line = trim(fgets($file, 4096)); - } else { - $SkipRead = false; - } - - if (ereg('^msgid "(.*)"$', $line, $match)) { - if ($match[1] == '') { - /* - * Potential multi-line - * msgid "" - * "string string " - * "string string" - */ - $key = ''; - $line = trim(fgets($file, 4096)); - while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) { - $key .= $match[1]; - $line = trim(fgets($file, 4096)); - } - $SkipRead = true; - } else { - /* msgid "string string" */ - $key = $match[1]; - } - } elseif (ereg('^msgstr "(.*)"$', $line, $match)) { - if ($match[1] == '') { - /* - * Potential multi-line - * msgstr "" - * "string string " - * "string string" - */ - $gettext_php_translateStrings[$key] = ''; - $line = trim(fgets($file, 4096)); - while (ereg('^[ ]*"(.*)"[ ]*$', $line, $match)) { - $gettext_php_translateStrings[$key] .= $match[1]; - $line = trim(fgets($file, 4096)); - } - $SkipRead = true; - } else { - /* msgstr "string string" */ - $gettext_php_translateStrings[$key] = $match[1]; - } - $gettext_php_translateStrings[$key] = - stripslashes($gettext_php_translateStrings[$key]); - /* If there is no translation, just use the untranslated string */ - if ($gettext_php_translateStrings[$key] == '') { - $gettext_php_translateStrings[$key] = $key; - } - $key = ''; - } - } - fclose($file); - - $gettext_php_loaded = true; - $gettext_php_loaded_language = $squirrelmail_language; -} +/** Load classes and other functions */ +include_once(SM_PATH . 'class/l10n.class.php'); +include_once(SM_PATH . 'functions/ngettext.php'); /** * Alternative php gettext function (short form) @@ -159,49 +28,9 @@ * @return string translated string */ function _($str) { - global $gettext_php_loaded, $gettext_php_translateStrings, - $squirrelmail_language, $gettext_php_loaded_language, - $gettext_php_short_circuit; - - if (! $gettext_php_loaded || - $gettext_php_loaded_language != $squirrelmail_language) { - gettext_php_load_strings(); - } - - /* Try finding the exact string */ - if (isset($gettext_php_translateStrings[$str])) { - return $gettext_php_translateStrings[$str]; - } - - /* See if we should short-circuit */ - if ($gettext_php_short_circuit) { - $gettext_php_translateStrings[$str] = $str; - return $str; - } - - /* Look for a string that is very close to the one we want - Very computationally expensive */ - $oldPercent = 0; - $oldStr = ''; - $newPercent = 0; - foreach ($gettext_php_translateStrings as $k => $v) { - similar_text($str, $k, $newPercent); - if ($newPercent > $oldPercent) { - $oldStr = $v; - $oldPercent = $newPercent; - } - } - /* Require 80% match or better - Adjust to suit your needs */ - if ($oldPercent > 80) { - /* Remember this so we don't need to search again */ - $gettext_php_translateStrings[$str] = $oldStr; - return $oldStr; - } - - /* Remember this so we don't need to search again */ - $gettext_php_translateStrings[$str] = $str; - return $str; + global $l10n, $gettext_domain; + if ($l10n[$gettext_domain]->error==1) return $str; + return $l10n[$gettext_domain]->translate($str); } /** @@ -210,21 +39,17 @@ * Sets path to directory containing domain translations * * @link http://www.php.net/function.bindtextdomain - * @param string $name gettext domain name + * @param string $domain gettext domain name * @param string $dir directory that contains all translations * @return string path to translation directory */ -function bindtextdomain($name, $dir) { - global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded; +function bindtextdomain($domain, $dir) { + global $l10n, $sm_notAlias; + if (substr($dir, -1) != '/') $dir .= '/'; + $mofile=$dir . $sm_notAlias . '/LC_MESSAGES/' . $domain . '.mo'; - if ($gettext_php_domain != $name) { - $gettext_php_domain = $name; - $gettext_php_loaded = false; - } - if ($gettext_php_dir != $dir) { - $gettext_php_dir = $dir; - $gettext_php_loaded = false; - } + $input = new FileReader($mofile); + $l10n[$domain] = new gettext_reader($input); return $dir; } @@ -239,13 +64,8 @@ * @return string gettext domain name */ function textdomain($name = false) { - global $gettext_php_domain, $gettext_php_loaded; - - if ($name != false && $gettext_php_domain != $name) { - $gettext_php_domain = $name; - $gettext_php_loaded = false; - } - - return $gettext_php_domain; + global $gettext_domain; + if ($name) $gettext_domain=$name; + return $gettext_domain; } ?> \ No newline at end of file |