- making all methods of translation class static

- caching the phrases in new egw_cache on Tree level
--> a good speed improvment on my devel system
- also added a global function
check_load_extension($extension,$throw=false)
This commit is contained in:
Ralf Becker 2009-04-20 11:59:39 +00:00
parent fd9856ebf5
commit bf036043b2
6 changed files with 517 additions and 478 deletions

View File

@ -49,7 +49,7 @@ class egw extends egw_minimal
* @var common * @var common
*/ */
var $common; var $common;
private $cat_cache; private $cat_cache;
/** /**
@ -128,6 +128,7 @@ class egw extends egw_minimal
//$GLOBALS['egw_info']['server'] = config::read('phpgwapi'); would unserialize arrays //$GLOBALS['egw_info']['server'] = config::read('phpgwapi'); would unserialize arrays
// setup the other subclasses // setup the other subclasses
// translation class is here only for backward compatibility, as all it's methods can be called static now
$this->translation = new translation(); $this->translation = new translation();
$this->common = new common(); $this->common = new common();
$this->accounts = accounts::getInstance(); $this->accounts = accounts::getInstance();
@ -154,7 +155,7 @@ class egw extends egw_minimal
$this->check_app_rights(); $this->check_app_rights();
$this->load_optional_classes(); $this->load_optional_classes();
$this->cat_cache =& categories::init_cache(); $this->cat_cache =& categories::init_cache();
} }
else // set the defines for login, in case it's more then just login else // set the defines for login, in case it's more then just login
@ -182,7 +183,7 @@ class egw extends egw_minimal
register_shutdown_function(array($this, 'shutdown')); register_shutdown_function(array($this, 'shutdown'));
$this->define_egw_constants(); $this->define_egw_constants();
categories::init_cache($this->cat_cache); categories::init_cache($this->cat_cache);
} }
@ -200,7 +201,8 @@ class egw extends egw_minimal
{ {
$this->template->set_root(EGW_APP_TPL); $this->template->set_root(EGW_APP_TPL);
} }
$this->translation->add_app($GLOBALS['egw_info']['flags']['currentapp']); // init the translation class, necessary as own wakeup would run before our's
translation::init();
// verify the session // verify the session
$GLOBALS['egw']->verify_session(); $GLOBALS['egw']->verify_session();
@ -439,20 +441,20 @@ class egw extends egw_minimal
/** /**
* Shortcut to translation class * Shortcut to translation class
* *
* This function is a basic wrapper to translation->translate() * This function is a basic wrapper to translation::translate()
* *
* @deprecated only used in the old timetracker * @deprecated only used in the old timetracker
* @param string The key for the phrase * @param string The key for the phrase
* @see translation->translate() * @see translation::translate()
*/ */
function lang($key,$args=null) static function lang($key,$args=null)
{ {
if (!is_array($args)) if (!is_array($args))
{ {
$args = func_get_args(); $args = func_get_args();
array_shift($args); array_shift($args);
} }
return $this->translation->translate($key,$args); return translation::translate($key,$args);
} }
/** /**

View File

@ -38,7 +38,7 @@
* The $app parameter should be either the app or the class name, which both are unique. * The $app parameter should be either the app or the class name, which both are unique.
* *
* The tree and instance wide cache uses a certain provider class, to store the data * The tree and instance wide cache uses a certain provider class, to store the data
* eg. in memcached or if there's nothing else configured in the session. * eg. in memcached or if there's nothing else configured in the filesystem (eGW's temp_dir).
*/ */
class egw_cache class egw_cache
{ {

File diff suppressed because it is too large Load Diff

View File

@ -59,6 +59,28 @@ function array2string($var)
return 'UNKNOWN TYPE!'; return 'UNKNOWN TYPE!';
} }
/**
* Check if a given extension is loaded or load it if possible (requires sometimes disabled dl function)
*
* @param string $extension
* @param boolean $throw=false should we throw an exception, if $extension could not be loaded, default false = no
* @return boolean true if loaded now, false otherwise
*/
function check_load_extension($extension,$throw=false)
{
if (!defined('PHP_SHLIB_PREFIX'))
{
define('PHP_SHLIB_PREFIX',PHP_SHLIB_SUFFIX == 'dll' ? 'php_' : '');
}
$loaded = extension_loaded($extension) || function_exists('dl') && @dl(PHP_SHLIB_PREFIX.$extension.'.'.PHP_SHLIB_SUFFIX);
if (!($loaded))
{
throw new Exception ("PHP extension '$ext' not loaded AND can NOT be loaded via dl('$dl')!");
}
return $loaded;
}
/** /**
* @internal Not to be used directly. Should only be used by print_debug() * @internal Not to be used directly. Should only be used by print_debug()
*/ */
@ -1325,7 +1347,7 @@ if (!function_exists('lang')) // setup declares an own version
$vars = func_get_args(); $vars = func_get_args();
array_shift($vars); // remove $key array_shift($vars); // remove $key
} }
return $GLOBALS['egw']->translation->translate($key,$vars); return translation::translate($key,$vars);
} }
} }
@ -1348,9 +1370,7 @@ function try_lang($key,$vars=null)
$vars = func_get_args(); $vars = func_get_args();
array_shift($vars); // remove $key array_shift($vars); // remove $key
} }
return is_object($GLOBALS['egw']) && isset($GLOBALS['egw']->translations) ? return class_exists(translations,false) ? translation::translate($key,$vars) : str_replace($varnames,$vars,$key);
$GLOBALS['egw']->translation->translate($key,$vars) :
str_replace($varnames,$vars,$key);
} }
/** /**

View File

@ -11,177 +11,152 @@
* @version $Id$ * @version $Id$
*/ */
if (!defined('MAX_MESSAGE_ID_LENGTH')) if (!defined('MAX_MESSAGE_ID_LENGTH'))
{
define('MAX_MESSAGE_ID_LENGTH',128);
}
// Define prefix for langfiles (historically 'phpgw_')
define('EGW_LANGFILE_PREFIX', 'egw_');
class setup_translation
{
var $langarray = array();
var $no_translation_marker = '*';
/**
* constructor for the class, loads all phrases into langarray
*
* @param $lang user lang variable (defaults to en)
*/
function __construct()
{ {
define('MAX_MESSAGE_ID_LENGTH',128); $ConfigLang = get_var('ConfigLang',Array('POST','COOKIE'));
if(!$ConfigLang)
{
$lang = 'en';
}
else
{
$lang = $ConfigLang;
}
$fn = '.' . SEP . 'lang' . SEP . EGW_LANGFILE_PREFIX . $lang . '.lang';
if (!file_exists($fn))
{
$fn = '.' . SEP . 'lang' . SEP . EGW_LANGFILE_PREFIX .'en.lang';
}
if (file_exists($fn) && ($fp = fopen($fn,'r')))
{
while (($data = fgets($fp,8000)))
{
// explode with "\t" and removing "\n" with str_replace, needed to work with mbstring.overload=7
list($message_id,,,$content) = explode("\t",$data);
$this->langarray[strtolower(trim($message_id))] = str_replace("\n",'',$content);
}
fclose($fp);
if (!$GLOBALS['egw_setup']->system_charset)
{
$GLOBALS['egw_setup']->system_charset = $this->langarray['charset'];
}
}
} }
// Define prefix for langfiles (historically 'phpgw_') /**
define('EGW_LANGFILE_PREFIX', 'egw_'); * Translate phrase to user selected lang
*
class setup_translation * @param $key phrase to translate
* @param $vars vars sent to lang function, passed to us
*/
function translate($key, $vars=False)
{ {
var $langarray = array(); static $placeholders = array('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10');
var $no_translation_marker = '*';
/** $ret = $key . $this->no_translation_marker;
* constructor for the class, loads all phrases into langarray $key = strtolower(trim($key));
* if (isset($this->langarray[$key]))
* @param $lang user lang variable (defaults to en)
*/
function setup_translation()
{ {
$ConfigLang = get_var('ConfigLang',Array('POST','COOKIE')); $ret = $this->langarray[$key];
}
if ($GLOBALS['egw_setup']->system_charset != $this->langarray['charset'])
{
$ret = translation::convert($ret,$this->langarray['charset']);
}
if (is_array($vars))
{
$ret = str_replace($placeholders, $vars, $ret);
}
return $ret;
}
if(!$ConfigLang) function get_langs($DEBUG=False)
{
return translaton::get_langs($DEBUG);
}
function drop_langs($appname,$DEBUG=False)
{
return translaton::drop_langs($appname,$DEBUG);
}
function add_langs($appname,$DEBUG=False,$force_langs=False)
{
return translaton::add_langs($appname,$DEBUG,$force_langs);
}
function drop_add_all_langs($langs=False)
{
if (!$langs && !count($langs = translation::get_langs()))
{
$langs[] = 'en';
}
return translation::install_langs($langs,'dumpold');
}
/**
* List availible charsets and it's supported languages
* @param boolean/string $name=false name for selectbox or false to return an array
* @param string $selected selected charset
* @return string/array html for a selectbox or array with charset / languages pairs
*/
function get_charsets($name=false,$selected='')
{
$charsets = array(
'utf-8' => 'utf-8: '.lang('all languages (incl. not listed ones)'),
);
if (($f = fopen('lang/languages','r')))
{
while(($line = fgets($f)) !== false)
{ {
$lang = 'en'; list($lang,$language) = explode("\t",trim($line));
} if ($lang && ($lf = @fopen("../phpgwapi/setup/" . EGW_LANGFILE_PREFIX . "$lang.lang",'r')))
else
{
$lang = $ConfigLang;
}
$fn = '.' . SEP . 'lang' . SEP . EGW_LANGFILE_PREFIX . $lang . '.lang';
if (!file_exists($fn))
{
$fn = '.' . SEP . 'lang' . SEP . EGW_LANGFILE_PREFIX .'en.lang';
}
if (file_exists($fn))
{
$fp = fopen($fn,'r');
while ($data = fgets($fp,8000))
{ {
// explode with "\t" and removing "\n" with str_replace, needed to work with mbstring.overload=7 while(($line = fgets($lf)) !== false)
list($message_id,,,$content) = explode("\t",$data);
$this->langarray[strtolower(trim($message_id))] = str_replace("\n",'',$content);
}
fclose($fp);
if (!$GLOBALS['egw_setup']->system_charset)
{
$GLOBALS['egw_setup']->system_charset = $this->langarray['charset'];
}
}
}
/**
* Translate phrase to user selected lang
*
* @param $key phrase to translate
* @param $vars vars sent to lang function, passed to us
*/
function translate($key, $vars=False)
{
$ret = $key . $this->no_translation_marker;
$key = strtolower(trim($key));
if (isset($this->langarray[$key]))
{
$ret = $this->langarray[$key];
}
if ($GLOBALS['egw_setup']->system_charset != $this->langarray['charset'])
{
if (!is_object($this->sql))
{
$this->setup_translation_sql();
}
$ret = $this->sql->convert($ret,$this->langarray['charset']);
}
if (is_array($vars))
{
foreach($vars as $n => $var)
{
$ret = str_replace( '%'.($n+1), $var, $ret );
}
}
return $ret;
}
// the following functions have been moved to phpgwapi/tanslation_sql
function setup_translation_sql()
{
if (!is_object($this->sql) || is_object($GLOBALS['egw_setup']->db) && !is_object($this->sql->db))
{
include_once(EGW_API_INC.'/class.translation.inc.php');
$this->sql =& new translation;
}
}
function get_langs($DEBUG=False)
{
$this->setup_translation_sql();
return $this->sql->get_langs($DEBUG);
}
function drop_langs($appname,$DEBUG=False)
{
$this->setup_translation_sql();
return $this->sql->drop_langs($appname,$DEBUG);
}
function add_langs($appname,$DEBUG=False,$force_langs=False)
{
$this->setup_translation_sql();
return $this->sql->add_langs($appname,$DEBUG,$force_langs);
}
function drop_add_all_langs($langs=False)
{
$this->setup_translation_sql();
if (!$langs && !count($langs = $this->sql->get_langs()))
{
$langs[] = 'en';
}
return $this->sql->install_langs($langs,'dumpold');
}
/**
* List availible charsets and it's supported languages
* @param boolean/string $name=false name for selectbox or false to return an array
* @param string $selected selected charset
* @return string/array html for a selectbox or array with charset / languages pairs
*/
function get_charsets($name=false,$selected='')
{
$charsets = array(
'utf-8' => 'utf-8: '.lang('all languages (incl. not listed ones)'),
);
if (($f = fopen('lang/languages','r')))
{
while(($line = fgets($f)) !== false)
{
list($lang,$language) = explode("\t",trim($line));
if ($lang && ($lf = @fopen("../phpgwapi/setup/" . EGW_LANGFILE_PREFIX . "$lang.lang",'r')))
{ {
while(($line = fgets($lf)) !== false) list($phrase,,,$charset) = explode("\t",$line);
if ($phrase == 'charset')
{ {
list($phrase,,,$charset) = explode("\t",$line); $charset = trim(strtolower($charset));
if ($phrase == 'charset')
if ($charset != 'utf-8')
{ {
$charset = trim(strtolower($charset)); $charsets[$charset] .= (isset($charsets[$charset]) ? ', ' : $charset.': ') . $language;
if ($charset != 'utf-8')
{
$charsets[$charset] .= (isset($charsets[$charset]) ? ', ' : $charset.': ') . $language;
}
break;
} }
break;
} }
fclose($lf);
} }
fclose($lf);
} }
fclose($f);
} }
if (!$name) fclose($f);
{ }
return $charsets; if (!$name)
} {
$html =& CreateObject('phpgwapi.html'); return $charsets;
}
return $html->select($name,trim(strtolower($selected)),$charsets,true); return html::select($name,trim(strtolower($selected)),$charsets,true);
}
} }
?> }

