load($instance->select());
}
return $instance;
}
/**
* Initializes the language system by fetching the appropriate language
*
* @param integer Language ID
*/
function load($languageid)
{
global $bugsys;
$phrases = $bugsys->db->query("
(SELECT varname, phrasetext FROM " . TABLE_PREFIX . "phrase)
UNION ALL
(SELECT varname, phrasetext FROM " . TABLE_PREFIX . "locale WHERE languageid = " . intval($languageid) . ")"
);
while ($phrase = $bugsys->db->fetch_array($phrases))
{
$this->language["$phrase[varname]"] = $phrase['phrasetext'];
}
$bugsys->db->free_result($phrases);
}
/**
* Selects the proper language for the user
*
* @return integer The language ID that will be used
*/
function select()
{
global $bugsys;
if ($bugsys->userinfo['userid'])
{
$languageid = $bugsys->userinfo['languageid'];
$language = $bugsys->datastore['language']["$languageid"];
}
if (!$languageid)
{
foreach ($bugsys->datastore['language'] AS $language)
{
if ($language['default'])
{
$languageid = $language['languageid'];
$language = $bugsys->datastore['language']["$languageid"];
break;
}
}
}
$this->vars['id'] = $language['languageid'];
$this->vars['charset'] = $language['charset'];
$this->vars['direction'] = $language['direction'];
$this->vars['code'] = $language['languagecode'];
return $languageid;
}
/**
* Fetches a phrase from the language array
*
* @param string Phrase name
* @return string The phrase text
*/
function fetch_phrase($phrasename)
{
return $this->language["$phrasename"];
}
/**
* Takes a phrase name and the arguments for it and constructs it
*
* @param string Phrase name
* @param arguments Values for the arguments the phrase takes
* @return string Processed phrase
*/
function p()
{
$obj =& lang::init();
$args = func_get_args();
$numargs = sizeof($args);
if ($numargs < 1)
{
return false;
}
if ($phrasetext = $obj->fetch_phrase($args[0]))
{
if ($numargs < 2)
{
$phrase = $phrasetext;
}
else
{
$args[0] = $phrasetext;
if (($phrase = @call_user_func_array('sprintf', $args)) === false)
{
global $bugsys;
$bugsys->debug("$args[0] reported an sprintf() error; parsing manually");
for ($i = 1; $i < $numargs; $i++)
{
$phrase = str_replace("%{$i}\$s", $args["$i"], $args[0]);
}
}
}
return preg_replace('#%([0-9].*?)\$s#', '[ARG \\1: UNDEFINED]', $phrase);
}
else
{
return "[UNDEFINED PHRASE: $args[0]]";
}
return $phrase;
}
/**
* Fetches a variable from the vars array
*
* @param string Variable name
* @return mixed The value of the variable
*/
function fetch_var($varname)
{
return $this->vars["$varname"];
}
/**
* Returns a variable about the language system
*
* @param string The name of the variable
* @return mixed The value fof the variable
*/
function v($varname)
{
$obj =& lang::init();
return $obj->fetch_var($varname);
}
}
/*=====================================================================*\
|| ###################################################################
|| # $HeadURL$
|| # $Id$
|| ###################################################################
\*=====================================================================*/
?>