1 | <?php |
---|
2 | /**************************************************************************\ |
---|
3 | * eGroupWare API - Translation class for phpgw lang files * |
---|
4 | * This file written by Miles Lott <milosch@phpgroupware.org> * |
---|
5 | * Handles multi-language support using flat files * |
---|
6 | * -------------------------------------------------------------------------* |
---|
7 | * This library is part of the eGroupWare API * |
---|
8 | * http://www.egroupware.org/api * |
---|
9 | * ------------------------------------------------------------------------ * |
---|
10 | * This library is free software; you can redistribute it and/or modify it * |
---|
11 | * under the terms of the GNU Lesser General Public License as published by * |
---|
12 | * the Free Software Foundation; either version 2.1 of the License, * |
---|
13 | * or any later version. * |
---|
14 | * This library is distributed in the hope that it will be useful, but * |
---|
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * |
---|
17 | * See the GNU Lesser General Public License for more details. * |
---|
18 | * You should have received a copy of the GNU Lesser General Public License * |
---|
19 | * along with this library; if not, write to the Free Software Foundation, * |
---|
20 | * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
---|
21 | \**************************************************************************/ |
---|
22 | |
---|
23 | |
---|
24 | // This should be considered experimental. It works, at the app level. |
---|
25 | // But, for admin and prefs it really slows things down. See the note |
---|
26 | // in the translate() function. |
---|
27 | // To use, set $GLOBALS['phpgw_info']['server']['translation_system'] = 'file'; in |
---|
28 | // class.translation.inc.php |
---|
29 | class translation |
---|
30 | { |
---|
31 | var $langarray; // Currently loaded translations |
---|
32 | var $loaded_apps = array(); // Loaded app langs |
---|
33 | |
---|
34 | /*! |
---|
35 | @function translate |
---|
36 | @abstract Translate phrase to user selected lang |
---|
37 | @param $key phrase to translate |
---|
38 | @param $vars vars sent to lang function, passed to us |
---|
39 | */ |
---|
40 | function translation($appname='phpgwapi',$loadlang='') |
---|
41 | { |
---|
42 | global $lang; |
---|
43 | if($loadlang) |
---|
44 | { |
---|
45 | $lang = $loadlang; |
---|
46 | } |
---|
47 | $this->add_app($appname,$lang); |
---|
48 | |
---|
49 | if(!isset($GLOBALS['phpgw_setup'])) |
---|
50 | { |
---|
51 | $this->system_charset = $GLOBALS['phpgw_info']['server']['system_charset']; |
---|
52 | } |
---|
53 | else |
---|
54 | { |
---|
55 | $GLOBALS['phpgw']->db->query("SELECT config_value FROM phpgw_config WHERE config_app='phpgwapi' AND config_name='system_charset'",__LINE__,__FILE__); |
---|
56 | if($GLOBALS['phpgw']->db->next_record()) |
---|
57 | { |
---|
58 | $this->system_charset = $GLOBALS['phpgw']->db->f(0); |
---|
59 | } |
---|
60 | } |
---|
61 | // load multi-byte-string-extension if needed, and set its internal encodeing to your system_charset |
---|
62 | if($this->system_charset && substr($this->system_charset,0,9) != 'iso-8859-1') |
---|
63 | { |
---|
64 | if($this->mbstring = extension_loaded('mbstring') || @dl(PHP_SHLIB_PREFIX.'mbstring.'.PHP_SHLIB_SUFFIX)) |
---|
65 | { |
---|
66 | ini_set('mbstring.internal_encoding',$this->system_charset); |
---|
67 | if(ini_get('mbstring.func_overload') < 7) |
---|
68 | { |
---|
69 | if($warnings) echo "<p>Warning: Please set <b>mbstring.func_overload = 7</b> in your php.ini for useing <b>$this->system_charset</b> as your charset !!!</p>\n"; |
---|
70 | } |
---|
71 | } |
---|
72 | else |
---|
73 | { |
---|
74 | if($warnings) echo "<p>Warning: Please get and/or enable the <b>mbstring extension</b> in your php.ini for useing <b>$this->system_charset</b> as your charset, we are defaulting to <b>iconv</b> for now !!!</p>\n"; |
---|
75 | } |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | function init() |
---|
80 | { |
---|
81 | // post-nuke and php-nuke are using $GLOBALS['lang'] too |
---|
82 | // but not as array! |
---|
83 | // this produces very strange results |
---|
84 | if (!is_array($GLOBALS['lang'])) |
---|
85 | { |
---|
86 | $GLOBALS['lang'] = array(); |
---|
87 | } |
---|
88 | |
---|
89 | if ($GLOBALS['phpgw_info']['user']['preferences']['common']['lang']) |
---|
90 | { |
---|
91 | $this->userlang = $GLOBALS['phpgw_info']['user']['preferences']['common']['lang']; |
---|
92 | } |
---|
93 | $this->add_app('common'); |
---|
94 | if (!count($GLOBALS['lang'])) |
---|
95 | { |
---|
96 | $this->userlang = 'en'; |
---|
97 | $this->add_app('common'); |
---|
98 | } |
---|
99 | $this->add_app($GLOBALS['phpgw_info']['flags']['currentapp']); |
---|
100 | } |
---|
101 | /* |
---|
102 | @function charset |
---|
103 | @abstract returns the charset to use (!$lang) or the charset of the lang-files or $lang |
---|
104 | */ |
---|
105 | function charset($lang=False) |
---|
106 | { |
---|
107 | if($lang) |
---|
108 | { |
---|
109 | if(!isset($this->charsets[$lang])) |
---|
110 | { |
---|
111 | $GLOBALS['phpgw']->db->query("SELECT content FROM phpgw_lang WHERE lang='$lang' AND message_id='charset' AND app_name='common'",__LINE__,__FILE__); |
---|
112 | $this->charsets[$lang] = $GLOBALS['phpgw']->db->next_record() ? strtolower($GLOBALS['phpgw']->db->f(0)) : 'iso-8859-1'; |
---|
113 | } |
---|
114 | return $this->charsets[$lang]; |
---|
115 | } |
---|
116 | if($this->system_charset) // do we have a system-charset ==> return it |
---|
117 | { |
---|
118 | return $this->system_charset; |
---|
119 | } |
---|
120 | // if no translations are loaded (system-startup) use a default, else lang('charset') |
---|
121 | return empty($GLOBALS['lang']['charset']) ? 'iso-8859-1' : strtolower($this->translate('charset')); |
---|
122 | } |
---|
123 | |
---|
124 | function isin_array($needle,$haystack) |
---|
125 | { |
---|
126 | while(list($k,$v) = each($haystack)) |
---|
127 | { |
---|
128 | if($v == $needle) |
---|
129 | { |
---|
130 | return True; |
---|
131 | } |
---|
132 | } |
---|
133 | return False; |
---|
134 | } |
---|
135 | |
---|
136 | function translate($key, $vars=False) |
---|
137 | { |
---|
138 | if(!$this->isin_array($GLOBALS['phpgw_info']['flags']['currentapp'],$this->loaded_apps) && |
---|
139 | $GLOBALS['phpgw_info']['flags']['currentapp'] != 'home') |
---|
140 | { |
---|
141 | //echo '<br>loading app "' . $GLOBALS['phpgw_info']['flags']['currentapp'] . '" for the first time.'; |
---|
142 | $this->add_app($GLOBALS['phpgw_info']['flags']['currentapp'],$GLOBALS['lang']); |
---|
143 | } |
---|
144 | elseif($GLOBALS['phpgw_info']['flags']['currentapp'] == 'admin' || |
---|
145 | $GLOBALS['phpgw_info']['flags']['currentapp'] == 'preferences') |
---|
146 | { |
---|
147 | // This is done because for these two apps, all langs must be loaded. |
---|
148 | // Note we cannot load for navbar, since it would slow down EVERY page. |
---|
149 | // This is true until all common/admin/prefs langs are in the api file only. |
---|
150 | @ksort($GLOBALS['phpgw_info']['apps']); |
---|
151 | while(list($x,$app) = @each($GLOBALS['phpgw_info']['apps'])) |
---|
152 | { |
---|
153 | if(!$this->isin_array($app['name'],$this->loaded_apps)) |
---|
154 | { |
---|
155 | //echo '<br>loading app "' . $app['name'] . '" for the first time.'; |
---|
156 | $this->add_app($app['name'],$GLOBALS['lang']); |
---|
157 | } |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | if(!$vars) |
---|
162 | { |
---|
163 | $vars = array(); |
---|
164 | } |
---|
165 | |
---|
166 | $ret = $key; |
---|
167 | |
---|
168 | if(isset($this->langarray[strtolower($key)]) && $this->langarray[strtolower($key)]) |
---|
169 | { |
---|
170 | $ret = $this->langarray[strtolower($key)]; |
---|
171 | } |
---|
172 | else |
---|
173 | { |
---|
174 | $ret = $key."*"; |
---|
175 | } |
---|
176 | $ndx = 1; |
---|
177 | while( list($key,$val) = each( $vars ) ) |
---|
178 | { |
---|
179 | $ret = preg_replace( "/%$ndx/", $val, $ret ); |
---|
180 | ++$ndx; |
---|
181 | } |
---|
182 | return $ret; |
---|
183 | } |
---|
184 | |
---|
185 | function autoload_changed_langfiles() |
---|
186 | { |
---|
187 | return; |
---|
188 | } |
---|
189 | function drop_langs($appname,$DEBUG=False) |
---|
190 | { |
---|
191 | if($DEBUG) |
---|
192 | { |
---|
193 | echo '<br>drop_langs(): Working on: ' . $appname; |
---|
194 | echo '<br>drop_langs(): Not needed with file-based lang class.'; |
---|
195 | } |
---|
196 | } |
---|
197 | |
---|
198 | function add_langs($appname,$DEBUG=False,$force_langs=False) |
---|
199 | { |
---|
200 | if($DEBUG) |
---|
201 | { |
---|
202 | echo '<br>add_langs(): Working on: ' . $appname; |
---|
203 | echo '<br>add_langs(): Not needed with file-based lang class.'; |
---|
204 | } |
---|
205 | } |
---|
206 | |
---|
207 | /*! |
---|
208 | @function add_app |
---|
209 | @abstract loads all app phrases into langarray |
---|
210 | @param $lang user lang variable (defaults to en) |
---|
211 | */ |
---|
212 | function add_app($app,$lang='') |
---|
213 | { |
---|
214 | define('SEP',filesystem_separator()); |
---|
215 | |
---|
216 | //echo '<br>add_app(): called with app="' . $app . '", lang="' . $userlang . '"'; |
---|
217 | if(!isset($lang) || !$lang) |
---|
218 | { |
---|
219 | if(isset($GLOBALS['phpgw_info']['user']['preferences']['common']['lang']) && |
---|
220 | $GLOBALS['phpgw_info']['user']['preferences']['common']['lang']) |
---|
221 | { |
---|
222 | $userlang = $GLOBALS['phpgw_info']['user']['preferences']['common']['lang']; |
---|
223 | } |
---|
224 | else |
---|
225 | { |
---|
226 | $userlang = 'en'; |
---|
227 | } |
---|
228 | } |
---|
229 | else |
---|
230 | { |
---|
231 | $userlang = $lang; |
---|
232 | } |
---|
233 | |
---|
234 | $fn = PHPGW_SERVER_ROOT . SEP . $app . SEP . 'setup' . SEP . 'phpgw_' . $userlang . '.lang'; |
---|
235 | if(!file_exists($fn)) |
---|
236 | { |
---|
237 | $fn = PHPGW_SERVER_ROOT . SEP . $app . SEP . 'setup' . SEP . 'phpgw_en.lang'; |
---|
238 | } |
---|
239 | |
---|
240 | if(file_exists($fn)) |
---|
241 | { |
---|
242 | $fp = fopen($fn,'r'); |
---|
243 | while($data = fgets($fp,8000)) |
---|
244 | { |
---|
245 | list($message_id,$app_name,$null,$content) = explode("\t",$data); |
---|
246 | //echo '<br>add_app(): adding phrase: $this->langarray["'.$message_id.'"]=' . trim($content); |
---|
247 | $this->langarray[$message_id] = trim($content); |
---|
248 | } |
---|
249 | fclose($fp); |
---|
250 | } |
---|
251 | // stuff class array listing apps that are included already |
---|
252 | $this->loaded_apps[] = $app; |
---|
253 | } |
---|
254 | } |
---|
255 | ?> |
---|