2001-01-11 10:52:33 +01:00
< ? php
2005-05-31 15:52:14 +02:00
/************************************************************************** \
* eGroupWare API - Translation class for SQL *
* This file written by Joseph Engo < jengo @ phpgroupware . org > *
* and Dan Kuykendall < seek3r @ phpgroupware . org > *
* Handles multi - language support use SQL tables *
* Copyright ( C ) 2000 , 2001 Joseph Engo *
* ------------------------------------------------------------------------ *
* This library is part of the eGroupWare API *
* http :// www . egroupware . org / api *
* ------------------------------------------------------------------------ *
* 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$ */
2001-01-11 10:52:33 +01:00
2003-10-04 15:24:34 +02:00
// define the maximal length of a message_id, all message_ids have to be unique
2003-04-02 19:46:37 +02:00
// in this length, our column is varchar 255, but addslashes might add some length
2003-10-04 15:24:34 +02:00
if ( ! defined ( 'MAX_MESSAGE_ID_LENGTH' ))
{
define ( 'MAX_MESSAGE_ID_LENGTH' , 230 );
}
2003-10-05 12:44:16 +02:00
// some constanst for pre php4.3
if ( ! defined ( 'PHP_SHLIB_SUFFIX' ))
{
define ( 'PHP_SHLIB_SUFFIX' , strtoupper ( substr ( PHP_OS , 0 , 3 )) == 'WIN' ? 'dll' : 'so' );
}
if ( ! defined ( 'PHP_SHLIB_PREFIX' ))
{
define ( 'PHP_SHLIB_PREFIX' , PHP_SHLIB_SUFFIX == 'dll' ? 'php_' : '' );
}
2003-10-04 15:24:34 +02:00
2001-06-15 23:29:33 +02:00
class translation
{
2003-09-14 12:20:46 +02:00
var $userlang = 'en' ;
var $loaded_apps = array ();
2004-07-20 18:11:31 +02:00
var $line_rejected = array ();
2005-07-17 23:00:49 +02:00
var $lang_array = array ();
2003-09-14 12:20:46 +02:00
2005-05-31 15:52:14 +02:00
/**
* Constructor , sets up a copy of the db - object , gets the system - charset and tries to load the mbstring extension
*/
2003-10-05 12:44:16 +02:00
function translation ( $warnings = False )
2003-10-04 15:24:34 +02:00
{
2004-05-04 22:26:08 +02:00
for ( $i = 1 ; $i <= 9 ; $i ++ )
{
$this -> placeholders [] = '%' . $i ;
}
2005-03-04 16:47:08 +01:00
$this -> db = is_object ( $GLOBALS [ 'egw' ] -> db ) ? $GLOBALS [ 'egw' ] -> db : $GLOBALS [ 'egw_setup' ] -> db ;
2004-08-04 17:34:14 +02:00
$this -> db -> set_app ( 'phpgwapi' );
$this -> lang_table = 'phpgw_lang' ;
$this -> languages_table = 'phpgw_languages' ;
$this -> config_table = 'phpgw_config' ;
2005-03-04 16:47:08 +01:00
if ( ! isset ( $GLOBALS [ 'egw_setup' ]))
2003-10-05 04:06:11 +02:00
{
2005-03-04 16:47:08 +01:00
$this -> system_charset = @ $GLOBALS [ 'egw_info' ][ 'server' ][ 'system_charset' ];
2003-10-05 04:06:11 +02:00
}
else
{
2004-08-04 17:34:14 +02:00
$this -> db -> select ( $this -> config_table , 'config_value' , array (
'config_app' => 'phpgwapi' ,
'config_name' => 'system_charset'
), __LINE__ , __FILE__ );
2003-10-05 04:06:11 +02:00
if ( $this -> db -> next_record ())
{
$this -> system_charset = $this -> db -> f ( 0 );
}
}
2003-10-05 12:44:16 +02:00
// load multi-byte-string-extension if needed, and set its internal encodeing to your system_charset
2003-10-08 12:50:24 +02:00
if ( $this -> system_charset && substr ( $this -> system_charset , 0 , 9 ) != 'iso-8859-1' )
2003-10-05 12:44:16 +02:00
{
2003-10-05 12:57:50 +02:00
if ( $this -> mbstring = extension_loaded ( 'mbstring' ) || @ dl ( PHP_SHLIB_PREFIX . 'mbstring.' . PHP_SHLIB_SUFFIX ))
2003-10-05 12:44:16 +02:00
{
ini_set ( 'mbstring.internal_encoding' , $this -> system_charset );
2003-10-07 03:02:19 +02:00
if ( ini_get ( 'mbstring.func_overload' ) < 7 )
2003-10-05 12:44:16 +02:00
{
2004-01-25 22:29:20 +01:00
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 " ;
}
2003-10-05 12:44:16 +02:00
}
}
else
{
2004-01-25 22:29:20 +01:00
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 " ;
}
2003-10-05 12:44:16 +02:00
}
}
2003-10-05 04:06:11 +02:00
}
2005-05-31 15:52:14 +02:00
/**
* returns the charset to use ( ! $lang ) or the charset of the lang - files or $lang
*
* @ param string / boolean $lang = False return charset of the active user - lang , or $lang if specified
* @ return string charset
*/
2003-10-05 04:06:11 +02:00
function charset ( $lang = False )
{
if ( $lang )
{
2003-10-05 12:44:16 +02:00
if ( ! isset ( $this -> charsets [ $lang ]))
{
2004-08-04 17:34:14 +02:00
$this -> db -> select ( $this -> lang_table , 'content' , array (
'lang' => $lang ,
'message_id' => 'charset' ,
'app_name' => 'common' ,
), __LINE__ , __FILE__ );
2003-10-06 13:30:21 +02:00
$this -> charsets [ $lang ] = $this -> db -> next_record () ? strtolower ( $this -> db -> f ( 0 )) : 'iso-8859-1' ;
2003-10-05 12:44:16 +02:00
}
return $this -> charsets [ $lang ];
2003-10-05 04:06:11 +02:00
}
2003-10-15 14:24:38 +02:00
if ( $this -> system_charset ) // do we have a system-charset ==> return it
{
2004-10-23 12:53:17 +02:00
$charset = $this -> system_charset ;
2003-10-15 14:24:38 +02:00
}
2004-10-23 12:53:17 +02:00
else
{
// if no translations are loaded (system-startup) use a default, else lang('charset')
2005-07-17 23:00:49 +02:00
$charset = ! is_array ( @ $this -> lang_arr ) ? 'iso-8859-1' : strtolower ( $this -> translate ( 'charset' ));
2004-10-23 12:53:17 +02:00
}
// we need to set our charset as mbstring.internal_encoding if mbstring.func_overlaod > 0
// else we get problems for a charset is different from the default utf-8
if ( ini_get ( 'mbstring.func_overload' ) && $this -> mbstring_internal_encoding != $charset )
{
ini_set ( 'mbstring.internal_encoding' , $this -> mbstring_internal_encoding = $charset );
}
return $charset ;
2003-10-04 15:24:34 +02:00
}
2005-05-31 15:52:14 +02:00
/**
* Initialises global lang - array and loads the 'common' and app - spec . translations
*/
2003-09-14 12:20:46 +02:00
function init ()
{
2005-07-17 23:00:49 +02:00
if ( ! is_array ( @ $this -> lang_arr ))
2003-09-14 12:20:46 +02:00
{
2005-07-17 23:00:49 +02:00
$this -> lang_arr = array ();
2003-09-14 12:20:46 +02:00
}
2005-03-04 16:47:08 +01:00
if ( $GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'common' ][ 'lang' ])
2003-09-14 12:20:46 +02:00
{
2005-03-04 16:47:08 +01:00
$this -> userlang = $GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'common' ][ 'lang' ];
2003-09-14 12:20:46 +02:00
}
$this -> add_app ( 'common' );
2005-07-17 23:00:49 +02:00
if ( ! count ( $this -> lang_arr ))
2003-09-23 23:26:58 +02:00
{
$this -> userlang = 'en' ;
$this -> add_app ( 'common' );
}
2005-03-04 16:47:08 +01:00
$this -> add_app ( $GLOBALS [ 'egw_info' ][ 'flags' ][ 'currentapp' ]);
2003-09-14 12:20:46 +02:00
}
2005-05-31 15:52:14 +02:00
/**
* translates a phrase and evtl . substitute some variables
*
* @ param string $key phrase to translate , may contain placeholders % N ( N = 1 , 2 , ... ) for vars
* @ param array / boolean $vars = false vars to replace the placeholders , or false for none
* @ param string $not_found = '*' what to add to not found phrases , default '*'
* @ return string with translation
*/
2003-10-10 02:07:20 +02:00
function translate ( $key , $vars = false , $not_found = '*' )
2002-02-20 05:44:19 +01:00
{
2005-07-17 23:00:49 +02:00
if ( ! is_array ( @ $this -> lang_arr ) || ! count ( $this -> lang_arr ))
2001-06-15 23:29:33 +02:00
{
2003-09-14 12:20:46 +02:00
$this -> init ();
2001-06-15 23:29:33 +02:00
}
2003-10-10 02:07:20 +02:00
$ret = $key . $not_found ; // save key if we dont find a translation
2003-09-14 12:20:46 +02:00
2005-07-17 23:00:49 +02:00
if ( isset ( $this -> lang_arr [ $key ]))
2002-02-20 05:44:19 +01:00
{
2005-07-17 23:00:49 +02:00
$ret = $this -> lang_arr [ $key ];
2002-02-20 05:44:19 +01:00
}
2004-05-04 22:26:08 +02:00
else
{
$new_key = strtolower ( trim ( substr ( $key , 0 , MAX_MESSAGE_ID_LENGTH )));
2005-07-17 23:00:49 +02:00
if ( isset ( $this -> lang_arr [ $new_key ]))
2004-05-04 22:26:08 +02:00
{
// we save the original key for performance
2005-07-17 23:00:49 +02:00
$ret = $this -> lang_arr [ $key ] = $this -> lang_arr [ $new_key ];
2004-05-04 22:26:08 +02:00
}
}
if ( is_array ( $vars ) && count ( $vars ))
2001-06-15 23:29:33 +02:00
{
2004-05-04 22:26:08 +02:00
if ( count ( $vars ) > 1 )
{
$ret = str_replace ( $this -> placeholders , $vars , $ret );
}
else
{
$ret = str_replace ( '%1' , $vars [ 0 ], $ret );
}
2001-06-15 23:29:33 +02:00
}
return $ret ;
}
2005-05-31 15:52:14 +02:00
/**
* adds translations for an application from the database to the lang - array
*
* @ param string $app name of the application to add ( or 'common' for the general translations )
* @ param string / boolean $lang = false 2 or 5 char lang - code or false for the users language
*/
2003-09-15 10:42:47 +02:00
function add_app ( $app , $lang = False )
2001-06-15 23:29:33 +02:00
{
2003-09-15 10:42:47 +02:00
$lang = $lang ? $lang : $this -> userlang ;
if ( ! isset ( $this -> loaded_apps [ $app ]) || $this -> loaded_apps [ $app ] != $lang )
2003-08-28 16:31:11 +02:00
{
2004-10-16 01:06:34 +02:00
if ( $app == 'setup' ) return $this -> add_setup ( $lang );
2004-08-04 17:34:14 +02:00
$this -> db -> select ( $this -> lang_table , 'message_id,content' , array (
'lang' => $lang ,
'app_name' => $app ,
), __LINE__ , __FILE__ );
2003-10-04 15:24:34 +02:00
while ( $this -> db -> next_record ())
2003-09-14 12:20:46 +02:00
{
2005-07-17 23:00:49 +02:00
$this -> lang_arr [ strtolower ( $this -> db -> f ( 'message_id' ))] = $this -> db -> f ( 'content' );
2003-09-14 12:20:46 +02:00
}
2003-09-15 10:42:47 +02:00
$this -> loaded_apps [ $app ] = $lang ;
2001-06-15 23:29:33 +02:00
}
}
2003-09-14 12:20:46 +02:00
2004-10-16 01:06:34 +02:00
/**
* Adds setup ' s translations , they are not in the DB !
2005-05-31 15:52:14 +02:00
*
* @ param string $lang 2 or 5 char lang - code
2004-10-16 01:06:34 +02:00
*/
function add_setup ( $lang )
{
2005-05-31 15:52:14 +02:00
$fn = EGW_SERVER_ROOT . '/setup/lang/phpgw_' . $lang . '.lang' ;
2004-10-16 01:06:34 +02:00
if ( ! file_exists ( $fn ))
{
2005-05-31 15:52:14 +02:00
$fn = EGW_SERVER_ROOT . '/setup/lang/phpgw_en.lang' ;
2004-10-16 01:06:34 +02:00
}
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 );
$phrases [ strtolower ( trim ( $message_id ))] = str_replace ( " \n " , '' , $content );
}
fclose ( $fp );
foreach ( $phrases as $message_id => $content )
{
2005-07-17 23:00:49 +02:00
$this -> lang_arr [ $message_id ] = $this -> convert ( $content , $phrases [ 'charset' ]);
2004-10-16 01:06:34 +02:00
}
}
$this -> loaded_apps [ 'setup' ] = $lang ;
}
2005-05-31 15:52:14 +02:00
/**
* returns a list of installed langs
*
* @ return array with lang - code => descriptiv lang - name pairs
*/
2003-04-02 19:46:37 +02:00
function get_installed_langs ()
{
2003-09-14 12:20:46 +02:00
if ( ! is_array ( $this -> langs ))
2003-04-02 19:46:37 +02:00
{
2004-08-04 00:53:45 +02:00
$this -> db -> query ( " SELECT DISTINCT l.lang,lx.lang_name FROM phpgw_lang l,phpgw_languages lx WHERE l.lang = lx.lang_id " , __LINE__ , __FILE__ );
2003-10-04 15:24:34 +02:00
if ( ! $this -> db -> num_rows ())
2003-09-14 12:20:46 +02:00
{
return False ;
}
2003-10-04 15:24:34 +02:00
while ( $this -> db -> next_record ())
2003-09-14 12:20:46 +02:00
{
2004-04-13 16:19:52 +02:00
$this -> langs [ $this -> db -> f ( 'lang' )] = $this -> db -> f ( 'lang_name' );
}
foreach ( $this -> langs as $lang => $name )
{
$this -> langs [ $lang ] = $this -> translate ( $name , False , '' );
2003-09-14 12:20:46 +02:00
}
2003-04-02 19:46:37 +02:00
}
2003-09-14 12:20:46 +02:00
return $this -> langs ;
2003-04-02 19:46:37 +02:00
}
2003-10-04 15:24:34 +02:00
2005-05-31 15:52:14 +02:00
/**
* returns a list of installed charsets
*
* @ return array with charset as key and comma - separated list of langs useing the charset as data
*/
2003-10-04 15:24:34 +02:00
function get_installed_charsets ()
{
if ( ! is_array ( $this -> charsets ))
{
2005-05-31 15:52:14 +02:00
$distinct = $this -> db -> capabilities [ 'distinct_on_text' ] ? 'DISTINCT' : '' ;
2004-09-19 14:46:22 +02:00
$this -> db -> query ( " SELECT $distinct l.lang,lx.lang_name,l.content AS charset FROM phpgw_lang l,phpgw_languages lx WHERE l.lang = lx.lang_id AND l.message_id='charset' " , __LINE__ , __FILE__ );
2003-10-04 15:24:34 +02:00
if ( ! $this -> db -> num_rows ())
{
return False ;
}
while ( $this -> db -> next_record ())
{
2003-10-06 13:30:21 +02:00
$data = & $this -> charsets [ $charset = strtolower ( $this -> db -> f ( 'charset' ))];
2004-09-19 14:46:22 +02:00
$lang = $this -> db -> f ( 'lang_name' ) . ' (' . $this -> db -> f ( 'lang' ) . ')' ;
if ( $distinct || strstr ( $data , $lang ) === false )
{
$data .= ( $data ? ', ' : $charset . ': ' ) . $lang ;
}
2003-10-04 15:24:34 +02:00
}
}
return $this -> charsets ;
}
2005-05-31 15:52:14 +02:00
/**
* converts a string $data from charset $from to charset $to
*
* @ param string / array $data string ( s ) to convert
* @ param string / boolean $from charset $data is in or False if it should be detected
* @ param string / boolean $to charset to convert to or False for the system - charset the converted string
* @ return string / array converted string ( s ) from $data
*/
2003-10-05 17:41:59 +02:00
function convert ( $data , $from = False , $to = False )
2003-10-05 04:06:11 +02:00
{
2003-10-05 17:41:59 +02:00
if ( is_array ( $data ))
{
foreach ( $data as $key => $str )
{
$ret [ $key ] = $this -> convert ( $str , $from , $to );
}
return $ret ;
}
2004-02-26 11:57:57 +01:00
2004-03-14 18:31:43 +01:00
if ( $from )
{
$from = strtolower ( $from );
}
if ( $to )
{
$to = strtolower ( $to );
}
2004-02-26 11:57:57 +01:00
2003-10-05 17:41:59 +02:00
if ( ! $from )
{
$from = $this -> mbstring ? strtolower ( mb_detect_encoding ( $data )) : 'iso-8859-1' ;
2004-03-14 18:31:43 +01:00
if ( $from == 'ascii' )
{
$from = 'iso-8859-1' ;
}
2003-10-05 17:41:59 +02:00
//echo "<p>autodetected charset of '$data' = '$from'</p>\n";
}
2004-03-14 18:31:43 +01:00
/*
2005-05-31 15:52:14 +02:00
php does not seem to support gb2312
but seems to be able to decode it as EUC - CN
2003-10-07 22:56:33 +02:00
*/
2004-02-26 11:57:57 +01:00
switch ( $from )
2003-10-07 22:56:33 +02:00
{
2004-02-26 07:42:20 +01:00
case 'gb2312' :
case 'gb18030' :
2004-03-14 18:31:43 +01:00
$from = 'EUC-CN' ;
2004-02-26 07:42:20 +01:00
break ;
case 'us-ascii' :
2004-06-11 07:32:03 +02:00
case 'macroman' :
2004-02-26 07:42:20 +01:00
$from = 'iso-8859-1' ;
break ;
2003-10-10 07:01:49 +02:00
}
2003-10-05 17:41:59 +02:00
if ( ! $to )
{
2003-10-07 03:02:19 +02:00
$to = $this -> charset ();
2003-10-05 17:41:59 +02:00
}
2003-10-08 12:50:24 +02:00
if ( $from == $to || ! $from || ! $to || ! $data )
2003-10-05 12:44:16 +02:00
{
return $data ;
}
if ( $from == 'iso-8859-1' && $to == 'utf-8' )
2003-10-05 04:06:11 +02:00
{
return utf8_encode ( $data );
}
2003-10-05 12:44:16 +02:00
if ( $to == 'iso-8859-1' && $from == 'utf-8' )
2003-10-05 04:06:11 +02:00
{
return utf8_decode ( $data );
}
2003-10-05 12:44:16 +02:00
if ( $this -> mbstring )
{
2003-10-15 21:15:25 +02:00
return @ mb_convert_encoding ( $data , $to , $from );
2003-10-05 12:44:16 +02:00
}
2003-10-10 01:38:24 +02:00
if ( function_exists ( 'iconv' ))
2003-10-08 12:50:24 +02:00
{
2004-05-29 03:14:29 +02:00
if ( $from == 'EUC-CN' ) $from = 'gb18030' ;
// the above is to workaround patch #962307
// if using EUC-CN, for iconv it strickly follow GB2312 and fail
// in an email on the first Traditional/Japanese/Korean character,
// but in reality when people send mails in GB2312, UMA mostly use
// extended GB13000/GB18030 which allow T/Jap/Korean characters.
2004-02-26 11:57:57 +01:00
if (( $data = iconv ( $from , $to , $data )))
2003-10-10 01:38:24 +02:00
{
return $data ;
}
2003-10-08 12:50:24 +02:00
}
2003-10-10 01:38:24 +02:00
#die("<p>Can't convert from charset '$from' to '$to' without the <b>mbstring extension</b> !!!</p>");
2004-03-14 18:31:43 +01:00
2003-10-10 01:38:24 +02:00
// this is not good, not convert did succed
return $data ;
2003-10-05 04:06:11 +02:00
}
2005-05-31 15:52:14 +02:00
/**
* installs translations for the selected langs into the database
*
* @ param array $langs langs to install ( as data NOT keys ( ! ))
* @ param string $upgrademethod = 'dumpold' 'dumpold' ( recommended & fastest ), 'addonlynew' languages , 'addmissing' phrases
* @ param string / boolean $only_app = false app - name to install only one app or default false for all
*/
2003-10-05 04:06:11 +02:00
function install_langs ( $langs , $upgrademethod = 'dumpold' , $only_app = False )
2003-10-04 15:24:34 +02:00
{
@ set_time_limit ( 0 ); // we might need some time
2004-08-04 00:53:45 +02:00
//echo "<p>translation_sql::install_langs(".print_r($langs,true).",'$upgrademthod','$only_app')</p>\n";
2005-03-04 16:47:08 +01:00
if ( ! isset ( $GLOBALS [ 'egw_info' ][ 'server' ]) && $upgrademethod != 'dumpold' )
2003-10-04 15:24:34 +02:00
{
2004-08-04 17:34:14 +02:00
$this -> db -> select ( $this -> config_table , 'config_value' , array (
'config_app' => 'phpgwapi' ,
'config_name' => 'lang_ctimes' ,
), __LINE__ , __FILE__ );
2003-10-04 15:24:34 +02:00
if ( $this -> db -> next_record ())
{
2005-03-04 16:47:08 +01:00
$GLOBALS [ 'egw_info' ][ 'server' ][ 'lang_ctimes' ] = unserialize ( stripslashes ( $this -> db -> f ( 'config_value' )));
2003-10-04 15:24:34 +02:00
}
}
if ( ! is_array ( $langs ) || ! count ( $langs ))
{
return ;
}
$this -> db -> transaction_begin ();
if ( $upgrademethod == 'dumpold' )
{
// dont delete the custom main- & loginscreen messages every time
2004-08-04 17:34:14 +02:00
$this -> db -> delete ( $this -> lang_table , array ( " app_name!='mainscreen' " , " app_name!='loginscreen' " ), __LINE__ , __FILE__ );
2003-10-04 15:24:34 +02:00
//echo '<br>Test: dumpold';
2005-03-04 16:47:08 +01:00
$GLOBALS [ 'egw_info' ][ 'server' ][ 'lang_ctimes' ] = array ();
2003-10-04 15:24:34 +02:00
}
foreach ( $langs as $lang )
{
2003-10-15 14:29:27 +02:00
//echo '<br>Working on: ' . $lang;
2003-10-04 15:24:34 +02:00
$addlang = False ;
if ( $upgrademethod == 'addonlynew' )
{
//echo "<br>Test: addonlynew - select count(*) from phpgw_lang where lang='".$lang."'";
2004-08-04 17:34:14 +02:00
$this -> db -> select ( $this -> lang_table , 'COUNT(*)' , array (
'lang' => $lang ,
), __LINE__ , __FILE__ );
2003-10-04 15:24:34 +02:00
$this -> db -> next_record ();
if ( $this -> db -> f ( 0 ) == 0 )
{
//echo '<br>Test: addonlynew - True';
$addlang = True ;
}
}
2003-10-05 17:41:59 +02:00
if ( $addlang && $upgrademethod == 'addonlynew' || $upgrademethod != 'addonlynew' )
2003-10-04 15:24:34 +02:00
{
//echo '<br>Test: loop above file()';
2005-03-04 16:47:08 +01:00
if ( ! is_object ( $GLOBALS [ 'egw_setup' ]))
2003-10-04 15:24:34 +02:00
{
2005-05-31 15:52:14 +02:00
$GLOBALS [ 'egw_setup' ] =& CreateObject ( 'setup.setup' );
$GLOBALS [ 'egw_setup' ] -> db = clone ( $this -> db );
2003-10-04 15:24:34 +02:00
}
2005-03-04 16:47:08 +01:00
$setup_info = $GLOBALS [ 'egw_setup' ] -> detection -> get_versions ();
$setup_info = $GLOBALS [ 'egw_setup' ] -> detection -> get_db_versions ( $setup_info );
2003-10-04 15:24:34 +02:00
$raw = array ();
2003-10-05 04:06:11 +02:00
$apps = $only_app ? array ( $only_app ) : array_keys ( $setup_info );
2003-10-04 15:24:34 +02:00
// Visit each app/setup dir, look for a phpgw_lang file
2003-10-05 04:06:11 +02:00
foreach ( $apps as $app )
2003-10-04 15:24:34 +02:00
{
2005-05-31 15:52:14 +02:00
$appfile = EGW_SERVER_ROOT . SEP . $app . SEP . 'setup' . SEP . 'phpgw_' . strtolower ( $lang ) . '.lang' ;
2004-08-04 00:53:45 +02:00
//echo '<br>Checking in: ' . $app;
2005-03-04 16:47:08 +01:00
if ( $GLOBALS [ 'egw_setup' ] -> app_registered ( $app ) && file_exists ( $appfile ))
2003-10-04 15:24:34 +02:00
{
2003-10-15 14:29:27 +02:00
//echo '<br>Including: ' . $appfile;
2003-10-04 15:24:34 +02:00
$lines = file ( $appfile );
foreach ( $lines as $line )
{
2004-05-09 12:17:44 +02:00
// explode with "\t" and removing "\n" with str_replace, needed to work with mbstring.overload=7
2004-08-01 10:05:44 +02:00
list ( $message_id , $app_name ,, $content ) = $_f_buffer = explode ( " \t " , $line );
2004-07-25 03:41:37 +02:00
$content = str_replace ( array ( " \n " , " \r " ), '' , $content );
2004-08-01 10:05:44 +02:00
if ( count ( $_f_buffer ) != 4 )
{
$line_display = str_replace ( array ( " \t " , " \n " ),
array ( " <font color='red'><b> \\ t</b></font> " , " <font color='red'><b> \\ n</b></font> " ), $line );
$this -> line_rejected [] = array (
'appfile' => $appfile ,
'line' => $line_display ,
);
}
2004-05-09 00:30:19 +02:00
$message_id = substr ( strtolower ( chop ( $message_id )), 0 , MAX_MESSAGE_ID_LENGTH );
$app_name = chop ( $app_name );
2003-10-04 15:24:34 +02:00
$raw [ $app_name ][ $message_id ] = $content ;
}
2005-05-31 15:52:14 +02:00
if ( $GLOBALS [ 'egw_info' ][ 'server' ][ 'lang_ctimes' ] && ! is_array ( $GLOBALS [ 'egw_info' ][ 'server' ][ 'lang_ctimes' ]))
{
$GLOBALS [ 'egw_info' ][ 'server' ][ 'lang_ctimes' ] = unserialize ( $GLOBALS [ 'egw_info' ][ 'server' ][ 'lang_ctimes' ]);
}
2005-03-04 16:47:08 +01:00
$GLOBALS [ 'egw_info' ][ 'server' ][ 'lang_ctimes' ][ $lang ][ $app ] = filectime ( $appfile );
2003-10-04 15:24:34 +02:00
}
}
2003-10-20 21:03:46 +02:00
$charset = strtolower ( @ $raw [ 'common' ][ 'charset' ] ? $raw [ 'common' ][ 'charset' ] : $this -> charset ( $lang ));
2003-10-05 04:06:11 +02:00
//echo "<p>lang='$lang', charset='$charset', system_charset='$this->system_charset')</p>\n";
2003-10-15 14:29:27 +02:00
//echo "<p>raw($lang)=<pre>".print_r($raw,True)."</pre>\n";
2003-10-04 15:24:34 +02:00
foreach ( $raw as $app_name => $ids )
{
foreach ( $ids as $message_id => $content )
{
2003-10-20 21:03:46 +02:00
if ( $this -> system_charset )
{
$content = $this -> convert ( $content , $charset , $this -> system_charset );
}
2003-10-04 15:24:34 +02:00
$addit = False ;
//echo '<br>APPNAME:' . $app_name . ' PHRASE:' . $message_id;
if ( $upgrademethod == 'addmissing' )
{
//echo '<br>Test: addmissing';
2004-08-04 17:34:14 +02:00
$this -> db -> select ( $this -> lang_table , " content,CASE WHEN app_name IN ('common') THEN 1 ELSE 0 END AS in_api " , array (
'message_id' => $message_id ,
'lang' => $lang ,
$this -> db -> expression ( $this -> lang_table , '(' , array (
'app_name' => $app_name
), " OR app_name='common') ORDER BY in_api DESC " )), __LINE__ , __FILE__ );
2003-10-04 15:24:34 +02:00
2003-10-05 17:41:59 +02:00
if ( ! ( $row = $this -> db -> row ( True )))
2003-10-04 15:24:34 +02:00
{
$addit = True ;
}
2003-10-05 17:41:59 +02:00
else
{
if ( $row [ 'in_api' ]) // same phrase is in the api
{
$addit = $row [ 'content' ] != $content ; // only add if not identical
}
$row2 = $this -> db -> row ( True );
if ( ! $row [ 'in_api' ] || $app_name == 'common' || $row2 ) // phrase is alread in the db
{
$addit = $content != ( $row2 ? $row2 [ 'content' ] : $row [ 'content' ]);
if ( $addit ) // if we want to add/update it ==> delete it
{
2004-08-04 17:34:14 +02:00
$this -> db -> delete ( $this -> lang_table , array (
'message_id' => $message_id ,
'lang' => $lang ,
'app_name' => $app_name ,
), __LINE__ , __FILE__ );
2003-10-05 17:41:59 +02:00
}
}
}
2003-10-04 15:24:34 +02:00
}
if ( $addit || $upgrademethod == 'addonlynew' || $upgrademethod == 'dumpold' )
{
if ( $message_id && $content )
{
2004-08-04 17:34:14 +02:00
// echo "<br>adding - insert into phpgw_lang values ('$message_id','$app_name','$lang','$content')";
$result = $this -> db -> insert ( $this -> lang_table , array (
'message_id' => $message_id ,
'app_name' => $app_name ,
'lang' => $lang ,
'content' => $content ,
), False , __LINE__ , __FILE__ );
2003-12-14 18:07:16 +01:00
if (( int ) $result <= 0 )
2003-10-04 15:24:34 +02:00
{
echo " <br>Error inserting record: phpgw_lang values (' $message_id ',' $app_name ',' $lang ',' $content ') " ;
}
}
}
}
}
}
}
$this -> db -> transaction_commit ();
// update the ctimes of the installed langsfiles for the autoloading of the lang-files
//
2005-03-04 16:47:08 +01:00
$config = CreateObject ( 'phpgwapi.config' );
$config -> save_value ( 'lang_ctimes' , $GLOBALS [ 'egw_info' ][ 'server' ][ 'lang_ctimes' ], 'phpgwapi' );
2003-10-04 15:24:34 +02:00
}
2005-05-31 15:52:14 +02:00
/**
* re - loads all ( ! ) langfiles if one langfile for the an app and the language of the user has changed
*/
2003-10-04 15:24:34 +02:00
function autoload_changed_langfiles ()
{
//echo "<h1>check_langs()</h1>\n";
2005-03-04 16:47:08 +01:00
if ( $GLOBALS [ 'egw_info' ][ 'server' ][ 'lang_ctimes' ] && ! is_array ( $GLOBALS [ 'egw_info' ][ 'server' ][ 'lang_ctimes' ]))
2003-10-04 15:24:34 +02:00
{
2005-03-04 16:47:08 +01:00
$GLOBALS [ 'egw_info' ][ 'server' ][ 'lang_ctimes' ] = unserialize ( $GLOBALS [ 'egw_info' ][ 'server' ][ 'lang_ctimes' ]);
2003-10-04 15:24:34 +02:00
}
2005-03-04 16:47:08 +01:00
//_debug_array($GLOBALS['egw_info']['server']['lang_ctimes']);
2003-10-04 15:24:34 +02:00
2005-03-04 16:47:08 +01:00
$lang = $GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'common' ][ 'lang' ];
$apps = $GLOBALS [ 'egw_info' ][ 'user' ][ 'apps' ];
2003-10-04 15:24:34 +02:00
$apps [ 'phpgwapi' ] = True ; // check the api too
foreach ( $apps as $app => $data )
{
2005-05-31 15:52:14 +02:00
$fname = EGW_SERVER_ROOT . " / $app /setup/phpgw_ $lang .lang " ;
2003-10-04 15:24:34 +02:00
if ( file_exists ( $fname ))
{
$ctime = filectime ( $fname );
2004-01-25 22:29:20 +01:00
/* This is done to avoid string offset error at least in php5 */
2005-03-04 16:47:08 +01:00
$tmp = $GLOBALS [ 'egw_info' ][ 'server' ][ 'lang_ctimes' ][ $lang ];
2004-01-25 22:29:20 +01:00
$ltime = ( int ) $tmp [ $app ];
unset ( $tmp );
2003-10-04 15:24:34 +02:00
//echo "checking lang='$lang', app='$app', ctime='$ctime', ltime='$ltime'<br>\n";
if ( $ctime != $ltime )
{
// update all langs
2003-10-08 12:50:24 +02:00
$installed = $this -> get_installed_langs ();
2003-10-15 14:29:27 +02:00
//echo "<p>install_langs(".print_r($installed,True).")</p>\n";
2003-10-08 12:50:24 +02:00
$this -> install_langs ( $installed ? array_keys ( $installed ) : array ());
2003-10-04 15:24:34 +02:00
break ;
}
}
}
}
2003-10-05 04:06:11 +02:00
/* Following functions are called for app (un)install */
2005-05-31 15:52:14 +02:00
/**
* gets array of installed languages , e . g . array ( 'de' , 'en' )
*
* @ param boolean $DEBUG = false debug messages or not , default not
* @ return array with installed langs
*/
2003-10-05 04:06:11 +02:00
function get_langs ( $DEBUG = False )
{
if ( $DEBUG )
{
echo '<br>get_langs(): checking db...' . " \n " ;
}
2003-10-05 12:57:50 +02:00
if ( ! $this -> langs )
{
$this -> get_installed_langs ();
}
return $this -> langs ? array_keys ( $this -> langs ) : array ();
2003-10-05 04:06:11 +02:00
}
2005-05-31 15:52:14 +02:00
/**
* delete all lang entries for an application , return True if langs were found
*
* @ param $appname app_name whose translations you want to delete
* @ param boolean $DEBUG = false debug messages or not , default not
* @ return boolean true if $appname had translations installed , false otherwise
*/
2003-10-05 04:06:11 +02:00
function drop_langs ( $appname , $DEBUG = False )
{
if ( $DEBUG )
{
echo '<br>drop_langs(): Working on: ' . $appname ;
}
2004-08-04 17:34:14 +02:00
$this -> db -> select ( $this -> lang_table , 'COUNT(message_id)' , array (
'app_name' => $appname
), __LINE__ , __FILE__ );
2005-05-31 15:52:14 +02:00
if ( $this -> db -> next_record () && $this -> db -> f ( 0 ))
2003-10-05 04:06:11 +02:00
{
2004-08-04 17:34:14 +02:00
$this -> db -> delete ( $this -> lang_table , array (
'app_name' => $appname
), __LINE__ , __FILE__ );
2003-10-05 04:06:11 +02:00
return True ;
}
return False ;
}
2005-05-31 15:52:14 +02:00
/**
* process an application ' s lang files , calling get_langs () to see what langs the admin installed already
*
* @ param string $appname app_name of application to process
* @ param boolean $DEBUG = false debug messages or not , default not
* @ param array / boolean $force_langs = false array with langs to install anyway ( beside the allready installed ones ), or false for none
*/
2003-10-05 04:06:11 +02:00
function add_langs ( $appname , $DEBUG = False , $force_langs = False )
{
$langs = $this -> get_langs ( $DEBUG );
if ( is_array ( $force_langs ))
{
foreach ( $force_langs as $lang )
{
if ( ! in_array ( $lang , $langs ))
{
$langs [] = $lang ;
}
}
}
if ( $DEBUG )
{
echo '<br>add_langs(): chose these langs: ' ;
_debug_array ( $langs );
}
$this -> install_langs ( $langs , 'addmissing' , $appname );
}
2001-06-15 23:29:33 +02:00
}