ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
All Data Structures Namespaces Files Functions Variables Modules Pages
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 
79  var $lang_key;
80 
88 
95  var $separator = "#:#";
96 
103  var $comment_separator = "###";
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 
306  $lang_key = $this->lang_key;
307 
308  if (empty($this->lang_key))
309  {
310  $lang_key = $this->lang_user;
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 
419  function getUserLanguage()
420  {
421  return $this->lang_user;
422  }
423 
424 
425 } // END class.Language
426 ?>
static _getObjectsByType($a_obj_type="", $a_owner="")
Get objects by type.
logging
Definition: class.ilLog.php:18
static _lookupEntry($a_lang_key, $a_mod, $a_id)
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, the topic itself with "-" will be returned
const DB_FETCHMODE_OBJECT
Definition: class.ilDB.php:11
static $used_modules
exists($a_topic)
Check if language entry exists.
const ILIAS_ABSOLUTE_PATH
global $ilIliasIniFile
redirection script todo: (a better solution should control the processing via a xml file) ...
const DB_FETCHMODE_ASSOC
Definition: class.ilDB.php:10
ilLanguage($a_lang_key)
Constructor read the single-language file and put this in an array text.
global $ilUser
Definition: imgupload.php:15
global $ilSetting
Definition: privfeed.php:40
loadLanguageModule($a_module)
global $ilDB
static lookupId($a_lang_key)
Lookup obj_id of language ilDB $ilDB.
language handling
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...
$r