r394: Add access keys to the checkboxes
[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 (defined('DEBUG_LOCALIZE'))
130 {
131 return '[[LOCALIZED: ' . $args[0] . ']]';
132 }
133
134 if ($numargs < 2)
135 {
136 $phrase = $phrasetext;
137 }
138 else
139 {
140 $args[0] = $phrasetext;
141 if (($phrase = @call_user_func_array('sprintf', $args)) === false)
142 {
143 global $bugsys;
144 $bugsys->debug("$args[0] reported an sprintf() error; parsing manually");
145
146 for ($i = 1; $i < $numargs; $i++)
147 {
148 $phrase = str_replace("%{$i}\$s", $args["$i"], $args[0]);
149 }
150 }
151 }
152 return preg_replace('#%([0-9].*?)\$s#', '<strong>[ARG \\1: UNDEFINED]</strong>', $phrase);
153 }
154 else
155 {
156 return "<strong>[UNDEFINED PHRASE: $args[0]]</strong>";
157 }
158
159 return $phrase;
160 }
161
162 /**
163 * Registers a phrase with the temporary text system
164 *
165 * @param string Phrase text
166 *
167 * @return string Phrase text
168 */
169 function r($string)
170 {
171 global $bugsys;
172
173 $mdstring = md5($string);
174
175 if ($regphrase = $bugsys->db->query_first("SELECT * FROM phrase WHERE md5 = '$mdstring'"))
176 {
177 return $regphrase['phrasetext'];
178 }
179 else
180 {
181 $bt = debug_backtrace();
182 $btstr = $bt[0]['file'] . ':' . $bt[0]['line'];
183
184 $bugsys->db->query("REPLACE INTO phraseregistry (md5, phrasetext, file) VALUES ('$mdstring', '" . $bugsys->sanitize($string) . "', '$btstr')");
185 }
186
187 return $string;
188 }
189
190 /**
191 * Fetches a variable from the vars array
192 *
193 * @param string Variable name
194 * @return mixed The value of the variable
195 */
196 function fetch_var($varname)
197 {
198 return $this->vars["$varname"];
199 }
200
201 /**
202 * Returns a variable about the language system
203 *
204 * @param string The name of the variable
205 * @return mixed The value fof the variable
206 */
207 function v($varname)
208 {
209 $obj =& lang::init();
210 return $obj->fetch_var($varname);
211 }
212 }
213
214 /*=====================================================================*\
215 || ###################################################################
216 || # $HeadURL$
217 || # $Id$
218 || ###################################################################
219 \*=====================================================================*/
220 ?>