2001-01-11 10:52:33 +01:00
|
|
|
<?php
|
|
|
|
/**************************************************************************\
|
2001-01-13 11:18:50 +01:00
|
|
|
* phpGroupWare API - Translation class for SQL *
|
|
|
|
* This file written by Joseph Engo <jengo@phpgroupware.org> *
|
|
|
|
* and Dan Kuykendall <seek3r@phpgroupware.org> *
|
2002-02-20 05:30:19 +01:00
|
|
|
* and Miles Lott <milosch@phpgroupware.org> *
|
|
|
|
* Handles multi-language support using SQL *
|
2001-01-13 11:18:50 +01:00
|
|
|
* Copyright (C) 2000, 2001 Joseph Engo *
|
|
|
|
* -------------------------------------------------------------------------*
|
2001-01-16 14:52:32 +01:00
|
|
|
* This library is part of the phpGroupWare API *
|
|
|
|
* http://www.phpgroupware.org/api *
|
|
|
|
* ------------------------------------------------------------------------ *
|
2001-01-13 11:18:50 +01: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 *
|
2001-01-11 10:52:33 +01:00
|
|
|
\**************************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
2001-06-15 23:29:33 +02:00
|
|
|
class translation
|
|
|
|
{
|
2002-02-20 05:30:19 +01:00
|
|
|
var $lang = array();
|
2002-10-19 00:53:12 +02:00
|
|
|
var $userlang = '';
|
2002-02-20 05:30:19 +01:00
|
|
|
var $currentapp = '';
|
2002-02-20 05:44:19 +01:00
|
|
|
var $loaded = False;
|
2002-05-31 10:29:19 +02:00
|
|
|
var $translator_helper = '*';
|
2002-02-20 05:30:19 +01:00
|
|
|
|
|
|
|
/*!
|
|
|
|
@function translation
|
2002-02-20 05:57:49 +01:00
|
|
|
@abstract class constructor - sets class vars for currentapp and user lang preference
|
|
|
|
@discussion User lang defaults to 'en'.
|
2002-02-20 05:30:19 +01:00
|
|
|
*/
|
|
|
|
function translation()
|
2001-06-15 23:29:33 +02:00
|
|
|
{
|
2002-02-20 05:30:19 +01:00
|
|
|
if(isset($GLOBALS['phpgw_info']['user']['preferences']['common']['lang']) &&
|
|
|
|
$GLOBALS['phpgw_info']['user']['preferences']['common']['lang'])
|
2001-11-23 20:35:45 +01:00
|
|
|
{
|
2002-02-20 05:30:19 +01:00
|
|
|
$this->userlang = $GLOBALS['phpgw_info']['user']['preferences']['common']['lang'];
|
2001-11-23 20:35:45 +01:00
|
|
|
}
|
2002-02-20 05:30:19 +01:00
|
|
|
|
|
|
|
$this->currentapp = $GLOBALS['phpgw_info']['flags']['currentapp'];
|
2002-02-20 05:44:19 +01:00
|
|
|
}
|
2002-02-20 05:30:19 +01:00
|
|
|
|
2002-02-20 05:57:49 +01:00
|
|
|
/*!
|
|
|
|
@function load_langs
|
|
|
|
@abstract loads up translations into $this->lang based on currentapp and userlang preference.
|
|
|
|
@discussion This also loads translations with appname='common' or appname='all'.
|
|
|
|
*/
|
2002-02-20 05:44:19 +01:00
|
|
|
function load_langs()
|
|
|
|
{
|
2002-10-19 00:53:12 +02:00
|
|
|
if($this->userlang == '')
|
|
|
|
{
|
|
|
|
$this->userlang = $GLOBALS['phpgw_info']['user']['preferences']['common']['lang'] ?
|
|
|
|
$GLOBALS['phpgw_info']['user']['preferences']['common']['lang'] : 'en';
|
|
|
|
}
|
2002-02-20 05:30:19 +01:00
|
|
|
$sql = "SELECT message_id,content FROM phpgw_lang WHERE lang LIKE '" . $this->userlang
|
|
|
|
. "' AND (app_name LIKE '" . $this->currentapp
|
|
|
|
. "' OR app_name LIKE 'common' OR app_name LIKE 'all') ORDER BY app_name ";
|
2002-02-17 19:43:31 +01:00
|
|
|
|
2002-02-20 05:30:19 +01:00
|
|
|
if(strcasecmp($this->currentapp, 'common') > 0)
|
2002-02-17 19:43:31 +01:00
|
|
|
{
|
2002-02-20 05:30:19 +01:00
|
|
|
$sql .= 'ASC';
|
2002-02-17 19:43:31 +01:00
|
|
|
}
|
2002-02-20 05:30:19 +01:00
|
|
|
else
|
2001-06-15 23:29:33 +02:00
|
|
|
{
|
2002-02-20 05:30:19 +01:00
|
|
|
$sql .= 'DESC';
|
|
|
|
}
|
2001-01-11 10:52:33 +01:00
|
|
|
|
2002-02-20 05:30:19 +01:00
|
|
|
$GLOBALS['phpgw']->db->query($sql,__LINE__,__FILE__);
|
|
|
|
$GLOBALS['phpgw']->db->next_record();
|
2001-01-11 10:52:33 +01:00
|
|
|
|
2002-02-20 05:30:19 +01:00
|
|
|
$count = $GLOBALS['phpgw']->db->num_rows();
|
|
|
|
for($idx = 0; $idx < $count; ++$idx)
|
|
|
|
{
|
|
|
|
$this->lang[strtolower($GLOBALS['phpgw']->db->f('message_id'))] = $GLOBALS['phpgw']->db->f('content');
|
2001-08-31 10:45:44 +02:00
|
|
|
$GLOBALS['phpgw']->db->next_record();
|
2001-06-15 23:29:33 +02:00
|
|
|
}
|
2002-02-20 05:30:19 +01:00
|
|
|
/* Done stuffing the array. If someone prefers to have $GLOBALS['lang'] set to this as before,
|
|
|
|
it could be done here. - $GLOBALS['lang'] = $this->lang;
|
|
|
|
*/
|
2002-02-20 05:44:19 +01:00
|
|
|
$this->loaded = True;
|
2002-02-20 05:30:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@function translate
|
|
|
|
@abstract Return the translated string from $this->lang, if it exists. If no translation exists, return the same string with an asterisk.
|
|
|
|
@discussion This should be called from the global function lang(), not directly.
|
2002-02-20 05:57:49 +01:00
|
|
|
@syntax translate('translate x',array('replacement')); OR translate('translate this');
|
|
|
|
@example translate('translate this x', array('lang entry')); returns 'Translate this Lang Entry' or 'translate this lang x*'
|
2002-02-20 05:30:19 +01:00
|
|
|
*/
|
|
|
|
function translate($key,$vars=False)
|
|
|
|
{
|
|
|
|
if(!$vars)
|
|
|
|
{
|
|
|
|
$vars = array();
|
|
|
|
}
|
|
|
|
$ret = $key;
|
|
|
|
$_key = strtolower($key);
|
|
|
|
|
2002-02-20 05:44:19 +01:00
|
|
|
if(!@isset($this->lang[$_key]) && !$this->loaded)
|
|
|
|
{
|
|
|
|
$this->load_langs();
|
|
|
|
}
|
|
|
|
|
2002-05-31 10:29:19 +02:00
|
|
|
$ret = @isset($this->lang[$_key]) ? $this->lang[$_key] : $key . $this->translator_helper;
|
2002-02-17 19:43:31 +01:00
|
|
|
|
2001-06-15 23:29:33 +02:00
|
|
|
$ndx = 1;
|
2002-02-17 19:43:31 +01:00
|
|
|
while(list($key,$val) = each($vars))
|
2001-06-15 23:29:33 +02:00
|
|
|
{
|
|
|
|
$ret = preg_replace( "/%$ndx/", $val, $ret );
|
|
|
|
++$ndx;
|
|
|
|
}
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
2002-02-20 05:30:19 +01:00
|
|
|
/*!
|
|
|
|
@function add_app
|
|
|
|
@abstract Add an additional app's translations to $this->lang
|
|
|
|
*/
|
2001-06-15 23:29:33 +02:00
|
|
|
function add_app($app)
|
|
|
|
{
|
2002-02-20 05:30:19 +01:00
|
|
|
$sql = "SELECT message_id,content FROM phpgw_lang WHERE lang LIKE '" . $this->userlang
|
|
|
|
. "' AND app_name LIKE '" . $app . "'";
|
|
|
|
|
2001-08-31 10:45:44 +02:00
|
|
|
$GLOBALS['phpgw']->db->query($sql,__LINE__,__FILE__);
|
|
|
|
$GLOBALS['phpgw']->db->next_record();
|
2002-02-20 05:30:19 +01:00
|
|
|
|
2001-08-31 10:45:44 +02:00
|
|
|
$count = $GLOBALS['phpgw']->db->num_rows();
|
2002-02-17 19:43:31 +01:00
|
|
|
for($idx = 0; $idx < $count; ++$idx)
|
2001-06-15 23:29:33 +02:00
|
|
|
{
|
2002-02-20 05:30:19 +01:00
|
|
|
$this->lang[strtolower($GLOBALS['phpgw']->db->f('message_id'))] = $GLOBALS['phpgw']->db->f('content');
|
2001-08-31 10:45:44 +02:00
|
|
|
$GLOBALS['phpgw']->db->next_record();
|
2001-06-15 23:29:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|