Tweak to work more like the sql version (no more global lang var)

This commit is contained in:
Miles Lott 2002-05-20 00:45:52 +00:00
parent a0e48a1c56
commit 01aff05336

View File

@ -22,15 +22,22 @@
/* $Id$ */
// This should be considered experimental. It works, at the app level.
// But, for admin and prefs it really slows things down. See the note
// in the translate() function.
// To use, set $GLOBALS['phpgw_info']["server"]["translation_system"] = "file"; in
// class.translation.inc.php
class translation
{
var $langarray; // Currently loaded translations
var $loaded_apps = array(); // Loaded app langs
var $lang; // Currently loaded translations
var $loaded = False;
var $all_loaded = False;
function translation()
{
if(isset($GLOBALS['phpgw_info']['user']['preferences']['common']['lang']) &&
$GLOBALS['phpgw_info']['user']['preferences']['common']['lang'])
{
$this->userlang = $GLOBALS['phpgw_info']['user']['preferences']['common']['lang'];
}
$this->currentapp = $GLOBALS['phpgw_info']['flags']['currentapp'];
}
/*!
@function translate
@ -38,60 +45,31 @@
@param $key phrase to translate
@param $vars vars sent to lang function, passed to us
*/
function translation($appname='phpgwapi',$loadlang='')
function translate($key,$vars=False)
{
global $lang;
if($loadlang)
{
$lang = $loadlang;
}
$this->add_app($appname,$lang);
}
function translate($key, $vars=False)
{
global $lang;
if (!@in_array($GLOBALS['phpgw_info']['flags']['currentapp'],$this->loaded_apps) &&
$GLOBALS['phpgw_info']['flags']['currentapp'] != 'home')
{
//echo '<br>loading app "' . $GLOBALS['phpgw_info']['flags']['currentapp'] . '" for the first time.';
$this->add_app($GLOBALS['phpgw_info']['flags']['currentapp'],$lang);
}
elseif ($GLOBALS['phpgw_info']['flags']['currentapp'] == 'admin' ||
$GLOBALS['phpgw_info']['flags']['currentapp'] == 'preferences')
{
// This is done because for these two apps, all langs must be loaded.
// Note we cannot load for navbar, since it would slow down EVERY page.
// This is true until all common/admin/prefs langs are in the api file only.
@ksort($GLOBALS['phpgw_info']['apps']);
while(list($x,$app) = each($GLOBALS['phpgw_info']['apps']))
{
if (!@in_array($app['name'],$this->loaded_apps))
{
//echo '<br>loading app "' . $app['name'] . '" for the first time.';
$this->add_app($app['name'],$lang);
}
}
}
if (!$vars)
if(!$vars)
{
$vars = array();
}
$ret = $key;
$_key = strtolower($key);
$ret = $key;
if(!@isset($this->lang[$_key]) && !$this->loaded)
{
$this->load_langs();
}
if(!@isset($this->lang[$_key]) &&
($this->currentapp == 'admin' || $this->currentapp == 'preferences') &&
!$this->all_loaded
)
{
$this->load_langs(True);
}
$ret = @isset($this->lang[$_key]) ? $this->lang[$_key] : $key . '*';
if (isset($this->langarray[strtolower ($key)]) && $this->langarray[strtolower ($key)])
{
$ret = $this->langarray[strtolower ($key)];
}
else
{
$ret = $key."*";
}
$ndx = 1;
while( list($key,$val) = each( $vars ) )
while(list($key,$val) = each($vars))
{
$ret = preg_replace( "/%$ndx/", $val, $ret );
++$ndx;
@ -99,52 +77,55 @@
return $ret;
}
/*!
@function add_app
@abstract loads all app phrases into langarray
@param $lang user lang variable (defaults to en)
*/
function add_app($app,$lang='')
function load_langs($all=False)
{
define('SEP',filesystem_separator());
//echo '<br>add_app(): called with app="' . $app . '", lang="' . $userlang . '"';
if (!isset($lang) || !$lang)
if($all)
{
if (isset($GLOBALS['phpgw_info']['user']['preferences']['common']['lang']) &&
$GLOBALS['phpgw_info']['user']['preferences']['common']['lang'])
@reset($GLOBALS['phpgw_info']['user']['apps']);
while(list(,$appname) = @each($GLOBALS['phpgw_info']['user']['apps']))
{
$userlang = $GLOBALS['phpgw_info']['user']['preferences']['common']['lang'];
}
else
{
$userlang = 'en';
$this->add_app($appname['name']);
}
$this->all_loaded = True;
}
else
{
$userlang = $lang;
$this->add_app('phpgwapi');
$this->add_app($this->currentapp);
}
$this->loaded = True;
}
/*!
@function add_app
@abstract loads all app phrases into lang
@param $lang user lang variable (defaults to en)
*/
function add_app($app)
{
//echo '<br>add_app(): adding phrases from: ' . $app;
define('SEP',filesystem_separator());
$userlang = $this->userlang ? $this->userlang : 'en';
$fn = PHPGW_SERVER_ROOT . SEP . $app . SEP . 'setup' . SEP . 'phpgw_' . $userlang . '.lang';
if (!file_exists($fn))
if(!file_exists($fn))
{
$fn = PHPGW_SERVER_ROOT . SEP . $app . SEP . 'setup' . SEP . 'phpgw_en.lang';
}
if (file_exists($fn))
if(file_exists($fn))
{
$fp = fopen($fn,'r');
while ($data = fgets($fp,8000))
$fp = fopen($fn,'rb');
while($data = fgets($fp,8000))
{
list($message_id,$app_name,$null,$content) = explode("\t",$data);
//echo '<br>add_app(): adding phrase: $this->langarray["'.$message_id.'"]=' . trim($content);
$this->langarray[$message_id] = trim($content);
//echo '<br>add_app(): adding phrase: $this->lang["'.$message_id.'"]=' . trim($content);
$this->lang[$message_id] = trim($content);
}
fclose($fp);
}
// stuff class array listing apps that are included already
$this->loaded_apps[] = $app;
}
}
?>