ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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
139 function ilLanguage($a_lang_key)
140 {
142
143 $this->ilias = $ilias;
144
145 if (!isset($log))
146 {
147 if (is_object($ilias))
148 {
149 require_once "./Services/Logging/classes/class.ilLog.php";
150 $this->log = new ilLog(ILIAS_LOG_DIR,ILIAS_LOG_FILE,$ilias->getClientId(),ILIAS_LOG_ENABLED);
151 }
152 }
153 else
154 {
155 $this->log =& $log;
156 }
157
158 $this->lang_key = $a_lang_key;
159
160 $this->text = array();
161 $this->loaded_modules = array();
162 //$this->lang_path = ILIAS_ABSOLUTE_PATH.substr($this->ilias->ini->readVariable("language","path"),1);
163
164 // if no directory was found fall back to default lang dir
165 //if (!is_dir($this->lang_path))
166 //{
167 $this->lang_path = ILIAS_ABSOLUTE_PATH."/lang";
168 //}
169 $this->cust_lang_path = ILIAS_ABSOLUTE_PATH."/Customizing/global/lang";
170
171 $this->lang_default = $ilIliasIniFile->readVariable("language","default");
172 if (is_object($ilSetting) && $ilSetting->get("language") != "")
173 {
174 $this->lang_default = $ilSetting->get("language");
175 }
176 $this->lang_user = $ilUser->prefs["language"];
177
178 $langs = $this->getInstalledLanguages();
179
180 if (!in_array($this->lang_key,$langs))
181 {
182 $this->lang_key = $this->lang_default;
183 }
184
185 require_once('./Services/Language/classes/class.ilCachedLanguage.php');
186 $this->global_cache = ilCachedLanguage::getInstance($this->lang_key);
187 if ($this->global_cache->isActive()) {
188 $this->cached_modules = $this->global_cache->getTranslations();
189 }
190
191 $this->loadLanguageModule("common");
192
193 return true;
194 }
195
196 function getLangKey()
197 {
198 return $this->lang_key;
199 }
200
202 {
203 return $this->lang_default ? $this->lang_default : 'en';
204 }
205
215 function txtlng($a_module, $a_topic, $a_language)
216 {
217 if (strcmp($a_language, $this->lang_key) == 0)
218 {
219 return $this->txt($a_topic);
220 }
221 else
222 {
223 return ilLanguage::_lookupEntry($a_language, $a_module, $a_topic);
224 }
225 }
226
235 function txt($a_topic, $a_default_lang_fallback_mod = "")
236 {
237 if (empty($a_topic))
238 {
239 return "";
240 }
241
242 // remember the used topics
243 self::$used_topics[$a_topic] = $a_topic;
244
245 $translation = "";
246 if (isset($this->text[$a_topic]))
247 {
248 $translation = $this->text[$a_topic];
249 }
250
251 if ($translation == "" && $a_default_lang_fallback_mod != "")
252 {
253 // #13467 - try current language first (could be missing module)
254 if($this->lang_key != $this->lang_default)
255 {
256 $translation = ilLanguage::_lookupEntry($this->lang_key,
257 $a_default_lang_fallback_mod, $a_topic);
258 }
259 // try default language last
260 if($translation == "" || $translation == "-".$a_topic."-")
261 {
262 $translation = ilLanguage::_lookupEntry($this->lang_default,
263 $a_default_lang_fallback_mod, $a_topic);
264 }
265 }
266
267
268 if ($translation == "")
269 {
270 if (ILIAS_LOG_ENABLED && is_object($this->log))
271 {
272 $this->log->writeLanguageLog($a_topic,$this->lang_key);
273 }
274 return "-".$a_topic."-";
275 }
276 else
277 {
278 return $translation;
279 }
280 }
281
287 public function exists($a_topic)
288 {
289 return isset($this->text[$a_topic]);
290 }
291
292 function loadLanguageModule ($a_module)
293 {
294 global $ilDB;
295
296 if (in_array($a_module, $this->loaded_modules))
297 {
298 return;
299 }
300
301 $this->loaded_modules[] = $a_module;
302
303 // remember the used modules globally
304 self::$used_modules[$a_module] = $a_module;
305
307
308 if (empty($this->lang_key))
309 {
311 }
312
313 if(is_array($this->cached_modules[$a_module])) {
314 $this->text = array_merge($this->text, $this->cached_modules[$a_module]);
315
316 return;
317 }
318
319/*
320 $query = "SELECT identifier,value FROM lng_data " .
321 "WHERE lang_key = '" . $lang_key."' " .
322 "AND module = '$a_module'";
323 $r = $this->ilias->db->query($query);
324
325 while ($row = $r->fetchRow(DB_FETCHMODE_OBJECT))
326 {
327 $this->text[$row->identifier] = $row->value;
328 }
329*/
330
331 $q = "SELECT * FROM lng_modules " .
332 "WHERE lang_key = ".$ilDB->quote($lang_key, "text")." AND module = ".
333 $ilDB->quote($a_module, "text");
334 $r = $ilDB->query($q);
335 $row = $r->fetchRow(DB_FETCHMODE_ASSOC);
336
337 $new_text = unserialize($row["lang_array"]);
338 if (is_array($new_text))
339 {
340 $this->text = array_merge($this->text, $new_text);
341 }
342 }
343
344
346 {
347 include_once("./Services/Object/classes/class.ilObject.php");
348 $langlist = ilObject::_getObjectsByType("lng");
349
350 foreach ($langlist as $lang)
351 {
352 if (substr($lang["desc"], 0, 9) == "installed")
353 {
354 $languages[] = $lang["title"];
355 }
356
357 }
358
359 return $languages ? $languages : array();
360 }
361
362 public static function _lookupEntry($a_lang_key, $a_mod, $a_id)
363 {
364 global $ilDB;
365
366 $set = $ilDB->query($q = sprintf("SELECT * FROM lng_data WHERE module = %s ".
367 "AND lang_key = %s AND identifier = %s",
368 $ilDB->quote((string) $a_mod, "text"), $ilDB->quote((string) $a_lang_key, "text"),
369 $ilDB->quote((string) $a_id, "text")));
370 $rec = $ilDB->fetchAssoc($set);
371
372 if ($rec["value"] != "")
373 {
374 // remember the used topics
375 self::$used_topics[$a_id] = $a_id;
376 self::$used_modules[$a_mod] = $a_mod;
377
378 return $rec["value"];
379 }
380
381 return "-".$a_id."-";
382 }
383
390 public static function lookupId($a_lang_key)
391 {
392 global $ilDB;
393
394 $query = 'SELECT obj_id FROM object_data '.' '.
395 'WHERE title = '.$ilDB->quote($a_lang_key, 'text').' '.
396 'AND type = '.$ilDB->quote('lng','text');
397
398 $res = $ilDB->query($query);
399 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
400 {
401 return $row->obj_id;
402 }
403 return 0;
404 }
405
406
407 function getUsedTopics()
408 {
409 asort(self::$used_topics);
410 return self::$used_topics;
411 }
412
413 function getUsedModules()
414 {
415 asort(self::$used_modules);
416 return self::$used_modules;
417 }
418
420 {
421 return $this->lang_user;
422 }
423
428 public static function getGlobalInstance()
429 {
434 global $ilUser, $ilSetting;
435
436 if(!ilSession::get('lang') && !$_GET['lang'])
437 {
438 if(
439 $ilUser instanceof ilObjUser &&
440 (!$ilUser->getId() || $ilUser->isAnonymous())
441 )
442 {
443 require_once 'Services/Language/classes/class.ilLanguageDetection.php';
444 $language_detection = new ilLanguageDetection();
445 $language = $language_detection->detect();
446
447 $ilUser->setPref('language', $language);
448 $_GET['lang'] = $language;
449 }
450 }
451
452 if(isset($_POST['change_lang_to']) && $_POST['change_lang_to'] != "")
453 {
454 $_GET['lang'] = ilUtil::stripSlashes($_POST['change_lang_to']);
455 }
456
457 // prefer personal setting when coming from login screen
458 // Added check for ilUser->getId > 0 because it is 0 when the language is changed and the terms of service should be displayed
459 if(
460 $ilUser instanceof ilObjUser &&
461 ($ilUser->getId() && !$ilUser->isAnonymous())
462 )
463 {
464 ilSession::set('lang', $ilUser->getPref('language'));
465 }
466
467 ilSession::set('lang', (isset($_GET['lang']) && $_GET['lang']) ? $_GET['lang'] : ilSession::get('lang'));
468
469 // check whether lang selection is valid
471 if(!in_array(ilSession::get('lang'), $langs))
472 {
473 if($ilSetting instanceof ilSetting && $ilSetting->get('language') != '')
474 {
475 ilSession::set('lang', $ilSetting->get('language'));
476 }
477 else
478 {
479 ilSession::set('lang', $langs[0]);
480 }
481 }
482 $_GET['lang'] = ilSession::get('lang');
483
484 return new self(ilSession::get('lang'));
485 }
486
487 /*
488 * Transfer text to Javascript
489 *
490 * @param string|array $a_lang_key languag key or array of language keys
491 * @param ilTemplate $a_tpl template
492 */
493 function toJS($a_lang_key, ilTemplate $a_tpl = null)
494 {
495 global $tpl;
496
497 if (!is_object($a_tpl))
498 {
499 $a_tpl = $tpl;
500 }
501
502 if (!is_array($a_lang_key))
503 {
504 $a_lang_key = array($a_lang_key);
505 }
506
507 $map = array();
508 foreach ($a_lang_key as $lk)
509 {
510 $map[$lk] = $this->txt($lk);
511 }
512 $this->toJSMap($map, $a_tpl);
513 }
514
521 function toJSMap($a_map, ilTemplate $a_tpl = null)
522 {
523 global $tpl;
524
525 if (!is_object($a_tpl))
526 {
527 $a_tpl = $tpl;
528 }
529
530 if (!is_array($a_map))
531 {
532 return;
533 }
534
535 foreach ($a_map as $k => $v)
536 {
537 if ($v != "")
538 {
539 include_once("./Services/JSON/classes/class.ilJsonUtil.php");
540 $a_tpl->addOnloadCode("il.Language.setLangVar('".$k."', ".ilJsonUtil::encode($v).");");
541 }
542 }
543 }
544
545
546} // END class.Language
547?>
global $tpl
Definition: ilias.php:8
$_GET["client_id"]
const DB_FETCHMODE_ASSOC
Definition: class.ilDB.php:10
const DB_FETCHMODE_OBJECT
Definition: class.ilDB.php:11
static encode($mixed, $suppress_native=false)
Class ilLanguageDetection.
language handling
loadLanguageModule($a_module)
ilLanguage($a_lang_key)
Constructor read the single-language file and put this in an array text.
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,...
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 _lookupEntry($a_lang_key, $a_mod, $a_id)
static lookupId($a_lang_key)
Lookup obj_id of language @global ilDB $ilDB.
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
$_POST['username']
Definition: cron.php:12
$r
Definition: example_031.php:79
redirection script todo: (a better solution should control the processing via a xml file)
global $ilSetting
Definition: privfeed.php:40
global $ilDB
global $ilIliasIniFile
global $ilUser
Definition: imgupload.php:15
const ILIAS_ABSOLUTE_PATH