From 7e22bf13475c7adbc06d3c376720f6bee1cd7b0a Mon Sep 17 00:00:00 2001 From: Ralf Becker Date: Tue, 1 Apr 2008 10:47:50 +0000 Subject: [PATCH] "New method try_lang(), usefull for exception handlers or early stages of the initialisation of the egw object, as calling lang would try to load the translations, evtl. cause more errors, eg. because there's no db-connection." --- phpgwapi/inc/common_functions.inc.php | 38 ++++++++++++++++++++------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/phpgwapi/inc/common_functions.inc.php b/phpgwapi/inc/common_functions.inc.php index a1264f401a..cc2394cd4d 100755 --- a/phpgwapi/inc/common_functions.inc.php +++ b/phpgwapi/inc/common_functions.inc.php @@ -1279,18 +1279,38 @@ */ function lang($key,$vars=null) { - static $varnames = array('%1','%2','%3','%4'); - if(!is_array($vars)) { $vars = func_get_args(); array_shift($vars); // remove $key } - return is_object($GLOBALS['egw']) && isset($GLOBALS['egw']->translations) ? - $GLOBALS['egw']->translation->translate($key,$vars) : - str_replace($varnames,$vars,$key); + return $GLOBALS['egw']->translation->translate($key,$vars); } } + + /** + * Translate message only if translation object is already loaded + * + * This function is usefull for exception handlers or early stages of the initialisation of the egw object, + * as calling lang would try to load the translations, evtl. cause more errors, eg. because there's no db-connection. + * + * @param string $key message in englich with %1, %2, ... placeholders + * @param string $vars=null multiple values to replace the placeholders + * @return string translated message with placeholders replaced + */ + function try_lang($key,$vars=null) + { + static $varnames = array('%1','%2','%3','%4'); + + if(!is_array($vars)) + { + $vars = func_get_args(); + array_shift($vars); // remove $key + } + return is_object($GLOBALS['egw']) && isset($GLOBALS['egw']->translations) ? + $GLOBALS['egw']->translation->translate($key,$vars) : + str_replace($varnames,$vars,$key); + } /** * php5 autoload function for eGroupWare understanding the following naming schema: @@ -1349,15 +1369,15 @@ function egw_exception_handler(Exception $e) { if ($e instanceof egw_exception_no_permission) { - $headline = lang('Permission denied!'); + $headline = try_lang('Permission denied!'); } elseif ($e instanceof egw_exception_db) { - $headline = lang('Database error'); + $headline = try_lang('Database error'); } else { - $headline = lang('An error happend'); + $headline = try_lang('An error happend'); } $message = '

'.$headline."

\n". '
'.$e->getMessage()."\n\n".
@@ -1365,7 +1385,7 @@ function egw_exception_handler(Exception $e)
 
 	if (is_object($GLOBALS['egw']) && isset($GLOBALS['egw']->session))
 	{
-		'

'.lang('Click here to resume your eGroupWare Session.').'

'; + '

'.try_lang('Click here to resume your eGroupWare Session.').'

'; } if (is_object($GLOBALS['egw']) && isset($GLOBALS['egw']->framework)) {