View File

@ -22,11 +22,9 @@ $GLOBALS['egw_setup']->loaddb();
if (@$_POST['submit']) if (@$_POST['submit'])
{ {
$GLOBALS['egw_setup']->translation->setup_translation_sql(); translation::install_langs(@$_POST['lang_selected'],@$_POST['upgrademethod']);
$GLOBALS['egw_setup']->translation->sql->install_langs(@$_POST['lang_selected'],@$_POST['upgrademethod']);
if(!translation::$line_rejected )
if( !$GLOBALS['egw_setup']->translation->sql->line_rejected )
{ {
Header('Location: index.php'); Header('Location: index.php');
exit; exit;
@ -89,10 +87,10 @@ else
} }
// Rejected Lines // Rejected Lines
if($_POST['debug'] && count($GLOBALS['egw_setup']->translation->sql->line_rejected)) if($_POST['debug'] && count(translation::$line_rejected))
{ {
$str = ''; $str = '';
foreach($GLOBALS['egw_setup']->translation->sql->line_rejected as $badline) foreach(translation::$line_rejected as $badline)
{ {
$_f_buffer = split("[/\\]", $badline['appfile']); $_f_buffer = split("[/\\]", $badline['appfile']);
$str .= lang('Application: %1, File: %2, Line: "%3"','<b>'.$_f_buffer[count($_f_buffer)-3].'</b>', $str .= lang('Application: %1, File: %2, Line: "%3"','<b>'.$_f_buffer[count($_f_buffer)-3].'</b>',