From c438e55b44d72e1bede18f6e96b893c1861ccb28 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Fri, 10 Jun 2005 06:13:13 +0000 Subject: [PATCH] r258: Switching to the new class_language.php phrase system --- includes/class_language.php | 130 ++++++++++++++++++++++++++++++++++++ includes/functions.php | 38 ----------- includes/init.php | 15 +---- 3 files changed, 133 insertions(+), 50 deletions(-) create mode 100644 includes/class_language.php diff --git a/includes/class_language.php b/includes/class_language.php new file mode 100644 index 0000000..a945256 --- /dev/null +++ b/includes/class_language.php @@ -0,0 +1,130 @@ +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); + } + + /** + * Singleton initializer + * + * @param integer Language ID + */ + function init($languageid = 0) + { + static $instance; + + if (!$instance) + { + if (!$languageid) + { + trigger_error('No language selected', E_USER_ERROR); + } + + $instance = new lang($languageid); + } + + return $instance; + } + + /** + * 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) + { + 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; + } +} + +/*=====================================================================*\ +|| ################################################################### +|| # $HeadURL$ +|| # $Id$ +|| ################################################################### +\*=====================================================================*/ +?> \ No newline at end of file diff --git a/includes/functions.php b/includes/functions.php index 36fdd3b..ab8f55c 100755 --- a/includes/functions.php +++ b/includes/functions.php @@ -10,44 +10,6 @@ || ################################################################### || \*=====================================================================*/ -// ########################### Start phrase ########################## -function phrase() -{ - global $bugsys; - - $args = func_get_args(); - $numargs = sizeof($args); - - if ($numargs < 1) - { - return false; - } - - if ($phrasetext = $bugsys->language["$args[0]"]) - { - if ($numargs < 2) - { - $phrase = $phrasetext; - } - else - { - $args[0] = $phrasetext; - if (($phrase = @call_user_func_array('sprintf', $args)) === false) - { - 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]]"; - } -} - // ################## Start fetch_user_display_name ################## // preps a dispaly name if one isn't set // should be able to be removed by the final version as registration should set this diff --git a/includes/init.php b/includes/init.php index 44325a0..c5e463d 100755 --- a/includes/init.php +++ b/includes/init.php @@ -57,10 +57,9 @@ require_once('./includes/functions_datastore.php'); require_once('./includes/functions.php'); // ################################################################### -// init the big four +// init the big three $bugsys->options = array(); $bugsys->userinfo = array(); -$bugsys->language = array(); $bugsys->datastore = array(); // ################################################################### @@ -133,16 +132,8 @@ $bugsys->options['lang_code'] = $language['languagecode']; // ################################################################### // load language information -$phrases = $db->query(" - (SELECT varname, phrasetext FROM " . TABLE_PREFIX . "phrase) - UNION ALL - (SELECT varname, phrasetext FROM " . TABLE_PREFIX . "locale WHERE languageid = " . $bugsys->options['lang_id'] . ")" -); -while ($phrase = $db->fetch_array($phrases)) -{ - $bugsys->language["$phrase[varname]"] = $phrase['phrasetext']; -} -$db->free_result($phrases); +require_once('./includes/class_language.php'); +$bugsys->lang =& lang::init($languageid); // ################################################################### // Initialize usergroup system -- 2.22.5