19declare(strict_types=1);
76 $client_ini =
$DIC->clientIni();
78 $this->log =
$DIC->logger()->root();
80 $this->lang_key = $a_lang_key;
84 $this->lang_path = ILIAS_ABSOLUTE_PATH .
"/lang";
85 $this->cust_lang_path = ILIAS_ABSOLUTE_PATH .
"/lang/customizing";
87 $this->lang_default = $client_ini->readVariable(
"language",
"default") ??
'en';
90 if (
$DIC->offsetExists(
"ilSetting")) {
93 $this->lang_default =
$ilSetting->get(
"language");
96 if (
$DIC->offsetExists(
"ilUser")) {
97 $ilUser =
$DIC->user();
98 $this->lang_user = $ilUser->getLanguage();
103 if (!in_array($this->lang_key, $langs,
true)) {
108 if ($this->global_cache->isActive()) {
109 $this->cached_modules = $this->global_cache->getTranslations();
127 return $this->lang_default ??
"en";
135 $rtl = array(
"ar",
"fa",
"ur",
"he");
157 public function txtlng(
string $a_module,
string $a_topic,
string $a_language): string
159 if (strcmp($a_language, $this->lang_key) === 0) {
160 return $this->
txt($a_topic);
170 public function txt(
string $a_topic,
string $a_default_lang_fallback_mod =
""): string
172 if (empty($a_topic)) {
177 self::$used_topics[$a_topic] = $a_topic;
179 $translation = $this->text[$a_topic] ??
"";
181 if ($translation ===
"" && $a_default_lang_fallback_mod !==
"") {
183 if ($this->lang_key != $this->lang_default) {
186 $a_default_lang_fallback_mod,
191 if ($translation ===
"" || $translation ===
"-" . $a_topic .
"-") {
194 $a_default_lang_fallback_mod,
201 if ($translation ===
"") {
202 if (ILIAS_LOG_ENABLED && is_object($this->log)) {
203 $this->log->debug(
"Language (" . $this->lang_key .
"): topic -" . $a_topic .
"- not present");
205 return "-" . $a_topic .
"-";
208 if ($this->usage_log_enabled) {
218 public function exists(
string $a_topic): bool
220 return isset($this->text[$a_topic]);
231 if (in_array($a_module, $this->loaded_modules,
true)) {
235 $this->loaded_modules[] = $a_module;
238 self::$used_modules[$a_module] = $a_module;
242 if (empty($this->lang_key)) {
246 if (isset($this->cached_modules[$a_module]) && is_array($this->cached_modules[$a_module])) {
247 $this->text = array_merge($this->text, $this->cached_modules[$a_module]);
249 if ($this->usage_log_enabled) {
250 foreach (array_keys($this->cached_modules[$a_module]) as $key) {
251 $this->map_modules_txt[$key] = $a_module;
258 $q =
"SELECT * FROM lng_modules " .
259 "WHERE lang_key = " .
$ilDB->quote(
$lang_key,
"text") .
" AND module = " .
260 $ilDB->quote($a_module,
"text");
264 if ($row ===
false) {
268 $new_text = unserialize($row[
"lang_array"], [
"allowed_classes" =>
false]);
269 if (is_array($new_text)) {
270 $this->text = array_merge($this->text, $new_text);
272 if ($this->usage_log_enabled) {
273 foreach (array_keys($new_text) as $key) {
274 $this->map_modules_txt[$key] = $a_module;
296 foreach ($langlist as
$lang) {
297 if (strpos(
$lang[
"desc"],
"installed") === 0) {
298 $languages[] =
$lang[
"title"];
302 return $languages ?: [];
305 public static function _lookupEntry(
string $a_lang_key,
string $a_mod,
string $a_id): string
310 $set =
$ilDB->query(
$q = sprintf(
311 "SELECT * FROM lng_data WHERE module = %s " .
312 "AND lang_key = %s AND identifier = %s",
313 $ilDB->quote($a_mod,
"text"),
314 $ilDB->quote($a_lang_key,
"text"),
315 $ilDB->quote($a_id,
"text")
317 $rec =
$ilDB->fetchAssoc($set);
319 if (isset($rec[
"value"]) && $rec[
"value"] !=
"") {
321 self::$used_topics[$a_id] = $a_id;
322 self::$used_modules[$a_mod] = $a_mod;
324 if (self::isUsageLogEnabled()) {
328 return $rec[
"value"];
331 return "-" . $a_id .
"-";
342 $query =
"SELECT obj_id FROM object_data " .
" " .
343 "WHERE title = " .
$ilDB->quote($a_lang_key,
"text") .
" " .
344 "AND type = " .
$ilDB->quote(
"lng",
"text");
348 return (
int) $row->obj_id;
358 asort(self::$used_topics);
367 asort(self::$used_modules);
389 return new self(
"en");
402 if (
$DIC->offsetExists(
"ilUser")) {
403 $ilUser =
$DIC->user();
406 $isset_get_lang =
$DIC->http()->wrapper()->query()->has(
"lang");
408 (!$ilUser->getId() || $ilUser->isAnonymous())) {
410 $language = $language_detection->detect();
415 $post_change_lang_to = [];
416 if (
$DIC->http()->wrapper()->post()->has(
'change_lang_to')) {
417 $post_change_lang_to =
$DIC->http()->wrapper()->post()->retrieve(
419 $DIC->refinery()->kindlyTo()->dictOf(
420 $DIC->refinery()->kindlyTo()->string()
429 (($ilUser->getId() && !$ilUser->isAnonymous()))
435 if ($isset_get_lang) {
436 $get_lang =
$DIC->http()->wrapper()->query()->retrieve(
438 $DIC->refinery()->kindlyTo()->string()
468 if (!is_object($a_tpl)) {
472 if (!is_array($a_lang_key)) {
473 $a_lang_key = array($a_lang_key);
477 foreach ($a_lang_key as $lk) {
478 $map[$lk] = $this->
txt($lk);
493 if (!is_object($a_tpl)) {
497 if (!is_array($a_map)) {
501 foreach ($a_map as $k => $v) {
503 $a_tpl->addOnloadCode(
"il.Language.setLangVar('" . $k .
"', " . json_encode($v, JSON_THROW_ON_ERROR) .
");");
511 protected static function logUsage(
string $a_module,
string $a_identifier): void
513 if ($a_module !==
"" && $a_identifier !==
"") {
514 self::$lng_log[$a_identifier] = $a_module;
527 $ilClientIniFile =
$DIC->clientIni();
530 if (!$ilClientIniFile instanceof
ilIniFile) {
534 if (defined(
"DEVMODE") && DEVMODE) {
538 if (!$ilClientIniFile->variableExists(
"system",
"LANGUAGE_LOG")) {
539 return (
int) $ilClientIniFile->readVariable(
"system",
"LANGUAGE_LOG") === 1;
552 if (!$this->usage_log_enabled || !
$DIC->isDependencyAvailable(
"database")) {
558 foreach (self::$lng_log as $identifier => $module) {
559 $wave[] =
"(" .
$ilDB->quote($module,
"text") .
', ' .
$ilDB->quote($identifier,
"text") .
")";
560 unset(self::$lng_log[$identifier]);
562 if (count($wave) === 150 || (count(self::$lng_log) === 0 && count($wave) > 0)) {
563 $query =
"REPLACE INTO lng_log (module, identifier) VALUES " . implode(
", ", $wave);
564 $ilDB->manipulate($query);
INIFile Parser Early access in init proceess! Avoid further dependencies like logging or other servic...
Class ilLanguageDetection.
toJSMap(array $a_map, ?ilGlobalTemplateInterface $a_tpl=null)
Transfer text to Javascript.
loadLanguageModule(string $a_module)
Load language module.
getLangKey()
Return lang key.
getContentLanguage()
Return content language.
static getFallbackInstance()
Builds a global default language instance.
toJS($a_lang_key, ?ilGlobalTemplateInterface $a_tpl=null)
Transfer text to Javascript.
static lookupId(string $a_lang_key)
Lookup obj_id of language.
getUsedTopics()
Return used topics.
static array $used_modules
exists(string $a_topic)
Check if language entry exists.
static _getInstalledLanguages()
Get installed languages.
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...
static isUsageLogEnabled()
checks if language usage log is enabled you need MySQL to use this function this function is automati...
static array $used_topics
getUsedModules()
Return used modules.
__destruct()
destructor saves all language usages to db if log is enabled and ilDB exists
getDefaultLanguage()
Return default language.
static logUsage(string $a_module, string $a_identifier)
saves tupel of language module and identifier
getInstalledLanguages()
Get installed languages.
string $comment_separator
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,...
getTextDirection()
Return text direction.
static getGlobalInstance()
Builds the global language object.
getUserLanguage()
Return language of user.
static _lookupEntry(string $a_lang_key, string $a_mod, string $a_id)
ilCachedLanguage $global_cache
__construct(string $a_lang_key)
Constructor read the single-language file and put this in an array text.
Component logger with individual log levels by component id.
static _getObjectsByType(string $obj_type="", ?int $owner=null)
static get(string $a_var)
static set(string $a_var, $a_val)
Set a value.
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.