ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilLanguage.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4
27{
34 var $ilias;
35
42 var $text;
43
52
62
72
80
88
95 var $separator = "#:#";
96
104
112
117 protected static $used_topics = array();
118
123 protected static $used_modules = array();
127 protected $cached_modules = array();
128
132 protected $map_modules_txt = array();
133
137 protected $usage_log_enabled = false;
138
142 protected static $lng_log = array();
143
154 function __construct($a_lang_key)
155 {
157
158 $this->ilias = $ilias;
159
160 if (!isset($log))
161 {
162 if (is_object($ilias))
163 {
164 require_once "./Services/Logging/classes/class.ilLog.php";
165 $this->log = new ilLog(ILIAS_LOG_DIR,ILIAS_LOG_FILE,$ilias->getClientId(),ILIAS_LOG_ENABLED);
166 }
167 }
168 else
169 {
170 $this->log =& $log;
171 }
172
173 $this->lang_key = $a_lang_key;
174
175 $this->text = array();
176 $this->loaded_modules = array();
177
178 $this->usage_log_enabled = self::isUsageLogEnabled();
179
180 //$this->lang_path = ILIAS_ABSOLUTE_PATH.substr($this->ilias->ini->readVariable("language","path"),1);
181
182 // if no directory was found fall back to default lang dir
183 //if (!is_dir($this->lang_path))
184 //{
185 $this->lang_path = ILIAS_ABSOLUTE_PATH."/lang";
186 //}
187 $this->cust_lang_path = ILIAS_ABSOLUTE_PATH."/Customizing/global/lang";
188
189 $this->lang_default = $ilIliasIniFile->readVariable("language","default");
190 if (is_object($ilSetting) && $ilSetting->get("language") != "")
191 {
192 $this->lang_default = $ilSetting->get("language");
193 }
194 $this->lang_user = $ilUser->prefs["language"];
195
196 $langs = $this->getInstalledLanguages();
197
198 if (!in_array($this->lang_key,$langs))
199 {
200 $this->lang_key = $this->lang_default;
201 }
202
203 require_once('./Services/Language/classes/class.ilCachedLanguage.php');
204 $this->global_cache = ilCachedLanguage::getInstance($this->lang_key);
205 if ($this->global_cache->isActive()) {
206 $this->cached_modules = $this->global_cache->getTranslations();
207 }
208
209 $this->loadLanguageModule("common");
210
211 return true;
212 }
213
214 function getLangKey()
215 {
216 return $this->lang_key;
217 }
218
220 {
221 return $this->lang_default ? $this->lang_default : 'en';
222 }
223
233 function txtlng($a_module, $a_topic, $a_language)
234 {
235 if (strcmp($a_language, $this->lang_key) == 0)
236 {
237 return $this->txt($a_topic);
238 }
239 else
240 {
241 return ilLanguage::_lookupEntry($a_language, $a_module, $a_topic);
242 }
243 }
244
253 function txt($a_topic, $a_default_lang_fallback_mod = "")
254 {
255 if (empty($a_topic))
256 {
257 return "";
258 }
259
260 // remember the used topics
261 self::$used_topics[$a_topic] = $a_topic;
262
263 $translation = "";
264 if (isset($this->text[$a_topic]))
265 {
266 $translation = $this->text[$a_topic];
267 }
268
269 if ($translation == "" && $a_default_lang_fallback_mod != "")
270 {
271 // #13467 - try current language first (could be missing module)
272 if($this->lang_key != $this->lang_default)
273 {
274 $translation = ilLanguage::_lookupEntry($this->lang_key,
275 $a_default_lang_fallback_mod, $a_topic);
276 }
277 // try default language last
278 if($translation == "" || $translation == "-".$a_topic."-")
279 {
280 $translation = ilLanguage::_lookupEntry($this->lang_default,
281 $a_default_lang_fallback_mod, $a_topic);
282 }
283 }
284
285
286 if ($translation == "")
287 {
288 if (ILIAS_LOG_ENABLED && is_object($this->log))
289 {
290 $this->log->writeLanguageLog($a_topic,$this->lang_key);
291 }
292 return "-".$a_topic."-";
293 }
294 else
295 {
296 if($this->usage_log_enabled)
297 {
298 self::logUsage($this->map_modules_txt[$a_topic], $a_topic);
299 }
300 return $translation;
301 }
302 }
303
309 public function exists($a_topic)
310 {
311 return isset($this->text[$a_topic]);
312 }
313
314 function loadLanguageModule ($a_module)
315 {
316 global $ilDB;
317
318 if (in_array($a_module, $this->loaded_modules))
319 {
320 return;
321 }
322
323 $this->loaded_modules[] = $a_module;
324
325 // remember the used modules globally
326 self::$used_modules[$a_module] = $a_module;
327
329
330 if (empty($this->lang_key))
331 {
333 }
334
335 if(is_array($this->cached_modules[$a_module])) {
336 $this->text = array_merge($this->text, $this->cached_modules[$a_module]);
337
338 if($this->usage_log_enabled)
339 {
340 foreach (array_keys($this->cached_modules[$a_module]) as $key)
341 {
342 $this->map_modules_txt[$key] = $a_module;
343 }
344 }
345
346 return;
347 }
348
349/*
350 $query = "SELECT identifier,value FROM lng_data " .
351 "WHERE lang_key = '" . $lang_key."' " .
352 "AND module = '$a_module'";
353 $r = $this->ilias->db->query($query);
354
355 while ($row = $r->fetchRow(ilDBConstants::FETCHMODE_OBJECT))
356 {
357 $this->text[$row->identifier] = $row->value;
358 }
359*/
360
361 $q = "SELECT * FROM lng_modules " .
362 "WHERE lang_key = ".$ilDB->quote($lang_key, "text")." AND module = ".
363 $ilDB->quote($a_module, "text");
364 $r = $ilDB->query($q);
366
367 $new_text = unserialize($row["lang_array"]);
368 if (is_array($new_text))
369 {
370 $this->text = array_merge($this->text, $new_text);
371
372 if($this->usage_log_enabled)
373 {
374 foreach (array_keys($new_text) as $key)
375 {
376 $this->map_modules_txt[$key] = $a_module;
377 }
378 }
379 }
380 }
381
382
384 {
386 }
387
388 static function _getInstalledLanguages()
389 {
390 include_once("./Services/Object/classes/class.ilObject.php");
391 $langlist = ilObject::_getObjectsByType("lng");
392
393 foreach ($langlist as $lang)
394 {
395 if (substr($lang["desc"], 0, 9) == "installed")
396 {
397 $languages[] = $lang["title"];
398 }
399
400 }
401
402 return $languages ? $languages : array();
403 }
404
405 public static function _lookupEntry($a_lang_key, $a_mod, $a_id)
406 {
407 global $ilDB;
408
409 $set = $ilDB->query($q = sprintf("SELECT * FROM lng_data WHERE module = %s ".
410 "AND lang_key = %s AND identifier = %s",
411 $ilDB->quote((string) $a_mod, "text"), $ilDB->quote((string) $a_lang_key, "text"),
412 $ilDB->quote((string) $a_id, "text")));
413 $rec = $ilDB->fetchAssoc($set);
414
415 if ($rec["value"] != "")
416 {
417 // remember the used topics
418 self::$used_topics[$a_id] = $a_id;
419 self::$used_modules[$a_mod] = $a_mod;
420
421 if(self::isUsageLogEnabled())
422 {
423 self::logUsage($a_mod, $a_id);
424 }
425
426 return $rec["value"];
427 }
428
429 return "-".$a_id."-";
430 }
431
438 public static function lookupId($a_lang_key)
439 {
440 global $ilDB;
441
442 $query = 'SELECT obj_id FROM object_data '.' '.
443 'WHERE title = '.$ilDB->quote($a_lang_key, 'text').' '.
444 'AND type = '.$ilDB->quote('lng','text');
445
446 $res = $ilDB->query($query);
447 while($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT))
448 {
449 return $row->obj_id;
450 }
451 return 0;
452 }
453
454
455 function getUsedTopics()
456 {
457 asort(self::$used_topics);
458 return self::$used_topics;
459 }
460
461 function getUsedModules()
462 {
463 asort(self::$used_modules);
464 return self::$used_modules;
465 }
466
468 {
469 return $this->lang_user;
470 }
471
476 public static function getGlobalInstance()
477 {
482 global $ilUser, $ilSetting, $lng;
483
484 if(!ilSession::get('lang') && !$_GET['lang'])
485 {
486 if(
487 $ilUser instanceof ilObjUser &&
488 (!$ilUser->getId() || $ilUser->isAnonymous())
489 )
490 {
491 require_once 'Services/Language/classes/class.ilLanguageDetection.php';
492 $language_detection = new ilLanguageDetection();
493 $language = $language_detection->detect();
494
495 $ilUser->setPref('language', $language);
496 $_GET['lang'] = $language;
497 }
498 }
499
500 if(isset($_POST['change_lang_to']) && $_POST['change_lang_to'] != "")
501 {
502 $_GET['lang'] = ilUtil::stripSlashes($_POST['change_lang_to']);
503 }
504
505 // prefer personal setting when coming from login screen
506 // Added check for ilUser->getId > 0 because it is 0 when the language is changed and the terms of service should be displayed
507 if(
508 $ilUser instanceof ilObjUser &&
509 ($ilUser->getId() && !$ilUser->isAnonymous())
510 )
511 {
512 ilSession::set('lang', $ilUser->getPref('language'));
513 }
514
515 ilSession::set('lang', (isset($_GET['lang']) && $_GET['lang']) ? $_GET['lang'] : ilSession::get('lang'));
516
517 // check whether lang selection is valid
519 if(!in_array(ilSession::get('lang'), $langs))
520 {
521 if($ilSetting instanceof ilSetting && $ilSetting->get('language') != '')
522 {
523 ilSession::set('lang', $ilSetting->get('language'));
524 }
525 else
526 {
527 ilSession::set('lang', $langs[0]);
528 }
529 }
530 $_GET['lang'] = ilSession::get('lang');
531
532 return new self(ilSession::get('lang'));
533 }
534
535 /*
536 * Transfer text to Javascript
537 *
538 * @param string|array $a_lang_key languag key or array of language keys
539 * @param ilTemplate $a_tpl template
540 */
541 function toJS($a_lang_key, ilTemplate $a_tpl = null)
542 {
543 global $tpl;
544
545 if (!is_object($a_tpl))
546 {
547 $a_tpl = $tpl;
548 }
549
550 if (!is_array($a_lang_key))
551 {
552 $a_lang_key = array($a_lang_key);
553 }
554
555 $map = array();
556 foreach ($a_lang_key as $lk)
557 {
558 $map[$lk] = $this->txt($lk);
559 }
560 $this->toJSMap($map, $a_tpl);
561 }
562
569 function toJSMap($a_map, ilTemplate $a_tpl = null)
570 {
571 global $tpl;
572
573 if (!is_object($a_tpl))
574 {
575 $a_tpl = $tpl;
576 }
577
578 if (!is_array($a_map))
579 {
580 return;
581 }
582
583 foreach ($a_map as $k => $v)
584 {
585 if ($v != "")
586 {
587 include_once("./Services/JSON/classes/class.ilJsonUtil.php");
588 $a_tpl->addOnloadCode("il.Language.setLangVar('".$k."', ".ilJsonUtil::encode($v).");");
589 }
590 }
591 }
592
599 protected static function logUsage($a_module, $a_identifier)
600 {
601 if($a_module != "" && $a_identifier != "")
602 {
603 self::$lng_log[$a_identifier] = $a_module;
604 }
605 }
606
615 protected static function isUsageLogEnabled()
616 {
618 global $ilClientIniFile, $ilDB;
619
620 if(!(($ilDB instanceof ilDBMySQL) || ($ilDB instanceof ilDBPdoMySQLMyISAM)) || !$ilClientIniFile instanceof ilIniFile)
621 {
622
623 return false;
624 }
625
626 if(DEVMODE)
627 {
628 return true;
629 }
630
631 if(!$ilClientIniFile->variableExists('system', 'LANGUAGE_LOG'))
632 {
633 return $ilClientIniFile->readVariable('system', 'LANGUAGE_LOG') == 1;
634 }
635 return false;
636 }
637
641 function __destruct()
642 {
643 global $ilDB;
644
645 //case $ilDB not existing should not happen but if something went wrong it shouldn't leads to any failures
646 if(!$this->usage_log_enabled || !(($ilDB instanceof ilDBMySQL) || ($ilDB instanceof ilDBPdoMySQLMyISAM)))
647 {
648 return;
649 }
650
651 foreach((array)self::$lng_log as $identifier => $module)
652 {
653 $wave[] = '(' . $ilDB->quote($module, 'text'). ', ' . $ilDB->quote($identifier, 'text') . ')';
654 unset(self::$lng_log[$identifier]);
655
656 if(count($wave) == 150 || (count(self::$lng_log) == 0 && count($wave) > 0))
657 {
658 $query = 'REPLACE INTO lng_log (module, identifier) VALUES ' . implode(', ', $wave);
659 $ilDB->manipulate($query);
660
661 $wave = array();
662 }
663 }
664 }
665
666} // END class.Language
667?>
sprintf('%.4f', $callTime)
global $tpl
Definition: ilias.php:8
$_GET["client_id"]
$_POST["username"]
An exception for terminatinating execution or to throw for unit testing.
MySQL Database Wrapper.
Class ilDBPdoMySQLMyISAM.
INIFile Parser.
static encode($mixed, $suppress_native=false)
Class ilLanguageDetection.
language handling
loadLanguageModule($a_module)
static $used_modules
toJSMap($a_map, ilTemplate $a_tpl=null)
Transfer text to Javascript.
txtlng($a_module, $a_topic, $a_language)
gets the text for a given topic in a given language if the topic is not in the list,...
static _getInstalledLanguages()
txt($a_topic, $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
static logUsage($a_module, $a_identifier)
saves tupel of language module and identifier
static _lookupEntry($a_lang_key, $a_mod, $a_id)
__destruct()
destructor saves all language usages to db if log is enabled and ilDB exists
static lookupId($a_lang_key)
Lookup obj_id of language @global ilDB $ilDB.
__construct($a_lang_key)
Constructor read the single-language file and put this in an array text.
exists($a_topic)
Check if language entry exists.
toJS($a_lang_key, ilTemplate $a_tpl=null)
logging
Definition: class.ilLog.php:19
static _getObjectsByType($a_obj_type="", $a_owner="")
Get objects by type.
static set($a_var, $a_val)
Set a value.
static get($a_var)
Get a value.
ILIAS Setting Class.
special template class to simplify handling of ITX/PEAR
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
$languages
Definition: cssgen2.php:34
$r
Definition: example_031.php:79
for($i=1; $i<=count($kw_cases_sel); $i+=1) $lang
Definition: langwiz.php:349
redirection script todo: (a better solution should control the processing via a xml file)
global $lng
Definition: privfeed.php:17
global $ilSetting
Definition: privfeed.php:17
global $ilDB
$ilIliasIniFile
$ilUser
Definition: imgupload.php:18