ILIAS  release_4-4 Revision
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 
123  function ilLanguage($a_lang_key)
124  {
126 
127  // store used modules and topics in a global variable
128  // ($lng seems to be initialized more than once)
129  global $ilias_lang_used_topics;
130  global $ilias_lang_used_modules;
131  $this->used_topics =& $ilias_lang_used_topics;
132  $this->used_modules =& $ilias_lang_used_modules;
133 
134  $this->ilias =& $ilias;
135 
136  if (!isset($log))
137  {
138  if (is_object($ilias))
139  {
140  require_once "./Services/Logging/classes/class.ilLog.php";
141  $this->log = new ilLog(ILIAS_LOG_DIR,ILIAS_LOG_FILE,$ilias->getClientId(),ILIAS_LOG_ENABLED);
142  }
143  }
144  else
145  {
146  $this->log =& $log;
147  }
148 
149  $this->lang_key = $a_lang_key;
150 
151  $this->text = array();
152  $this->loaded_modules = array();
153  //$this->lang_path = ILIAS_ABSOLUTE_PATH.substr($this->ilias->ini->readVariable("language","path"),1);
154 
155  // if no directory was found fall back to default lang dir
156  //if (!is_dir($this->lang_path))
157  //{
158  $this->lang_path = ILIAS_ABSOLUTE_PATH."/lang";
159  //}
160  $this->cust_lang_path = ILIAS_ABSOLUTE_PATH."/Customizing/global/lang";
161 
162  $this->lang_default = $ilIliasIniFile->readVariable("language","default");
163  if (is_object($ilSetting) && $ilSetting->get("language") != "")
164  {
165  $this->lang_default = $ilSetting->get("language");
166  }
167  $this->lang_user = $ilUser->prefs["language"];
168 
169  $langs = $this->getInstalledLanguages();
170 
171  if (!in_array($this->lang_key,$langs))
172  {
173  $this->lang_key = $this->lang_default;
174  }
175 
176  $this->loadLanguageModule("common");
177 
178  return true;
179  }
180 
181  function getLangKey()
182  {
183  return $this->lang_key;
184  }
185 
187  {
188  return $this->lang_default ? $this->lang_default : 'en';
189  }
190 
200  function txtlng($a_module, $a_topic, $a_language)
201  {
202  if (strcmp($a_language, $this->lang_key) == 0)
203  {
204  return $this->txt($a_topic);
205  }
206  else
207  {
208  return ilLanguage::_lookupEntry($a_language, $a_module, $a_topic);
209  }
210  }
211 
220  function txt($a_topic, $a_default_lang_fallback_mod = "")
221  {
222  if (empty($a_topic))
223  {
224  return "";
225  }
226 
227  // remember the used topics
228  $this->used_topics[$a_topic] = $a_topic;
229 
230  $translation = "";
231  if (isset($this->text[$a_topic]))
232  {
233  $translation = $this->text[$a_topic];
234  }
235 
236  if ($translation == "" && $a_default_lang_fallback_mod != "")
237  {
238  // #13467 - try current language first (could be missing module)
239  if($this->lang_key != $this->lang_default)
240  {
241  $translation = ilLanguage::_lookupEntry($this->lang_key,
242  $a_default_lang_fallback_mod, $a_topic);
243  }
244  // try default language last
245  if($translation == "" || $translation == "-".$a_topic."-")
246  {
247  $translation = ilLanguage::_lookupEntry($this->lang_default,
248  $a_default_lang_fallback_mod, $a_topic);
249  }
250  }
251 
252 
253  if ($translation == "")
254  {
255  if (ILIAS_LOG_ENABLED && is_object($this->log))
256  {
257  $this->log->writeLanguageLog($a_topic,$this->lang_key);
258  }
259  return "-".$a_topic."-";
260  }
261  else
262  {
263  return $translation;
264  }
265  }
266 
272  public function exists($a_topic)
273  {
274  return isset($this->text[$a_topic]);
275  }
276 
277  function loadLanguageModule ($a_module)
278  {
279  global $ilDB;
280 
281  if (in_array($a_module, $this->loaded_modules))
282  {
283  return;
284  }
285 
286  $this->loaded_modules[] = $a_module;
287 
288  // remember the used modules globally
289  $this->used_modules[$a_module] = $a_module;
290 
291  $lang_key = $this->lang_key;
292 
293  if (empty($this->lang_key))
294  {
295  $lang_key = $this->lang_user;
296  }
297 
298 /*
299  $query = "SELECT identifier,value FROM lng_data " .
300  "WHERE lang_key = '" . $lang_key."' " .
301  "AND module = '$a_module'";
302  $r = $this->ilias->db->query($query);
303 
304  while ($row = $r->fetchRow(DB_FETCHMODE_OBJECT))
305  {
306  $this->text[$row->identifier] = $row->value;
307  }
308 */
309 
310  $q = "SELECT * FROM lng_modules " .
311  "WHERE lang_key = ".$ilDB->quote($lang_key, "text")." AND module = ".
312  $ilDB->quote($a_module, "text");
313  $r = $ilDB->query($q);
314  $row = $r->fetchRow(DB_FETCHMODE_ASSOC);
315 
316  $new_text = unserialize($row["lang_array"]);
317  if (is_array($new_text))
318  {
319  $this->text = array_merge($this->text, $new_text);
320  }
321  }
322 
323 
325  {
326  include_once("./Services/Object/classes/class.ilObject.php");
327  $langlist = ilObject::_getObjectsByType("lng");
328 
329  foreach ($langlist as $lang)
330  {
331  if (substr($lang["desc"], 0, 9) == "installed")
332  {
333  $languages[] = $lang["title"];
334  }
335 
336  }
337 
338  return $languages ? $languages : array();
339  }
340 
341  function _lookupEntry($a_lang_key, $a_mod, $a_id)
342  {
343  global $ilDB;
344 
345  $set = $ilDB->query($q = sprintf("SELECT * FROM lng_data WHERE module = %s ".
346  "AND lang_key = %s AND identifier = %s",
347  $ilDB->quote((string) $a_mod, "text"), $ilDB->quote((string) $a_lang_key, "text"),
348  $ilDB->quote((string) $a_id, "text")));
349  $rec = $ilDB->fetchAssoc($set);
350 
351  if ($rec["value"] != "")
352  {
353  // remember the used topics
354  $this->used_topics[$a_id] = $a_id;
355  $this->used_modules[$a_mod] = $a_mod;
356 
357  return $rec["value"];
358  }
359 
360  return "-".$a_id."-";
361  }
362 
369  public static function lookupId($a_lang_key)
370  {
371  global $ilDB;
372 
373  $query = 'SELECT obj_id FROM object_data '.' '.
374  'WHERE title = '.$ilDB->quote($a_lang_key, 'text').' '.
375  'AND type = '.$ilDB->quote('lng','text');
376 
377  $res = $ilDB->query($query);
378  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
379  {
380  return $row->obj_id;
381  }
382  return 0;
383  }
384 
385 
386  function getUsedTopics()
387  {
388  if (is_array($this->used_topics))
389  {
390  asort($this->used_topics);
391  return $this->used_topics;
392  }
393  else
394  {
395  return array();
396  }
397  }
398 
399  function getUsedModules()
400  {
401  if (is_array($this->used_modules))
402  {
403  asort($this->used_modules);
404  return $this->used_modules;
405  }
406  else
407  {
408  return array();
409  }
410  }
411 
412  function getUserLanguage()
413  {
414  return $this->lang_user;
415  }
416 
417 
418 } // END class.Language
419 ?>
_lookupEntry($a_lang_key, $a_mod, $a_id)
static _getObjectsByType($a_obj_type="", $a_owner="")
Get objects by type.
logging
Definition: class.ilLog.php:18
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
exists($a_topic)
Check if language entry exists.
const ILIAS_ABSOLUTE_PATH
redirection script todo: (a better solution should control the processing via a xml file) ...
const DB_FETCHMODE_ASSOC
Definition: class.ilDB.php:10
global $ilIliasIniFile
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)
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