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 .
"/lang/customizing";
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")) {
98 $ilUser = $DIC->user();
99 $this->lang_user = $ilUser->prefs[
"language"];
104 if (!in_array($this->lang_key, $langs,
true)) {
109 if ($this->global_cache->isActive()) {
110 $this->cached_modules = $this->global_cache->getTranslations();
128 return $this->lang_default ??
"en";
136 $rtl = array(
"ar",
"fa",
"ur",
"he");
158 public function txtlng(
string $a_module,
string $a_topic,
string $a_language): string
160 if (strcmp($a_language, $this->lang_key) === 0) {
161 return $this->
txt($a_topic);
163 return self::_lookupEntry($a_language, $a_module, $a_topic);
171 public function txt(
string $a_topic,
string $a_default_lang_fallback_mod =
""): string
173 if (empty($a_topic)) {
178 self::$used_topics[$a_topic] = $a_topic;
180 $translation = $this->text[$a_topic] ??
"";
182 if ($translation ===
"" && $a_default_lang_fallback_mod !==
"") {
184 if ($this->lang_key != $this->lang_default) {
185 $translation = self::_lookupEntry(
187 $a_default_lang_fallback_mod,
192 if ($translation ===
"" || $translation ===
"-" . $a_topic .
"-") {
193 $translation = self::_lookupEntry(
195 $a_default_lang_fallback_mod,
202 if ($translation ===
"") {
203 if (ILIAS_LOG_ENABLED && is_object($this->log)) {
204 $this->log->debug(
"Language (" . $this->lang_key .
"): topic -" . $a_topic .
"- not present");
206 return "-" . $a_topic .
"-";
209 if ($this->usage_log_enabled) {
210 self::logUsage($this->map_modules_txt[$a_topic] ??
"", $a_topic);
219 public function exists(
string $a_topic): bool
221 return isset($this->text[$a_topic]);
230 $ilDB = $DIC->database();
232 if (in_array($a_module, $this->loaded_modules,
true)) {
236 $this->loaded_modules[] = $a_module;
239 self::$used_modules[$a_module] = $a_module;
243 if (empty($this->lang_key)) {
247 if (isset($this->cached_modules[$a_module]) && is_array($this->cached_modules[$a_module])) {
248 $this->text = array_merge($this->text, $this->cached_modules[$a_module]);
250 if ($this->usage_log_enabled) {
251 foreach (array_keys($this->cached_modules[$a_module]) as $key) {
252 $this->map_modules_txt[$key] = $a_module;
259 $q =
"SELECT * FROM lng_modules " .
260 "WHERE lang_key = " .
$ilDB->quote($lang_key,
"text") .
" AND module = " .
261 $ilDB->quote($a_module,
"text");
265 if ($row ===
false) {
269 $new_text = unserialize($row[
"lang_array"], [
"allowed_classes" =>
false]);
270 if (is_array($new_text)) {
271 $this->text = array_merge($this->text, $new_text);
273 if ($this->usage_log_enabled) {
274 foreach (array_keys($new_text) as $key) {
275 $this->map_modules_txt[$key] = $a_module;
286 return self::_getInstalledLanguages();
297 foreach ($langlist as $lang) {
298 if (strpos($lang[
"desc"],
"installed") === 0) {
299 $languages[] = $lang[
"title"];
303 return $languages ?: [];
306 public static function _lookupEntry(
string $a_lang_key,
string $a_mod,
string $a_id): string
309 $ilDB = $DIC->database();
311 $set =
$ilDB->query(
$q = sprintf(
312 "SELECT * FROM lng_data WHERE module = %s " .
313 "AND lang_key = %s AND identifier = %s",
314 $ilDB->quote($a_mod,
"text"),
315 $ilDB->quote($a_lang_key,
"text"),
316 $ilDB->quote($a_id,
"text")
318 $rec =
$ilDB->fetchAssoc($set);
320 if (isset($rec[
"value"]) && $rec[
"value"] !=
"") {
322 self::$used_topics[$a_id] = $a_id;
323 self::$used_modules[$a_mod] = $a_mod;
325 if (self::isUsageLogEnabled()) {
326 self::logUsage($a_mod, $a_id);
329 return $rec[
"value"];
332 return "-" . $a_id .
"-";
341 $ilDB = $DIC->database();
343 $query =
"SELECT obj_id FROM object_data " .
" " .
344 "WHERE title = " .
$ilDB->quote($a_lang_key,
"text") .
" " .
345 "AND type = " .
$ilDB->quote(
"lng",
"text");
349 return (
int) $row->obj_id;
359 asort(self::$used_topics);
360 return self::$used_topics;
368 asort(self::$used_modules);
369 return self::$used_modules;
390 return new self(
"en");
403 if ($DIC->offsetExists(
"ilUser")) {
404 $ilUser = $DIC->user();
407 $isset_get_lang = $DIC->http()->wrapper()->query()->has(
"lang");
409 (!$ilUser->getId() || $ilUser->isAnonymous())) {
411 $language = $language_detection->detect();
416 $post_change_lang_to = [];
417 if ($DIC->http()->wrapper()->post()->has(
'change_lang_to')) {
418 $post_change_lang_to = $DIC->http()->wrapper()->post()->retrieve(
420 $DIC->refinery()->kindlyTo()->dictOf(
421 $DIC->refinery()->kindlyTo()->string()
430 (($ilUser->getId() && !$ilUser->isAnonymous()))
436 if ($isset_get_lang) {
437 $get_lang = $DIC->http()->wrapper()->query()->retrieve(
439 $DIC->refinery()->kindlyTo()->string()
445 $langs = self::_getInstalledLanguages();
469 if (!is_object($a_tpl)) {
473 if (!is_array($a_lang_key)) {
474 $a_lang_key = array($a_lang_key);
478 foreach ($a_lang_key as $lk) {
479 $map[$lk] = $this->
txt($lk);
494 if (!is_object($a_tpl)) {
498 if (!is_array($a_map)) {
502 foreach ($a_map as $k => $v) {
504 $a_tpl->addOnloadCode(
"il.Language.setLangVar('" . $k .
"', " . json_encode($v, JSON_THROW_ON_ERROR) .
");");
512 protected static function logUsage(
string $a_module,
string $a_identifier): void
514 if ($a_module !==
"" && $a_identifier !==
"") {
515 self::$lng_log[$a_identifier] = $a_module;
528 $ilClientIniFile = $DIC->clientIni();
529 $ilDB = $DIC->database();
531 if (!$ilClientIniFile instanceof
ilIniFile) {
535 if (defined(
"DEVMODE") && DEVMODE) {
539 if (!$ilClientIniFile->variableExists(
"system",
"LANGUAGE_LOG")) {
540 return (
int) $ilClientIniFile->readVariable(
"system",
"LANGUAGE_LOG") === 1;
553 if (!$this->usage_log_enabled || !$DIC->isDependencyAvailable(
"database")) {
557 $ilDB = $DIC->database();
559 foreach (self::$lng_log as $identifier => $module) {
560 $wave[] =
"(" .
$ilDB->quote($module,
"text") .
', ' .
$ilDB->quote($identifier,
"text") .
")";
561 unset(self::$lng_log[$identifier]);
563 if (count($wave) === 150 || (count(self::$lng_log) === 0 && count($wave) > 0)) {
564 $query =
"REPLACE INTO lng_log (module, identifier) VALUES " . implode(
", ", $wave);
565 $ilDB->manipulate($query);
__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.
Interface Observer Contains several chained tasks and infos about them.
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.
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.
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...