20 declare(strict_types=1);
77 $client_ini = $DIC->clientIni();
79 $this->log = $DIC->logger()->root();
81 $this->lang_key = $a_lang_key;
83 $this->usage_log_enabled = self::isUsageLogEnabled();
85 $this->lang_path = ILIAS_ABSOLUTE_PATH .
"/lang";
86 $this->cust_lang_path = ILIAS_ABSOLUTE_PATH .
"/Customizing/global/lang";
88 $this->lang_default = $client_ini->readVariable(
"language",
"default") ??
'en';
91 if ($DIC->offsetExists(
"ilSetting")) {
94 $this->lang_default =
$ilSetting->get(
"language");
97 if ($DIC->offsetExists(
"ilUser")) {
99 $this->lang_user =
$ilUser->prefs[
"language"];
104 if (!in_array($this->lang_key, $langs,
true)) {
108 require_once(
"./Services/Language/classes/class.ilCachedLanguage.php");
110 if ($this->global_cache->isActive()) {
111 $this->cached_modules = $this->global_cache->getTranslations();
129 return $this->lang_default ??
"en";
137 $rtl = array(
"ar",
"fa",
"ur",
"he");
159 public function txtlng(
string $a_module,
string $a_topic,
string $a_language): string
161 if (strcmp($a_language, $this->lang_key) === 0) {
162 return $this->
txt($a_topic);
164 return self::_lookupEntry($a_language, $a_module, $a_topic);
172 public function txt(
string $a_topic,
string $a_default_lang_fallback_mod =
""): string
174 if (empty($a_topic)) {
179 self::$used_topics[$a_topic] = $a_topic;
181 $translation = $this->text[$a_topic] ??
"";
183 if ($translation ===
"" && $a_default_lang_fallback_mod !==
"") {
185 if ($this->lang_key != $this->lang_default) {
186 $translation = self::_lookupEntry(
188 $a_default_lang_fallback_mod,
193 if ($translation ===
"" || $translation ===
"-" . $a_topic .
"-") {
194 $translation = self::_lookupEntry(
196 $a_default_lang_fallback_mod,
203 if ($translation ===
"") {
204 if (ILIAS_LOG_ENABLED && is_object($this->log)) {
205 $this->log->debug(
"Language (" . $this->lang_key .
"): topic -" . $a_topic .
"- not present");
207 return "-" . $a_topic .
"-";
210 if ($this->usage_log_enabled) {
211 self::logUsage($this->map_modules_txt[$a_topic] ??
"", $a_topic);
220 public function exists(
string $a_topic): bool
222 return isset($this->text[$a_topic]);
231 $ilDB = $DIC->database();
233 if (in_array($a_module, $this->loaded_modules,
true)) {
237 $this->loaded_modules[] = $a_module;
240 self::$used_modules[$a_module] = $a_module;
244 if (empty($this->lang_key)) {
248 if (isset($this->cached_modules[$a_module]) && is_array($this->cached_modules[$a_module])) {
249 $this->text = array_merge($this->text, $this->cached_modules[$a_module]);
251 if ($this->usage_log_enabled) {
252 foreach (array_keys($this->cached_modules[$a_module]) as
$key) {
253 $this->map_modules_txt[
$key] = $a_module;
260 $q =
"SELECT * FROM lng_modules " .
261 "WHERE lang_key = " .
$ilDB->quote($lang_key,
"text") .
" AND module = " .
262 $ilDB->quote($a_module,
"text");
263 $r =
$ilDB->query($q);
266 if ($row ===
false) {
270 $new_text = unserialize($row[
"lang_array"], [
"allowed_classes" =>
false]);
271 if (is_array($new_text)) {
272 $this->text = array_merge($this->text, $new_text);
274 if ($this->usage_log_enabled) {
275 foreach (array_keys($new_text) as
$key) {
276 $this->map_modules_txt[
$key] = $a_module;
287 return self::_getInstalledLanguages();
295 include_once
"./Services/Object/classes/class.ilObject.php";
299 foreach ($langlist as
$lang) {
300 if (strpos($lang[
"desc"],
"installed") === 0) {
301 $languages[] = $lang[
"title"];
305 return $languages ?: [];
308 public static function _lookupEntry(
string $a_lang_key,
string $a_mod,
string $a_id): string
311 $ilDB = $DIC->database();
313 $set =
$ilDB->query($q = sprintf(
314 "SELECT * FROM lng_data WHERE module = %s " .
315 "AND lang_key = %s AND identifier = %s",
316 $ilDB->quote($a_mod,
"text"),
317 $ilDB->quote($a_lang_key,
"text"),
318 $ilDB->quote($a_id,
"text")
320 $rec =
$ilDB->fetchAssoc($set);
322 if (isset($rec[
"value"]) && $rec[
"value"] !=
"") {
324 self::$used_topics[$a_id] = $a_id;
325 self::$used_modules[$a_mod] = $a_mod;
327 if (self::isUsageLogEnabled()) {
328 self::logUsage($a_mod, $a_id);
331 return $rec[
"value"];
334 return "-" . $a_id .
"-";
343 $ilDB = $DIC->database();
345 $query =
"SELECT obj_id FROM object_data " .
" " .
346 "WHERE title = " .
$ilDB->quote($a_lang_key,
"text") .
" " .
347 "AND type = " .
$ilDB->quote(
"lng",
"text");
351 return (
int) $row->obj_id;
361 asort(self::$used_topics);
362 return self::$used_topics;
370 asort(self::$used_modules);
371 return self::$used_modules;
392 return new self(
"en");
405 if ($DIC->offsetExists(
"ilUser")) {
409 $isset_get_lang = $DIC->http()->wrapper()->query()->has(
"lang");
413 $language = $language_detection->detect();
418 $post_change_lang_to = [];
419 if ($DIC->http()->wrapper()->post()->has(
'change_lang_to')) {
420 $post_change_lang_to = $DIC->http()->wrapper()->post()->retrieve(
422 $DIC->refinery()->kindlyTo()->dictOf(
423 $DIC->refinery()->kindlyTo()->string()
438 if ($isset_get_lang) {
439 $get_lang = $DIC->http()->wrapper()->query()->retrieve(
441 $DIC->refinery()->kindlyTo()->string()
447 $langs = self::_getInstalledLanguages();
471 if (!is_object($a_tpl)) {
475 if (!is_array($a_lang_key)) {
476 $a_lang_key = array($a_lang_key);
480 foreach ($a_lang_key as $lk) {
481 $map[$lk] = $this->
txt($lk);
496 if (!is_object($a_tpl)) {
500 if (!is_array($a_map)) {
504 foreach ($a_map as $k => $v) {
506 $a_tpl->addOnloadCode(
"il.Language.setLangVar('" . $k .
"', " . json_encode($v, JSON_THROW_ON_ERROR) .
");");
514 protected static function logUsage(
string $a_module,
string $a_identifier): void
516 if ($a_module !==
"" && $a_identifier !==
"") {
517 self::$lng_log[$a_identifier] = $a_module;
530 $ilClientIniFile = $DIC->clientIni();
531 $ilDB = $DIC->database();
533 if (!$ilClientIniFile instanceof
ilIniFile) {
537 if (defined(
"DEVMODE") && DEVMODE) {
541 if (!$ilClientIniFile->variableExists(
"system",
"LANGUAGE_LOG")) {
542 return (
int) $ilClientIniFile->readVariable(
"system",
"LANGUAGE_LOG") === 1;
555 if (!$this->usage_log_enabled || !$DIC->isDependencyAvailable(
"database")) {
559 $ilDB = $DIC->database();
561 foreach (self::$lng_log as $identifier => $module) {
562 $wave[] =
"(" .
$ilDB->quote($module,
"text") .
', ' .
$ilDB->quote($identifier,
"text") .
")";
563 unset(self::$lng_log[$identifier]);
565 if (count($wave) === 150 || (count(self::$lng_log) === 0 && count($wave) > 0)) {
566 $query =
"REPLACE INTO lng_log (module, identifier) VALUES " . implode(
", ", $wave);
__construct(string $a_lang_key)
Constructor read the single-language file and put this in an array text.
static get(string $a_var)
exists(string $a_topic)
Check if language entry exists.
getContentLanguage()
Return content language.
toJSMap(array $a_map, ilGlobalTemplateInterface $a_tpl=null)
Transfer text to Javascript.
txtlng(string $a_module, string $a_topic, string $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
txt(string $a_topic, string $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...
getUserLanguage()
Return language of user.
getDefaultLanguage()
Return default language.
Class ChatMainBarProvider .
getLangKey()
Return lang key.
static array $used_topics
loadLanguageModule(string $a_module)
Load language module.
static _getObjectsByType(string $obj_type="", int $owner=null)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getUsedModules()
Return used modules.
static getGlobalInstance()
Builds the global language object.
ilCachedLanguage $global_cache
static lookupId(string $a_lang_key)
Lookup obj_id of language.
getInstalledLanguages()
Get installed languages.
static getFallbackInstance()
Builds a global default language instance.
static array $used_modules
getUsedTopics()
Return used topics.
__destruct()
destructor saves all language usages to db if log is enabled and ilDB exists
static _lookupEntry(string $a_lang_key, string $a_mod, string $a_id)
toJS($a_lang_key, ilGlobalTemplateInterface $a_tpl=null)
Transfer text to Javascript.
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static logUsage(string $a_module, string $a_identifier)
saves tupel of language module and identifier
static _getInstalledLanguages()
Get installed languages.
static set(string $a_var, $a_val)
Set a value.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
string $comment_separator
getTextDirection()
Return text direction.
static isUsageLogEnabled()
checks if language usage log is enabled you need MySQL to use this function this function is automati...