ILIAS  release_7 Revision v7.30-3-g800a261c036
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 public $ilias;
35
42 public $text;
43
52
61 public $lang_user;
62
71 public $lang_path;
72
79 public $lang_key;
80
87 public $lang_name;
88
95 public $separator = "#:#";
96
103 public $comment_separator = "###";
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();
144 protected $log;
145 protected $global_cache;
146
157 public function __construct($a_lang_key)
158 {
159 global $DIC;
160 $ilIliasIniFile = $DIC->iliasIni();
161
162 $this->log = $DIC->logger()->root();
163
164 $this->lang_key = $a_lang_key;
165
166 $this->text = array();
167 $this->loaded_modules = array();
168
169 $this->usage_log_enabled = self::isUsageLogEnabled();
170
171 $this->lang_path = ILIAS_ABSOLUTE_PATH . "/lang";
172 $this->cust_lang_path = ILIAS_ABSOLUTE_PATH . "/Customizing/global/lang";
173
174 $this->lang_default = $ilIliasIniFile->readVariable("language", "default");
175
176 if ($DIC->offsetExists('ilSetting')) {
177 $ilSetting = $DIC->settings();
178 if ($ilSetting->get("language") != "") {
179 $this->lang_default = $ilSetting->get("language");
180 }
181 }
182 if ($DIC->offsetExists('ilUser')) {
183 $ilUser = $DIC->user();
184 $this->lang_user = $ilUser->prefs["language"];
185 }
186
187 $langs = $this->getInstalledLanguages();
188
189 if (!in_array($this->lang_key, $langs)) {
190 $this->lang_key = $this->lang_default;
191 }
192
193 require_once('./Services/Language/classes/class.ilCachedLanguage.php');
194 $this->global_cache = ilCachedLanguage::getInstance($this->lang_key);
195 if ($this->global_cache->isActive()) {
196 $this->cached_modules = $this->global_cache->getTranslations();
197 }
198
199 $this->loadLanguageModule("common");
200
201 return true;
202 }
203
204 public function getLangKey()
205 {
206 return $this->lang_key;
207 }
208
209 public function getDefaultLanguage()
210 {
211 return $this->lang_default ? $this->lang_default : 'en';
212 }
213
214 public function getTextDirection()
215 {
216 $rtl = array('ar', 'fa', 'ur', 'he');
217 if (in_array($this->getContentLanguage(), $rtl)) {
218 return 'rtl';
219 }
220 return 'ltr';
221 }
222
223 public function getContentLanguage()
224 {
225 if ($this->getUserLanguage()) {
226 return $this->getUserLanguage();
227 }
228 return $this->getLangKey();
229 }
230
240 public function txtlng($a_module, $a_topic, $a_language)
241 {
242 if (strcmp($a_language, $this->lang_key) == 0) {
243 return $this->txt($a_topic);
244 } else {
245 return ilLanguage::_lookupEntry($a_language, $a_module, $a_topic);
246 }
247 }
248
257 public function txt($a_topic, $a_default_lang_fallback_mod = "")
258 {
259 if (empty($a_topic)) {
260 return "";
261 }
262
263 // remember the used topics
264 self::$used_topics[$a_topic] = $a_topic;
265
266 $translation = "";
267 if (isset($this->text[$a_topic])) {
268 $translation = $this->text[$a_topic];
269 }
270
271 if ($translation == "" && $a_default_lang_fallback_mod != "") {
272 // #13467 - try current language first (could be missing module)
273 if ($this->lang_key != $this->lang_default) {
274 $translation = ilLanguage::_lookupEntry(
275 $this->lang_key,
276 $a_default_lang_fallback_mod,
277 $a_topic
278 );
279 }
280 // try default language last
281 if ($translation == "" || $translation == "-" . $a_topic . "-") {
282 $translation = ilLanguage::_lookupEntry(
283 $this->lang_default,
284 $a_default_lang_fallback_mod,
285 $a_topic
286 );
287 }
288 }
289
290
291 if ($translation == "") {
292 if (ILIAS_LOG_ENABLED && is_object($this->log)) {
293 $this->log->debug("Language (" . $this->lang_key . "): topic -" . $a_topic . "- not present");
294 }
295 return "-" . $a_topic . "-";
296 } else {
297 if ($this->usage_log_enabled) {
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 public function loadLanguageModule($a_module)
315 {
316 global $DIC;
317 $ilDB = $DIC->database();
318
319 if (in_array($a_module, $this->loaded_modules)) {
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)) {
332 }
333
334 if (isset($this->cached_modules[$a_module]) && is_array($this->cached_modules[$a_module])) {
335 $this->text = array_merge($this->text, $this->cached_modules[$a_module]);
336
337 if ($this->usage_log_enabled) {
338 foreach (array_keys($this->cached_modules[$a_module]) as $key) {
339 $this->map_modules_txt[$key] = $a_module;
340 }
341 }
342
343 return;
344 }
345
346 $q = "SELECT * FROM lng_modules " .
347 "WHERE lang_key = " . $ilDB->quote($lang_key, "text") . " AND module = " .
348 $ilDB->quote($a_module, "text");
349 $r = $ilDB->query($q);
350 $row = $r->fetchRow(ilDBConstants::FETCHMODE_ASSOC);
351
352 if ($row === false) {
353 return;
354 }
355
356 $new_text = unserialize($row["lang_array"]);
357 if (is_array($new_text)) {
358 $this->text = array_merge($this->text, $new_text);
359
360 if ($this->usage_log_enabled) {
361 foreach (array_keys($new_text) as $key) {
362 $this->map_modules_txt[$key] = $a_module;
363 }
364 }
365 }
366 }
367
368
369 public function getInstalledLanguages()
370 {
372 }
373
374 public static function _getInstalledLanguages()
375 {
376 include_once("./Services/Object/classes/class.ilObject.php");
377 $langlist = ilObject::_getObjectsByType("lng");
378
379 foreach ($langlist as $lang) {
380 if (substr($lang["desc"], 0, 9) == "installed") {
381 $languages[] = $lang["title"];
382 }
383 }
384
385 return $languages ? $languages : array();
386 }
387
388 public static function _lookupEntry($a_lang_key, $a_mod, $a_id)
389 {
390 global $DIC;
391 $ilDB = $DIC->database();
392
393 $set = $ilDB->query($q = sprintf(
394 "SELECT * FROM lng_data WHERE module = %s " .
395 "AND lang_key = %s AND identifier = %s",
396 $ilDB->quote((string) $a_mod, "text"),
397 $ilDB->quote((string) $a_lang_key, "text"),
398 $ilDB->quote((string) $a_id, "text")
399 ));
400 $rec = $ilDB->fetchAssoc($set);
401
402 if ($rec["value"] != "") {
403 // remember the used topics
404 self::$used_topics[$a_id] = $a_id;
405 self::$used_modules[$a_mod] = $a_mod;
406
407 if (self::isUsageLogEnabled()) {
408 self::logUsage($a_mod, $a_id);
409 }
410
411 return $rec["value"];
412 }
413
414 return "-" . $a_id . "-";
415 }
416
423 public static function lookupId($a_lang_key)
424 {
425 global $DIC;
426 $ilDB = $DIC->database();
427
428 $query = 'SELECT obj_id FROM object_data ' . ' ' .
429 'WHERE title = ' . $ilDB->quote($a_lang_key, 'text') . ' ' .
430 'AND type = ' . $ilDB->quote('lng', 'text');
431
432 $res = $ilDB->query($query);
433 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
434 return $row->obj_id;
435 }
436 return 0;
437 }
438
439
440 public function getUsedTopics()
441 {
442 asort(self::$used_topics);
443 return self::$used_topics;
444 }
445
446 public function getUsedModules()
447 {
448 asort(self::$used_modules);
449 return self::$used_modules;
450 }
451
452 public function getUserLanguage()
453 {
454 return $this->lang_user;
455 }
456
461 public static function getFallbackInstance()
462 {
463 return new self('en');
464 }
465
470 public static function getGlobalInstance()
471 {
472 global $DIC;
473 $ilSetting = $DIC->settings();
474 if ($DIC->offsetExists('ilUser')) {
475 $ilUser = $DIC->user();
476 }
477
478 if (!ilSession::get('lang') && !$_GET['lang']) {
479 if (
480 $ilUser instanceof ilObjUser &&
481 (!$ilUser->getId() || $ilUser->isAnonymous())
482 ) {
483 require_once 'Services/Language/classes/class.ilLanguageDetection.php';
484 $language_detection = new ilLanguageDetection();
485 $language = $language_detection->detect();
486
487 $ilUser->setPref('language', $language);
488 $_GET['lang'] = $language;
489 }
490 }
491
492 if (isset($_POST['change_lang_to']) && $_POST['change_lang_to'] != "") {
493 $_GET['lang'] = ilUtil::stripSlashes($_POST['change_lang_to']);
494 }
495
496 // prefer personal setting when coming from login screen
497 // Added check for ilUser->getId > 0 because it is 0 when the language is changed and the terms of service should be displayed
498 if (
499 $ilUser instanceof ilObjUser &&
500 ($ilUser->getId() && !$ilUser->isAnonymous())
501 ) {
502 ilSession::set('lang', $ilUser->getPref('language'));
503 }
504
505 ilSession::set('lang', (isset($_GET['lang']) && $_GET['lang']) ? $_GET['lang'] : ilSession::get('lang'));
506
507 // check whether lang selection is valid
509 if (!in_array(ilSession::get('lang'), $langs)) {
510 if ($ilSetting instanceof ilSetting && $ilSetting->get('language') != '') {
511 ilSession::set('lang', $ilSetting->get('language'));
512 } else {
513 ilSession::set('lang', $langs[0]);
514 }
515 }
516 $_GET['lang'] = ilSession::get('lang');
517
518 return new self(ilSession::get('lang'));
519 }
520
521 /*
522 * Transfer text to Javascript
523 *
524 * @param string|array $a_lang_key languag key or array of language keys
525 * @param ilTemplate $a_tpl template
526 */
527 public function toJS($a_lang_key, ilGlobalTemplateInterface $a_tpl = null)
528 {
529 global $DIC;
530 $tpl = $DIC['tpl'];
531
532 if (!is_object($a_tpl)) {
533 $a_tpl = $tpl;
534 }
535
536 if (!is_array($a_lang_key)) {
537 $a_lang_key = array($a_lang_key);
538 }
539
540 $map = array();
541 foreach ($a_lang_key as $lk) {
542 $map[$lk] = $this->txt($lk);
543 }
544 $this->toJSMap($map, $a_tpl);
545 }
546
553 public function toJSMap($a_map, ilGlobalTemplateInterface $a_tpl = null)
554 {
555 global $DIC;
556 $tpl = $DIC['tpl'];
557
558 if (!is_object($a_tpl)) {
559 $a_tpl = $tpl;
560 }
561
562 if (!is_array($a_map)) {
563 return;
564 }
565
566 foreach ($a_map as $k => $v) {
567 if ($v != "") {
568 include_once("./Services/JSON/classes/class.ilJsonUtil.php");
569 $a_tpl->addOnloadCode("il.Language.setLangVar('" . $k . "', " . ilJsonUtil::encode($v) . ");");
570 }
571 }
572 }
573
580 protected static function logUsage($a_module, $a_identifier)
581 {
582 if ($a_module != "" && $a_identifier != "") {
583 self::$lng_log[$a_identifier] = $a_module;
584 }
585 }
586
595 protected static function isUsageLogEnabled()
596 {
597 global $DIC;
598 $ilClientIniFile = $DIC->clientIni();
599 $ilDB = $DIC->database();
600
601 if (!(($ilDB instanceof ilDBMySQL) || ($ilDB instanceof ilDBPdoMySQLMyISAM)) || !$ilClientIniFile instanceof ilIniFile) {
602 return false;
603 }
604
605 if (defined("DEVMODE") && DEVMODE) {
606 return true;
607 }
608
609 if (!$ilClientIniFile->variableExists('system', 'LANGUAGE_LOG')) {
610 return $ilClientIniFile->readVariable('system', 'LANGUAGE_LOG') == 1;
611 }
612 return false;
613 }
614
618 public function __destruct()
619 {
620 global $DIC;
621
622 //case $ilDB not existing should not happen but if something went wrong it shouldn't leads to any failures
623 if (!$this->usage_log_enabled || !$DIC->isDependencyAvailable("database")) {
624 return;
625 }
626
627 $ilDB = $DIC->database();
628
629 foreach ((array) self::$lng_log as $identifier => $module) {
630 $wave[] = '(' . $ilDB->quote($module, 'text') . ', ' . $ilDB->quote($identifier, 'text') . ')';
631 unset(self::$lng_log[$identifier]);
632
633 if (count($wave) == 150 || (count(self::$lng_log) == 0 && count($wave) > 0)) {
634 $query = 'REPLACE INTO lng_log (module, identifier) VALUES ' . implode(', ', $wave);
635 $ilDB->manipulate($query);
636
637 $wave = array();
638 }
639 }
640 }
641} // END class.Language
$_GET["client_id"]
$_POST["username"]
An exception for terminatinating execution or to throw for unit testing.
Class ilDBPdoMySQLMyISAM.
INIFile Parser.
static encode($mixed, $suppress_native=false)
Class ilLanguageDetection.
language handling
static getFallbackInstance()
Builds a global default language instance.
loadLanguageModule($a_module)
static $used_modules
toJS($a_lang_key, ilGlobalTemplateInterface $a_tpl=null)
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 isUsageLogEnabled()
checks if language usage log is enabled you need MySQL to use this function this function is automati...
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.
toJSMap($a_map, ilGlobalTemplateInterface $a_tpl=null)
Transfer text to Javascript.
static getGlobalInstance()
Builds the global language object.
exists($a_topic)
Check if language entry exists.
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.
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
global $DIC
Definition: goto.php:24
$ilIliasIniFile
Definition: imgupload.php:16
$ilUser
Definition: imgupload.php:18
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41
global $ilSetting
Definition: privfeed.php:17
$query
foreach($_POST as $key=> $value) $res
global $ilDB
$lang
Definition: xapiexit.php:8