r265: Unloading more language tasks from includes/init.php to includes/class_language.php
[bugdar.git] / includes / class_language.php
1 <?php
2 /*=====================================================================*\
3 || ################################################################### ||
4 || # app [#]version[#]
5 || # --------------------------------------------------------------- # ||
6 || # Copyright ©2002-[#]year[#] by Iris Studios, Inc. All Rights Reserved. # ||
7 || # This file may not be reproduced in any way without permission. # ||
8 || # --------------------------------------------------------------- # ||
9 || # User License Agreement at http://www.iris-studios.com/license/ # ||
10 || ################################################################### ||
11 \*=====================================================================*/
12
13 /**
14 * Language management class. Because this uses Simpleton we want to
15 * keep names as short as short as possible
16 */
17 class lang
18 {
19 var $language = array();
20 var $vars = array();
21
22 /**
23 * Singleton initializer
24 *
25 * @param integer Language ID
26 * @return object An instance of the class
27 */
28 function init($languageid = 0)
29 {
30 static $instance;
31
32 if (!$instance)
33 {
34 $instance = new lang();
35 $instance->load($instance->select());
36 }
37
38 return $instance;
39 }
40
41 /**
42 * Initializes the language system by fetching the appropriate language
43 *
44 * @param integer Language ID
45 */
46 function load($languageid)
47 {
48 global $bugsys;
49
50 $phrases = $bugsys->db->query("
51 (SELECT varname, phrasetext FROM " . TABLE_PREFIX . "phrase)
52 UNION ALL
53 (SELECT varname, phrasetext FROM " . TABLE_PREFIX . "locale WHERE languageid = " . intval($languageid) . ")"
54 );
55 while ($phrase = $bugsys->db->fetch_array($phrases))
56 {
57 $this->language["$phrase[varname]"] = $phrase['phrasetext'];
58 }
59 $bugsys->db->free_result($phrases);
60 }
61
62 /**
63 * Selects the proper language for the user
64 *
65 * @return integer The language ID that will be used
66 */
67 function select()
68 {
69 global $bugsys;
70
71 if ($bugsys->userinfo['userid'])
72 {
73 $languageid = $bugsys->userinfo['languageid'];
74 $language = $bugsys->datastore['language']["$languageid"];
75 }
76 if (!$languageid)
77 {
78 foreach ($bugsys->datastore['language'] AS $language)
79 {
80 if ($language['default'])
81 {
82 $languageid = $language['languageid'];
83 $language = $bugsys->datastore['language']["$languageid"];
84 break;
85 }
86 }
87 }
88
89 $this->vars['id'] = $language['languageid'];
90 $this->vars['charset'] = $language['charset'];
91 $this->vars['direction'] = $language['direction'];
92 $this->vars['code'] = $language['languagecode'];
93
94 return $languageid;
95 }
96
97 /**
98 * Fetches a phrase from the language array
99 *
100 * @param string Phrase name
101 * @return string The phrase text
102 */
103 function fetch_phrase($phrasename)
104 {
105 return $this->language["$phrasename"];
106 }
107
108 /**
109 * Takes a phrase name and the arguments for it and constructs it
110 *
111 * @param string Phrase name
112 * @param arguments Values for the arguments the phrase takes
113 * @return string Processed phrase
114 */
115 function p()
116 {
117 $obj =& lang::init();
118
119 $args = func_get_args();
120 $numargs = sizeof($args);
121
122 if ($numargs < 1)
123 {
124 return false;
125 }
126
127 if ($phrasetext = $obj->fetch_phrase($args[0]))
128 {
129 if ($numargs < 2)
130 {
131 $phrase = $phrasetext;
132 }
133 else
134 {
135 $args[0] = $phrasetext;
136 if (($phrase = @call_user_func_array('sprintf', $args)) === false)
137 {
138 global $bugsys;
139 $bugsys->debug("$args[0] reported an sprintf() error; parsing manually");
140
141 for ($i = 1; $i < $numargs; $i++)
142 {
143 $phrase = str_replace("%{$i}\$s", $args["$i"], $args[0]);
144 }
145 }
146 }
147 return preg_replace('#%([0-9].*?)\$s#', '<strong>[ARG \\1: UNDEFINED]</strong>', $phrase);
148 }
149 else
150 {
151 return "<strong>[UNDEFINED PHRASE: $args[0]]</strong>";
152 }
153
154 return $phrase;
155 }
156
157 /**
158 * Fetches a variable from the vars array
159 *
160 * @param string Variable name
161 * @return mixed The value of the variable
162 */
163 function fetch_var($varname)
164 {
165 return $this->vars["$varname"];
166 }
167
168 /**
169 * Returns a variable about the language system
170 *
171 * @param string The name of the variable
172 * @return mixed The value fof the variable
173 */
174 function v($varname)
175 {
176 $obj =& lang::init();
177 return $obj->fetch_var($varname);
178 }
179 }
180
181 /*=====================================================================*\
182 || ###################################################################
183 || # $HeadURL$
184 || # $Id$
185 || ###################################################################
186 \*=====================================================================*/
187 ?>