ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
class.ilPluginLanguage.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
25{
27
29 {
30 $this->plugin_info = $plugin_info;
31 }
32
33 protected function getLanguageDirectory(): string
34 {
35 return $this->plugin_info->getPath() . "/lang";
36 }
37
43 public function getAvailableLangFiles(): array
44 {
45 $directory = $this->getLanguageDirectory();
46 if (!@is_dir($directory)) {
47 return [];
48 }
49
50 $langs = [];
51
52 $dir = opendir($directory);
53 while ($file = readdir($dir)) {
54 if ($file === "." || $file === "..") {
55 continue;
56 }
57
58 // directories
59 if (@is_file($directory . "/" . $file) &&
60 strpos($file, "ilias_") === 0 &&
61 substr($file, strlen($file) - 5) === ".lang") {
62 $langs[] = [
63 "key" => substr($file, 6, 2),
64 "file" => $file
65 ];
66 }
67 }
68
69 return $langs;
70 }
71
72 public function hasAvailableLangFiles(): bool
73 {
74 return count($this->getAvailableLangFiles()) > 0;
75 }
76
77 public function getPrefix(): string
78 {
80 $component = $plugin->getComponent();
81 $slot = $plugin->getPluginSlot();
82
83 return $component->getId() . "_" . $slot->getId() . "_" . $plugin->getId();
84 }
85
91 public function updateLanguages(?array $a_lang_keys = null): void
92 {
93 // get the keys of all installed languages if keys are not provided
94 if (!isset($a_lang_keys)) {
96 }
97
98 $langs = $this->getAvailableLangFiles();
99
100 $prefix = $this->getPrefix();
101
102 foreach ($langs as $lang) {
103 // check if the language should be updated, otherwise skip it
104 if (!in_array($lang['key'], $a_lang_keys, true)) {
105 continue;
106 }
107
108 $txt = file($this->getLanguageDirectory() . "/" . $lang["file"]);
109 $lang_array = [];
110
111 // get locally changed variables of the module (these should be kept)
112 $local_changes = ilObjLanguage::_getLocalChangesByModule($lang['key'], $prefix);
113
114 // get language data
115 if (is_array($txt)) {
116 foreach ($txt as $row) {
117 if ($row[0] !== "#" && strpos($row, "#:#") > 0) {
118 $a = explode("#:#", trim($row));
119 $identifier = $prefix . "_" . trim($a[0]);
120 $value = trim($a[1]);
121
122 if (isset($local_changes[$identifier])) {
123 $lang_array[$identifier] = $local_changes[$identifier];
124 } else {
125 $lang_array[$identifier] = $value;
126 ilObjLanguage::replaceLangEntry($prefix, $identifier, $lang["key"], $value);
127 }
128 //echo "<br>-$prefix-".$prefix."_".trim($a[0])."-".$lang["key"]."-";
129 }
130 }
131 }
132
133 ilObjLanguage::replaceLangModule($lang["key"], $prefix, $lang_array);
134 }
135 }
136
137 public function uninstall(): void
138 {
139 global $DIC;
140 $ilDB = $DIC->database();
141
142 // remove all language entries (see ilObjLanguage)
143 // see updateLanguages
144 $prefix = $this->getPrefix();
145 if ($prefix) {
146 $ilDB->manipulate(
147 "DELETE FROM lng_data" .
148 " WHERE module = " . $ilDB->quote($prefix, "text")
149 );
150 $ilDB->manipulate(
151 "DELETE FROM lng_modules" .
152 " WHERE module = " . $ilDB->quote($prefix, "text")
153 );
154 }
155 }
156
160 public function loadLanguageModule(): void
161 {
162 global $DIC;
163 $lng = $DIC->language();
164
165 if (is_object($lng)) {
166 $lng->loadLanguageModule($this->getPrefix());
167 }
168 }
169
173 public function txt(string $a_var): string
174 {
175 global $DIC;
176 $lng = $DIC->language();
177 $this->loadLanguageModule();
178
179 return $lng->txt($this->getPrefix() . "_" . $a_var, $this->getPrefix());
180 }
181}
static getLangKeysOfInstalledLanguages()
Return the language keys of the installed languages.
static replaceLangEntry(string $a_module, string $a_identifier, string $a_lang_key, string $a_value, ?string $a_local_change=null, ?string $a_remarks=null)
Replace lang entry.
static replaceLangModule(string $a_key, string $a_module, array $a_array)
Replace language module array.
static _getLocalChangesByModule(string $a_key, string $a_module)
Get the local changes of a language module $a_key Language key $a_module Module key Return array iden...
Simple value class for information about a plugin.
__construct(ilPluginInfo $plugin_info)
txt(string $a_var)
Get Language Variable (prefix will be prepended automatically)
getAvailableLangFiles()
Get array of all language files in the plugin.
loadLanguageModule()
Load language module for plugin.
updateLanguages(?array $a_lang_keys=null)
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
global $lng
Definition: privfeed.php:31
global $DIC
Definition: shib_login.php:26