source: trunk/inc/languages.inc.php

Last change on this file was 2, checked in by george, 14 years ago
  • Přidáno: Trunk revize 13719.
File size: 14.0 KB
Line 
1<?php
2
3/***************************************************************************
4* Dolphin Smart Community Builder
5* -----------------
6* begin : Mon Mar 23 2006
7* copyright : (C) 2006 BoonEx Group
8* website : http://www.boonex.com/
9* This file is part of Dolphin - Smart Community Builder
10*
11* Dolphin is free software. This work is licensed under a Creative Commons Attribution 3.0 License.
12* http://creativecommons.org/licenses/by/3.0/
13*
14* Dolphin is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16* See the Creative Commons Attribution 3.0 License for more details.
17* You should have received a copy of the Creative Commons Attribution 3.0 License along with Dolphin,
18* see license.txt file; if not, write to marketing@boonex.com
19***************************************************************************/
20
21define('BX_DOL_LANGUAGE_CATEGORY_SYSTEM', 1);
22
23if (!defined ('BX_SKIP_INSTALL_CHECK')) {
24 $sCurrentLanguage = getCurrentLangName(false);
25 if( !$sCurrentLanguage ) {
26 echo '<br /><b>Fatal error:</b> Cannot apply localization.';
27 exit;
28 }
29 require_once( BX_DIRECTORY_PATH_ROOT . "langs/lang-{$sCurrentLanguage}.php" );
30}
31
32require_once(BX_DIRECTORY_PATH_INC . 'db.inc.php');
33require_once(BX_DIRECTORY_PATH_INC . 'utils.inc.php');
34require_once(BX_DIRECTORY_PATH_INC . 'profiles.inc.php');
35require_once(BX_DIRECTORY_PATH_INC . 'params.inc.php');
36
37if (!defined ('BX_SKIP_INSTALL_CHECK')) {
38 getCurrentLangName(true);
39}
40
41function getCurrentLangName($isSetCookie = true) {
42 $sLang = '';
43
44 if( !$sLang ) $sLang = tryToGetLang( $_GET['lang'], $isSetCookie );
45 if( !$sLang ) $sLang = tryToGetLang( $_POST['lang'], $isSetCookie );
46 if( !$sLang ) $sLang = tryToGetLang( $_COOKIE['lang'] );
47 if( !$sLang ) $sLang = tryToGetLang( $_SERVER['HTTP_ACCEPT_LANGUAGE'] );
48 if( !$sLang ) $sLang = tryToGetLang( getParam( 'lang_default' ) );
49 if( !$sLang ) $sLang = tryToGetLang( 'en' );
50
51 setlocale(LC_TIME, $sLang.'_'.strtoupper($sLang).'.utf-8', $sLang.'_'.strtoupper($sLang).'.utf8', $sLang.'.utf-8', $sLang.'.utf8', $sLang);
52
53 return $sLang;
54}
55
56function tryToGetLang( $sLangs, $bSetCookie = false ) {
57 $sLangs = trim( $sLangs );
58 if( !$sLangs )
59 return '';
60
61 $sLangs = preg_replace( '/[^a-zA-Z0-9,;-]/m', '', $sLangs ); // we do not need 'q=0.3'. we are using live queue :)
62 $sLangs = strtolower( $sLangs );
63
64 if( !$sLangs )
65 return '';
66
67 $aLangs = explode( ',', $sLangs ); // ru,en-us;q=0.7,en;q=0.3 => array( 'ru' , 'en-us;q=0.7' , 'en;q=0.3' );
68 foreach( $aLangs as $sLang ) {
69 if( !$sLang ) continue;
70
71 list( $sLang ) = explode( ';', $sLang, 2 ); // en-us;q=0.7 => en-us
72 if( !$sLang ) continue;
73
74 // check with country
75 if( checkLangExists( $sLang ) ) {
76 if( $bSetCookie )
77 setLangCookie( $sLang );
78 return $sLang;
79 }
80
81 //drop country
82 list( $sLang, $sCntr ) = explode( '-', $sLang, 2 ); // en-us => en
83 if( !$sLang or !$sCntr ) continue; //no lang or nothing changed
84
85 //check again. without country
86 if( checkLangExists( $sLang ) ) {
87 if( $bSetCookie )
88 setLangCookie( $sLang );
89 return $sLang;
90 }
91 }
92
93 return '';
94}
95
96function checkLangExists( $sLang ) {
97 if (!preg_match('/^[A-Za-z0-9_]+$/', $sLang))
98 return false;
99 if( file_exists( BX_DIRECTORY_PATH_ROOT . "langs/lang-{$sLang}.php" ) )
100 return true;
101
102 // $sQuery = "SELECT `ID` FROM `sys_localization_languages` WHERE `Name` = '$sLang'";
103 // $iLangID = (int)db_value( $sQuery );
104
105 $iLangID = (int)$GLOBALS['MySQL']->fromCache('checkLangExists_'.$sLang, 'getOne', "SELECT `ID` FROM `sys_localization_languages` WHERE `Name` = '$sLang'");
106
107 if( !$iLangID )
108 return false;
109
110 if( compileLanguage( $iLangID ) )
111 return true;
112
113 return false;
114}
115
116function setLangCookie( $sLang ) {
117
118 $iProfileId = getLoggedId();
119
120 if ($iProfileId) {
121 $iLangID = db_value( "SELECT `ID` FROM `sys_localization_languages` WHERE `Name` = '" . process_db_input($sLang) . "'" );
122 if (!$iLangID)
123 $iLangID = 0 ;
124
125 db_res( 'UPDATE `Profiles` SET `LangID` = ' . (int) $iLangID . ' WHERE `ID` = ' . (int) $_COOKIE['memberID'] );
126
127 // recompile profile cache ;
128 createUserDataFile($iProfileId);
129 }
130
131 setcookie( 'lang', '', time() - 60*60*24, '/' );
132 setcookie( 'lang', $sLang, time() + 60*60*24*365, '/' );
133}
134
135/**
136 * Function will generate list of installed languages list;
137 *
138 * @return : Html presentation data;
139 */
140function getLangSwitcher() {
141 global $site;
142
143 $aLangs = getLangsArr();
144 if( count( $aLangs ) < 2 ) {
145 return ;
146 }
147
148 $sOutputCode = null;
149 foreach( $aLangs as $sName => $sLang ) {
150 $sFlag = $site['flags'] . $sName . '.gif';
151 $aTemplateKeys = array (
152
153 'bx_if:item_img' => array (
154 'condition' => ( $sFlag ),
155 'content' => array (
156 'item_img_src' => $sFlag,
157 'item_img_alt' => $sName,
158 'item_img_width' => 18,
159 'item_img_height' => 12,
160 ),
161 ),
162
163 'item_link' => BX_DOL_URL_ROOT . 'index.php?lang=' . $sName,
164 'item_onclick' => null,
165 'item_title' => $sLang,
166 'extra_info' => null,
167 );
168
169 $sOutputCode .= $GLOBALS['oSysTemplate']->parseHtmlByName( 'member_menu_sub_item.html', $aTemplateKeys );
170 }
171
172 return $sOutputCode;
173}
174
175function getLangsArr( $bAddFlag = false, $bRetIDs = false ) {
176 $rLangs = db_res('SELECT * FROM `sys_localization_languages` ORDER BY `Title` ASC');
177
178 $aLangs = array();
179 while( $aLang = mysql_fetch_assoc($rLangs) ) {
180 $sFlag = '';
181 $sFlag = $bAddFlag ? ( $aLang['Flag'] ? $aLang['Flag'] : 'xx' ) : '';
182
183 $sKey = ($bRetIDs) ? $aLang['ID'] : $aLang['Name'];
184 $aLangs[ $sKey ] = $aLang['Title'] . $sFlag;
185 }
186
187 return $aLangs;
188}
189
190function deleteLanguage($langID = 0) {
191 $langID = (int)$langID;
192
193 if($langID <= 0) return false;
194
195 $resLangs = db_res('
196 SELECT `ID`, `Name`
197 FROM `sys_localization_languages`
198 WHERE `ID` = '.$langID);
199
200 if(mysql_num_rows($resLangs) <= 0) return false;
201
202 $arrLang = mysql_fetch_assoc($resLangs);
203
204 $numStrings = db_res('
205 SELECT COUNT(`IDKey`)
206 FROM `sys_localization_strings`
207 WHERE `IDLanguage` = '.$langID);
208 $numStrings = mysql_fetch_row($numStrings);
209 $numStrings = $numStrings[0];
210
211 db_res('DELETE FROM `sys_localization_strings` WHERE `IDLanguage` = '.$langID);
212
213 if(db_affected_rows() < $numStrings) return false;
214
215 db_res('DELETE FROM `sys_localization_languages` WHERE `ID` = '.$langID);
216
217 if(db_affected_rows() <= 0) return false;
218
219 @unlink( BX_DIRECTORY_PATH_ROOT . 'langs/lang-'.$arrLang['Name'].'.php');
220
221 // delete from email templates
222 $sQuery = "DELETE FROM `sys_email_templates` WHERE `LangID` = '{$langID}'";
223 db_res($sQuery);
224
225 return true;
226}
227
228function getLocalizationKeys() {
229 $resKeys = db_res('SELECT `ID`, `IDCategory`, `Key` FROM `sys_localization_keys`');
230
231 $arrKeys = array();
232
233 while($arr = mysql_fetch_assoc($resKeys)) {
234 $ID = $arr['ID'];
235 unset($arr['ID']);
236 $arrKeys[$ID] = $arr;
237 }
238
239 return $arrKeys;
240}
241
242function getLocalizationStringParams($keyID) {
243 $keyID = (int)$keyID;
244
245 $resParams = db_res("
246 SELECT `IDParam`,
247 `Description`
248 FROM `sys_localization_string_params`
249 WHERE `IDKey` = $keyID
250 ORDER BY `IDParam`
251 ");
252
253 $arrParams = array();
254
255 while ($arr = mysql_fetch_assoc($resParams)) {
256 $arrParams[(int)$arr['IDParam']] = $arr['Description'];
257 }
258
259 return $arrParams;
260}
261
262function getLocalizationCategories() {
263 $resCategories = db_res('SELECT `ID`, `Name` FROM `sys_localization_categories` ORDER BY `Name`');
264
265 $arrCategories = array();
266
267 while ($arr = mysql_fetch_assoc($resCategories)) {
268 $arrCategories[$arr['ID']] = $arr['Name'];
269 }
270
271 return $arrCategories;
272}
273
274function compileLanguage($langID = 0) {
275 $langID = (int)$langID;
276
277 $newLine = "\r\n";
278
279 if($langID <= 0) {
280 $resLangs = db_res('SELECT `ID`, `Name` FROM `sys_localization_languages`');
281 } else {
282 $resLangs = db_res('
283 SELECT `ID`, `Name`
284 FROM `sys_localization_languages`
285 WHERE `ID` = '.$langID
286 );
287 }
288
289 if ( mysql_num_rows($resLangs) <= 0 )
290 return false;
291
292 while($arrLanguage = mysql_fetch_assoc($resLangs)) {
293 $resKeysStrings = db_res("
294 SELECT `sys_localization_keys`.`Key` AS `Key`,
295 `sys_localization_strings`.`String` AS `String`
296 FROM `sys_localization_strings` INNER JOIN
297 `sys_localization_keys` ON
298 `sys_localization_keys`.`ID` = `sys_localization_strings`.`IDKey`
299 WHERE `sys_localization_strings`.`IDLanguage` = {$arrLanguage['ID']}");
300
301 $handle = fopen( BX_DIRECTORY_PATH_ROOT . "langs/lang-{$arrLanguage['Name']}.php", 'w');
302
303 if($handle === false) return false;
304
305 $fileContent = "<?{$newLine}\$LANG = array(";
306
307 while($arrKeyString = mysql_fetch_assoc($resKeysStrings)) {
308 $langKey = str_replace("\\", "\\\\", $arrKeyString['Key']);
309 $langKey = str_replace("'", "\\'", $langKey);
310
311 $langStr = str_replace("\\", "\\\\", $arrKeyString['String']);
312 $langStr = str_replace("'", "\\'", $langStr);
313
314 $fileContent .= "{$newLine}\t'$langKey' => '$langStr',";
315 }
316
317 $fileContent = trim($fileContent, ',');
318
319 $writeResult = fwrite($handle, $fileContent."{$newLine});?>");
320 if($writeResult === false) return false;
321
322 if(fclose($handle) === false) return false;
323
324 @chmod( BX_DIRECTORY_PATH_ROOT . "langs/lang-{$arrLanguage['Name']}.php", 0666);
325 }
326
327 return true;
328}
329
330function addStringToLanguage($langKey, $langString, $langID = -1, $categoryID = BX_DOL_LANGUAGE_CATEGORY_SYSTEM) {
331 // input validation
332 $langID = (int)$langID;
333 $categoryID = (int)$categoryID;
334
335 if ( $langID == -1 ) {
336 $resLangs = db_res('SELECT `ID`, `Name` FROM `sys_localization_languages`');
337 } else {
338 $resLangs = db_res('
339 SELECT `ID`, `Name`
340 FROM `sys_localization_languages`
341 WHERE `ID` = '.$langID);
342 }
343
344 $langKey = process_db_input($langKey, BX_TAGS_STRIP);
345 $langString = process_db_input($langString, BX_TAGS_VALIDATE);
346
347 $resInsertKey = db_res( "
348 INSERT INTO `sys_localization_keys`
349 SET `IDCategory` = $categoryID,
350 `Key` = '$langKey'", false );
351 if ( !$resInsertKey || db_affected_rows() <= 0 )
352 return false;
353
354 $keyID = db_last_id();
355
356 while($arrLanguage = mysql_fetch_assoc($resLangs)) {
357 $resInsertString = db_res( "
358 INSERT INTO `sys_localization_strings`
359 SET `IDKey` = $keyID,
360 `IDLanguage` = {$arrLanguage['ID']},
361 `String` = '$langString'", false );
362 if ( !$resInsertString || db_affected_rows() <= 0 )
363 return false;
364
365 compileLanguage($arrLanguage['ID']);
366 }
367
368 return true;
369}
370
371function updateStringInLanguage($langKey, $langString, $langID = -1) {
372 // input validation
373 $langID = (int)$langID;
374
375 if ( $langID == -1 ) {
376 $resLangs = db_res('SELECT `ID`, `Name` FROM `sys_localization_languages`');
377 } else {
378 $resLangs = db_res('
379 SELECT `ID`, `Name`
380 FROM `sys_localization_languages`
381 WHERE `ID` = '.$langID);
382 }
383
384 $langKey = process_db_input($langKey, BX_TAGS_STRIP);
385 $langString = process_db_input($langString, BX_TAGS_VALIDATE);
386
387 $arrKey = db_arr( "
388 SELECT `ID`
389 FROM `sys_localization_keys`
390 WHERE `Key` = '$langKey'", false );
391
392 if ( !$arrKey )
393 return false;
394
395 $keyID = $arrKey['ID'];
396
397 while($arrLanguage = mysql_fetch_assoc($resLangs)) {
398 $resUpdateString = db_res( "
399 UPDATE `sys_localization_strings`
400 SET `String` = '$langString'
401 WHERE `IDKey` = $keyID
402 AND `IDLanguage` = {$arrLanguage['ID']}", false );
403 if ( !$resUpdateString || db_affected_rows() <= 0 )
404 return false;
405 }
406
407 return true;
408}
409
410function deleteStringFromLanguage($langKey, $langID = -1) {
411 // input validation
412 $langID = (int)$langID;
413
414 if ( $langID == -1 ) {
415 $resLangs = db_res('SELECT `ID`, `Name` FROM `sys_localization_languages`');
416 } else {
417 $resLangs = db_res('
418 SELECT `ID`, `Name`
419 FROM `sys_localization_languages`
420 WHERE `ID` = '.$langID);
421 }
422
423 $langKey = process_db_input($langKey, BX_TAGS_STRIP);
424 $langString = process_db_input($langString, BX_TAGS_VALIDATE);
425
426 $arrKey = db_arr( "
427 SELECT `ID`
428 FROM `sys_localization_keys`
429 WHERE `Key` = '$langKey'", false );
430
431 if ( !$arrKey )
432 return false;
433
434 $keyID = $arrKey['ID'];
435
436 while($arrLanguage = mysql_fetch_assoc($resLangs)) {
437 $resDeleteString = db_res( "
438 DELETE FROM `sys_localization_strings`
439 WHERE `IDKey` = $keyID
440 AND `IDLanguage` = {$arrLanguage['ID']}", false );
441 if ( !$resDeleteString || db_affected_rows() <= 0 )
442 return false;
443 }
444
445 $resDeleteKey = db_res( "
446 DELETE FROM `sys_localization_keys`
447 WHERE `Key` = '$langKey' LIMIT 1", false );
448
449 return !$resDeleteKey || db_affected_rows() <= 0 ? false : true;
450}
451
452function _t_action( $str, $arg0 = "", $arg1 = "", $arg2 = "" ) {
453 return MsgBox( _t($str,$arg0,$arg1,$arg2) );
454}
455
456function _t_echo_action( $str, $arg0 = "", $arg1 = "", $arg2 = "" ) {
457 return MsgBox( _t($str,$arg0,$arg1,$arg2) );
458}
459
460function echo_t_err( $str, $arg0 = "", $arg1 = "", $arg2 = "" ) {
461 return MsgBox( _t($str,$arg0,$arg1,$arg2) );
462}
463
464function _t_err( $str, $arg0 = "", $arg1 = "", $arg2 = "" ) {
465 return MsgBox( _t($str,$arg0,$arg1,$arg2) );
466}
467
468function _t($key, $arg0 = "", $arg1 = "", $arg2 = "") {
469 global $LANG;
470
471 if(isset($LANG[$key])) {
472 $str = $LANG[$key];
473 $str = str_replace('{0}', $arg0, $str);
474 $str = str_replace('{1}', $arg1, $str);
475 $str = str_replace('{2}', $arg2, $str);
476 return $str;
477 } else {
478 return $key;
479 }
480}
481
482function _t_ext($key, $args) {
483 global $LANG;
484
485 if(isset($LANG[$key])) {
486 $str = $LANG[$key];
487
488 if(!is_array($args)) {
489 return str_replace('{0}', $args, $str);
490 }
491
492 foreach ($args as $key => $val) {
493 $str = str_replace('{'.$key.'}', $val, $str);
494 }
495
496 return $str;
497 } else {
498 return $key;
499 }
500}
501
502?>
Note: See TracBrowser for help on using the repository browser.