2001-06-15 23:23:46 +02:00
< ? php
/************************************************************************** \
2004-05-05 14:06:13 +02:00
* eGroupWare API - Translation class for phpgw lang files *
2001-06-15 23:23:46 +02:00
* This file written by Miles Lott < milosch @ phpgroupware . org > *
* Handles multi - language support using flat files *
* -------------------------------------------------------------------------*
2004-05-05 14:06:13 +02:00
* This library is part of the eGroupWare API *
* http :// www . egroupware . org / api *
2001-06-15 23:23:46 +02:00
* ------------------------------------------------------------------------ *
* This library is free software ; you can redistribute it and / or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation ; either version 2.1 of the License , *
* or any later version . *
* This library is distributed in the hope that it will be useful , but *
* WITHOUT ANY WARRANTY ; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . *
* See the GNU Lesser General Public License for more details . *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library ; if not , write to the Free Software Foundation , *
* Inc . , 59 Temple Place , Suite 330 , Boston , MA 02111 - 1307 USA *
\ **************************************************************************/
/* $Id$ */
2003-08-28 16:31:11 +02:00
// 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.
2003-10-19 17:15:00 +02:00
// To use, set $GLOBALS['phpgw_info']['server']['translation_system'] = 'file'; in
2003-08-28 16:31:11 +02:00
// class.translation.inc.php
2001-06-15 23:23:46 +02:00
class translation
{
2003-08-28 16:31:11 +02:00
var $langarray ; // Currently loaded translations
var $loaded_apps = array (); // Loaded app langs
2001-06-15 23:23:46 +02:00
/*!
@ function translate
@ abstract Translate phrase to user selected lang
@ param $key phrase to translate
@ param $vars vars sent to lang function , passed to us
*/
2003-08-28 16:31:11 +02:00
function translation ( $appname = 'phpgwapi' , $loadlang = '' )
2001-06-15 23:23:46 +02:00
{
2003-08-28 16:31:11 +02:00
global $lang ;
if ( $loadlang )
2001-09-28 23:32:44 +02:00
{
2003-08-28 16:31:11 +02:00
$lang = $loadlang ;
2001-09-28 23:32:44 +02:00
}
2003-08-28 16:31:11 +02:00
$this -> add_app ( $appname , $lang );
2003-10-19 17:15:00 +02:00
if ( ! isset ( $GLOBALS [ 'phpgw_setup' ]))
{
$this -> system_charset = $GLOBALS [ 'phpgw_info' ][ 'server' ][ 'system_charset' ];
}
else
{
$GLOBALS [ 'phpgw' ] -> db -> query ( " SELECT config_value FROM phpgw_config WHERE config_app='phpgwapi' AND config_name='system_charset' " , __LINE__ , __FILE__ );
if ( $GLOBALS [ 'phpgw' ] -> db -> next_record ())
{
$this -> system_charset = $GLOBALS [ 'phpgw' ] -> db -> f ( 0 );
}
}
// load multi-byte-string-extension if needed, and set its internal encodeing to your system_charset
if ( $this -> system_charset && substr ( $this -> system_charset , 0 , 9 ) != 'iso-8859-1' )
{
if ( $this -> mbstring = extension_loaded ( 'mbstring' ) || @ dl ( PHP_SHLIB_PREFIX . 'mbstring.' . PHP_SHLIB_SUFFIX ))
{
ini_set ( 'mbstring.internal_encoding' , $this -> system_charset );
if ( ini_get ( 'mbstring.func_overload' ) < 7 )
{
if ( $warnings ) echo " <p>Warning: Please set <b>mbstring.func_overload = 7</b> in your php.ini for useing <b> $this->system_charset </b> as your charset !!!</p> \n " ;
}
}
else
{
if ( $warnings ) echo " <p>Warning: Please get and/or enable the <b>mbstring extension</b> in your php.ini for useing <b> $this->system_charset </b> as your charset, we are defaulting to <b>iconv</b> for now !!!</p> \n " ;
}
}
}
function init ()
{
// post-nuke and php-nuke are using $GLOBALS['lang'] too
// but not as array!
// this produces very strange results
if ( ! is_array ( $GLOBALS [ 'lang' ]))
{
$GLOBALS [ 'lang' ] = array ();
}
if ( $GLOBALS [ 'phpgw_info' ][ 'user' ][ 'preferences' ][ 'common' ][ 'lang' ])
{
$this -> userlang = $GLOBALS [ 'phpgw_info' ][ 'user' ][ 'preferences' ][ 'common' ][ 'lang' ];
}
$this -> add_app ( 'common' );
if ( ! count ( $GLOBALS [ 'lang' ]))
{
$this -> userlang = 'en' ;
$this -> add_app ( 'common' );
}
$this -> add_app ( $GLOBALS [ 'phpgw_info' ][ 'flags' ][ 'currentapp' ]);
}
/*
@ function charset
@ abstract returns the charset to use ( ! $lang ) or the charset of the lang - files or $lang
*/
function charset ( $lang = False )
{
if ( $lang )
{
if ( ! isset ( $this -> charsets [ $lang ]))
{
$GLOBALS [ 'phpgw' ] -> db -> query ( " SELECT content FROM phpgw_lang WHERE lang=' $lang ' AND message_id='charset' AND app_name='common' " , __LINE__ , __FILE__ );
$this -> charsets [ $lang ] = $GLOBALS [ 'phpgw' ] -> db -> next_record () ? strtolower ( $GLOBALS [ 'phpgw' ] -> db -> f ( 0 )) : 'iso-8859-1' ;
}
return $this -> charsets [ $lang ];
}
if ( $this -> system_charset ) // do we have a system-charset ==> return it
{
return $this -> system_charset ;
}
// if no translations are loaded (system-startup) use a default, else lang('charset')
return ! is_array ( $GLOBALS [ 'lang' ]) ? 'iso-8859-1' : strtolower ( $this -> translate ( 'charset' ));
2003-08-28 16:31:11 +02:00
}
2001-06-15 23:23:46 +02:00
2003-08-28 16:31:11 +02:00
function isin_array ( $needle , $haystack )
{
2003-10-19 17:15:00 +02:00
while ( list ( $k , $v ) = each ( $haystack ))
2001-06-15 23:23:46 +02:00
{
2003-10-19 17:15:00 +02:00
if ( $v == $needle )
2003-08-28 16:31:11 +02:00
{
return True ;
}
2001-06-15 23:23:46 +02:00
}
2003-08-28 16:31:11 +02:00
return False ;
}
2001-06-15 23:23:46 +02:00
2003-08-28 16:31:11 +02:00
function translate ( $key , $vars = False )
{
2003-10-19 17:15:00 +02:00
if ( ! $this -> isin_array ( $GLOBALS [ 'phpgw_info' ][ 'flags' ][ 'currentapp' ], $this -> loaded_apps ) &&
2003-08-28 16:31:11 +02:00
$GLOBALS [ 'phpgw_info' ][ 'flags' ][ 'currentapp' ] != 'home' )
2001-06-15 23:23:46 +02:00
{
2003-08-28 16:31:11 +02:00
//echo '<br>loading app "' . $GLOBALS['phpgw_info']['flags']['currentapp'] . '" for the first time.';
2003-10-19 17:15:00 +02:00
$this -> add_app ( $GLOBALS [ 'phpgw_info' ][ 'flags' ][ 'currentapp' ], $GLOBALS [ 'lang' ]);
2003-08-28 16:31:11 +02:00
}
2003-10-19 17:15:00 +02:00
elseif ( $GLOBALS [ 'phpgw_info' ][ 'flags' ][ 'currentapp' ] == 'admin' ||
2003-08-28 16:31:11 +02:00
$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' ]);
2003-10-19 17:15:00 +02:00
while ( list ( $x , $app ) = @ each ( $GLOBALS [ 'phpgw_info' ][ 'apps' ]))
2003-08-28 16:31:11 +02:00
{
2003-10-19 17:15:00 +02:00
if ( ! $this -> isin_array ( $app [ 'name' ], $this -> loaded_apps ))
2003-08-28 16:31:11 +02:00
{
//echo '<br>loading app "' . $app['name'] . '" for the first time.';
2003-10-19 17:15:00 +02:00
$this -> add_app ( $app [ 'name' ], $GLOBALS [ 'lang' ]);
2003-08-28 16:31:11 +02:00
}
}
2001-06-15 23:23:46 +02:00
}
2003-10-19 17:15:00 +02:00
if ( ! $vars )
2002-05-20 03:11:27 +02:00
{
2003-08-28 16:31:11 +02:00
$vars = array ();
2002-05-20 03:11:27 +02:00
}
2003-08-28 16:31:11 +02:00
$ret = $key ;
2003-10-19 17:15:00 +02:00
if ( isset ( $this -> langarray [ strtolower ( $key )]) && $this -> langarray [ strtolower ( $key )])
2001-06-15 23:23:46 +02:00
{
2003-10-19 17:15:00 +02:00
$ret = $this -> langarray [ strtolower ( $key )];
2001-06-15 23:23:46 +02:00
}
else
{
2003-08-28 16:31:11 +02:00
$ret = $key . " * " ;
2001-06-15 23:23:46 +02:00
}
2003-08-28 16:31:11 +02:00
$ndx = 1 ;
while ( list ( $key , $val ) = each ( $vars ) )
{
$ret = preg_replace ( " /% $ndx / " , $val , $ret );
++ $ndx ;
}
return $ret ;
2002-05-20 02:45:52 +02:00
}
2003-10-19 17:15:00 +02:00
function autoload_changed_langfiles ()
{
return ;
}
function drop_langs ( $appname , $DEBUG = False )
{
if ( $DEBUG )
{
echo '<br>drop_langs(): Working on: ' . $appname ;
echo '<br>drop_langs(): Not needed with file-based lang class.' ;
}
}
function add_langs ( $appname , $DEBUG = False , $force_langs = False )
{
if ( $DEBUG )
{
echo '<br>add_langs(): Working on: ' . $appname ;
echo '<br>add_langs(): Not needed with file-based lang class.' ;
}
}
2002-05-20 02:45:52 +02:00
/*!
@ function add_app
2003-08-28 16:31:11 +02:00
@ abstract loads all app phrases into langarray
2002-05-20 02:45:52 +02:00
@ param $lang user lang variable ( defaults to en )
*/
2003-08-28 16:31:11 +02:00
function add_app ( $app , $lang = '' )
2002-05-20 02:45:52 +02:00
{
define ( 'SEP' , filesystem_separator ());
2003-08-28 16:31:11 +02:00
//echo '<br>add_app(): called with app="' . $app . '", lang="' . $userlang . '"';
2003-10-19 17:15:00 +02:00
if ( ! isset ( $lang ) || ! $lang )
2003-08-28 16:31:11 +02:00
{
2003-10-19 17:15:00 +02:00
if ( isset ( $GLOBALS [ 'phpgw_info' ][ 'user' ][ 'preferences' ][ 'common' ][ 'lang' ]) &&
2003-08-28 16:31:11 +02:00
$GLOBALS [ 'phpgw_info' ][ 'user' ][ 'preferences' ][ 'common' ][ 'lang' ])
{
$userlang = $GLOBALS [ 'phpgw_info' ][ 'user' ][ 'preferences' ][ 'common' ][ 'lang' ];
}
else
{
$userlang = 'en' ;
}
}
else
{
$userlang = $lang ;
}
2002-05-20 02:45:52 +02:00
2001-06-15 23:23:46 +02:00
$fn = PHPGW_SERVER_ROOT . SEP . $app . SEP . 'setup' . SEP . 'phpgw_' . $userlang . '.lang' ;
2003-10-19 17:15:00 +02:00
if ( ! file_exists ( $fn ))
2001-06-15 23:23:46 +02:00
{
$fn = PHPGW_SERVER_ROOT . SEP . $app . SEP . 'setup' . SEP . 'phpgw_en.lang' ;
}
2003-10-19 17:15:00 +02:00
if ( file_exists ( $fn ))
2001-06-15 23:23:46 +02:00
{
2002-05-20 03:11:27 +02:00
$fp = fopen ( $fn , 'r' );
2003-10-19 17:15:00 +02:00
while ( $data = fgets ( $fp , 8000 ))
2001-06-15 23:23:46 +02:00
{
list ( $message_id , $app_name , $null , $content ) = explode ( " \t " , $data );
2003-08-28 16:31:11 +02:00
//echo '<br>add_app(): adding phrase: $this->langarray["'.$message_id.'"]=' . trim($content);
$this -> langarray [ $message_id ] = trim ( $content );
2001-06-15 23:23:46 +02:00
}
fclose ( $fp );
}
2003-08-28 16:31:11 +02:00
// stuff class array listing apps that are included already
$this -> loaded_apps [] = $app ;
2001-06-15 23:23:46 +02:00
}
}
?>