00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00045 class ilLanguage
00046 {
00052 var $text = array();
00053
00060 var $lang_default = "en";
00061
00069 var $lang_path;
00070
00076 var $lang_key;
00077
00083 var $separator = "#:#";
00084
00090 var $comment_separator = "###";
00091
00101 function ilLanguage($a_lang_key)
00102 {
00103 $this->lang_key = ($a_lang_key) ? $a_lang_key : $this->lang_default;
00104
00105 $this->lang_path = ILIAS_ABSOLUTE_PATH."/lang";
00106
00107
00108 $txt = file($this->lang_path."/setup_lang_sel_multi.lang");
00109
00110
00111 if (is_array($txt))
00112 {
00113 foreach ($txt as $row)
00114 {
00115 if ($row[0] != "#")
00116 {
00117 $a = explode($this->separator,trim($row));
00118 $this->text[trim($a[0])] = trim($a[1]);
00119 }
00120 }
00121 }
00122
00123
00124 $txt = file($this->lang_path."/setup_".$this->lang_key.".lang");
00125
00126
00127 if (is_array($txt))
00128 {
00129 foreach ($txt as $row)
00130 {
00131 if ($row[0] != "#")
00132 {
00133 $a = explode($this->separator,trim($row));
00134 $this->text[trim($a[0])] = trim($a[1]);
00135 }
00136 }
00137
00138 return true;
00139 }
00140
00141 return false;
00142 }
00143
00152 function txt($a_topic)
00153 {
00154 global $log;
00155
00156 if (empty($a_topic))
00157 {
00158 return "";
00159 }
00160
00161 $translation = $this->text[$a_topic];
00162
00163
00164 $pos = strpos($translation, $this->comment_separator);
00165
00166 if ($pos !== false)
00167 {
00168
00169 $translation = substr($translation,0,$pos);
00170 }
00171
00172 if ($translation == "")
00173 {
00174 $log->writeLanguageLog($a_topic,$this->lang_key);
00175 return "-".$a_topic."-";
00176 }
00177 else
00178 {
00179 return $translation;
00180 }
00181 }
00182
00190 function getLanguages()
00191 {
00192 $d = dir($this->lang_path);
00193 $tmpPath = getcwd();
00194 chdir ($this->lang_path);
00195
00196
00197 while ($entry = $d->read())
00198 {
00199 if (is_file($entry) && (ereg ("(^setup_.{2}\.lang$)", $entry)))
00200 {
00201 $lang_key = substr($entry,6,2);
00202 $languages[] = $lang_key;
00203 }
00204 }
00205
00206 chdir($tmpPath);
00207
00208 return $languages;
00209 }
00210
00217 function installLanguages($a_lang_keys, $a_local_keys)
00218 {
00219 if (empty($a_lang_keys))
00220 {
00221 $a_lang_keys = array();
00222 }
00223
00224 if (empty($a_local_keys))
00225 {
00226 $a_local_keys = array();
00227 }
00228
00229 $err_lang = array();
00230
00231 $this->flushLanguages();
00232
00233 $db_langs = $this->getAvailableLanguages();
00234
00235 foreach ($a_lang_keys as $lang_key)
00236 {
00237 if ($this->checkLanguage($lang_key))
00238 {
00239 $this->insertLanguage($lang_key);
00240
00241 if (in_array($lang_key, $a_local_keys))
00242 {
00243 if ($this->checkLanguage($lang_key, "local"))
00244 {
00245 $this->insertLanguage($lang_key, "local");
00246 }
00247 else
00248 {
00249 $err_lang[] = $lang_key;
00250 }
00251 }
00252
00253
00254 if (!array_key_exists($lang_key,$db_langs))
00255 {
00256 if (in_array($lang_key, $a_local_keys))
00257 {
00258 $query = "INSERT INTO object_data ".
00259 "(type,title,description,owner,create_date,last_update) ".
00260 "VALUES ".
00261 "('lng','".$lang_key."','installed_local','-1',now(),now())";
00262 }
00263 else
00264 {
00265 $query = "INSERT INTO object_data ".
00266 "(type,title,description,owner,create_date,last_update) ".
00267 "VALUES ".
00268 "('lng','".$lang_key."','installed','-1',now(),now())";
00269 }
00270 $this->db->query($query);
00271 }
00272 }
00273 else
00274 {
00275 $err_lang[] = $lang_key;
00276 }
00277 }
00278
00279 foreach ($db_langs as $key => $val)
00280 {
00281 if (!in_array($key,$err_lang))
00282 {
00283 if (in_array($key,$a_lang_keys))
00284 {
00285 if (in_array($key, $a_local_keys))
00286 {
00287 $query = "UPDATE object_data SET " .
00288 "description = 'installed_local', " .
00289 "last_update = now() " .
00290 "WHERE obj_id='".$val["obj_id"]."' " .
00291 "AND type='lng'";
00292 }
00293 else
00294 {
00295 $query = "UPDATE object_data SET " .
00296 "description = 'installed', " .
00297 "last_update = now() " .
00298 "WHERE obj_id='".$val["obj_id"]."' " .
00299 "AND type='lng'";
00300 }
00301 $this->db->query($query);
00302 }
00303 else
00304 {
00305 if (substr($val["status"], 0, 9) == "installed")
00306 {
00307 $query = "UPDATE object_data SET " .
00308 "description = 'not_installed', " .
00309 "last_update = now() " .
00310 "WHERE obj_id='" . $val["obj_id"] . "' " .
00311 "AND type='lng'";
00312 $this->db->query($query);
00313 }
00314 }
00315 }
00316 }
00317
00318 return ($err_lang) ? $err_lang : true;
00319 }
00320
00326 function getInstalledLanguages()
00327 {
00328 $arr = array();
00329
00330 $query = "SELECT * FROM object_data ".
00331 "WHERE type = 'lng' ".
00332 "AND description LIKE 'installed%'";
00333 $r = $this->db->query($query);
00334
00335 while ($row = $r->fetchRow(DB_FETCHMODE_OBJECT))
00336 {
00337 $arr[] = $row->title;
00338 }
00339
00340 return $arr;
00341 }
00342
00348 function getInstalledLocalLanguages()
00349 {
00350 $arr = array();
00351
00352 $query = "SELECT * FROM object_data ".
00353 "WHERE type = 'lng' ".
00354 "AND description = 'installed_local'";
00355 $r = $this->db->query($query);
00356
00357 while ($row = $r->fetchRow(DB_FETCHMODE_OBJECT))
00358 {
00359 $arr[] = $row->title;
00360 }
00361
00362 return $arr;
00363 }
00364
00369 function getAvailableLanguages()
00370 {
00371 $arr = array();
00372
00373 $query = "SELECT * FROM object_data ".
00374 "WHERE type = 'lng'";
00375 $r = $this->db->query($query);
00376
00377 while ($row = $r->fetchRow(DB_FETCHMODE_OBJECT))
00378 {
00379 $arr[$row->title]["obj_id"] = $row->obj_id;
00380 $arr[$row->title]["status"] = $row->description;
00381 }
00382
00383 return $arr;
00384 }
00385
00397 function checkLanguage($a_lang_key, $scope = '')
00398 {
00399 if (!empty($scope))
00400 {
00401 if ($scope == 'global')
00402 {
00403 $scope = '';
00404 }
00405 else
00406 {
00407 $scopeExtension = '.' . $scope;
00408 }
00409 }
00410
00411 $tmpPath = getcwd();
00412 chdir($this->lang_path);
00413
00414
00415 $lang_file = "ilias_" . $a_lang_key . ".lang" . $scopeExtension;
00416
00417
00418 if (!is_file($lang_file))
00419 {
00420 chdir($tmpPath);
00421 return false;
00422 }
00423
00424
00425 if (!$content = $this->cut_header(file($lang_file)))
00426 {
00427 chdir($tmpPath);
00428 return false;
00429 }
00430
00431
00432 foreach ($content as $key => $val)
00433 {
00434 $separated = explode($this->separator, trim($val));
00435 $num = count($separated);
00436
00437 if ($num != 3)
00438 {
00439 chdir($tmpPath);
00440 return false;
00441 }
00442 }
00443
00444 chdir($tmpPath);
00445
00446
00447 return true;
00448 }
00449
00460 function cut_header($content)
00461 {
00462 foreach ($content as $key => $val)
00463 {
00464 if (trim($val) == "<!-- language file start -->")
00465 {
00466 return array_slice($content,$key +1);
00467 }
00468 }
00469
00470 return false;
00471 }
00472
00476 function flushLanguages()
00477 {
00478 $query = "DELETE FROM lng_data";
00479 $this->db->query($query);
00480 }
00481
00482
00490 function insertLanguage($lang_key, $scope = '')
00491 {
00492 $ilDB =& $this->db;
00493
00494 $lang_array = array();
00495
00496 if (!empty($scope))
00497 {
00498 if ($scope == 'global')
00499 {
00500 $scope = '';
00501 }
00502 else
00503 {
00504 $scopeExtension = '.' . $scope;
00505 }
00506 }
00507
00508 $tmpPath = getcwd();
00509 chdir($this->lang_path);
00510
00511 $lang_file = "ilias_" . $lang_key . ".lang" . $scopeExtension;
00512
00513 if ($lang_file)
00514 {
00515
00516 if ($content = $this->cut_header(file($lang_file)))
00517 {
00518 foreach ($content as $key => $val)
00519 {
00520 $separated = explode($this->separator,trim($val));
00521
00522
00523 $pos = strpos($separated[2], $this->comment_separator);
00524
00525 if ($pos !== false)
00526 {
00527
00528 $separated[2] = substr($separated[2] , 0 , $pos);
00529 }
00530
00531 if (empty($scope))
00532 {
00533 $query = "INSERT INTO lng_data ".
00534 "(module,identifier,lang_key,value) ".
00535 "VALUES ".
00536 "('" . $separated[0] . "','" . $separated[1] . "','" . $lang_key . "','" . addslashes($separated[2]) . "')";
00537 $lang_array[$separated[0]][$separated[1]] = $separated[2];
00538 }
00539 else if ($scope == 'local')
00540 {
00541 $query = "UPDATE lng_data SET ".
00542 "module = '" . $separated[0] . "', " .
00543 "identifier = '" . $separated[1] . "', " .
00544 "lang_key = '" . $lang_key . "', " .
00545 "value = '" . addslashes($separated[2]) . "' " .
00546 "WHERE module = '" . $separated[0] . "' " .
00547 "AND identifier = '" . $separated[1] . "' " .
00548 "AND lang_key = '" . $lang_key . "'";
00549 $lang_array[$separated[0]][$separated[1]] = $separated[2];
00550 }
00551 $this->db->query($query);
00552 }
00553 }
00554
00555
00556 $lang_array[$separated[0]][$separated[1]] = $separated[2];
00557
00558 foreach($lang_array as $module => $lang_arr)
00559 {
00560 if ($scope == "local")
00561 {
00562 $q = "SELECT * FROM lng_modules WHERE ".
00563 " lang_key = ".$ilDB->quote($this->key).
00564 " AND module = ".$ilDB->quote($module);
00565 $set = $ilDB->query($q);
00566 $row = $set->fetchRow(DB_FETCHMODE_ASSOC);
00567 $arr2 = unserialize($row["lang_array"]);
00568 if (is_array($arr2))
00569 {
00570 $lang_arr = array_merge($arr2, $lang_arr);
00571 }
00572 }
00573 $query = "REPLACE INTO lng_modules (lang_key, module, lang_array) VALUES ".
00574 "(".$ilDB->quote($lang_key).", " .
00575 " ".$ilDB->quote($module).", " .
00576 " ".$ilDB->quote(serialize($lang_arr)).") ";
00577 $ilDB->query($query);
00578
00579 }
00580 }
00581
00582 chdir($tmpPath);
00583 }
00584
00590 function getLocalLanguages()
00591 {
00592 $local_langs = array();
00593 $d = dir($this->lang_path);
00594 $tmpPath = getcwd();
00595 chdir ($this->lang_path);
00596
00597
00598 while ($entry = $d->read())
00599 {
00600 if (is_file($entry) && (ereg ("(^ilias_.{2}\.lang.local$)", $entry)))
00601 {
00602 $lang_key = substr($entry,6,2);
00603 $local_langs[] = $lang_key;
00604 }
00605 }
00606
00607 chdir($tmpPath);
00608
00609 return $local_langs;
00610 }
00611
00612 function getInstallableLanguages()
00613 {
00614 $setup_langs = $this->getLanguages();
00615
00616 $d = dir($this->lang_path);
00617 $tmpPath = getcwd();
00618 chdir ($this->lang_path);
00619
00620
00621 while ($entry = $d->read())
00622 {
00623 if (is_file($entry) && (ereg ("(^ilias_.{2}\.lang$)", $entry)))
00624 {
00625 $lang_key = substr($entry,6,2);
00626 $languages1[] = $lang_key;
00627 }
00628 }
00629
00630
00631
00632 chdir($tmpPath);
00633
00634 return $languages1;
00635 }
00636
00642 function setDbHandler($a_db_handler)
00643 {
00644 if (empty($a_db_handler) or !is_object($a_db_handler))
00645 {
00646 return false;
00647 }
00648
00649 $this->db =& $a_db_handler;
00650
00651 return true;
00652 }
00653 }
00654 ?>