ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilPluginLanguage.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
25 {
27 
28  public function __construct(ilPluginInfo $plugin_info)
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)) {
95  $a_lang_keys = [];
96  foreach (ilObjLanguage::getInstalledLanguages() as $langObj) {
97  if ($langObj->isInstalled()) {
98  $a_lang_keys[] = $langObj->getKey();
99  }
100  }
101  }
102 
103  $langs = $this->getAvailableLangFiles();
104 
105  $prefix = $this->getPrefix();
106 
107  foreach ($langs as $lang) {
108  // check if the language should be updated, otherwise skip it
109  if (!in_array($lang['key'], $a_lang_keys, true)) {
110  continue;
111  }
112 
113  $txt = file($this->getLanguageDirectory() . "/" . $lang["file"]);
114  $lang_array = [];
115 
116  // get locally changed variables of the module (these should be kept)
117  $local_changes = ilObjLanguage::_getLocalChangesByModule($lang['key'], $prefix);
118 
119  // get language data
120  if (is_array($txt)) {
121  foreach ($txt as $row) {
122  if ($row[0] !== "#" && strpos($row, "#:#") > 0) {
123  $a = explode("#:#", trim($row));
124  $identifier = $prefix . "_" . trim($a[0]);
125  $value = trim($a[1]);
126 
127  if (isset($local_changes[$identifier])) {
128  $lang_array[$identifier] = $local_changes[$identifier];
129  } else {
130  $lang_array[$identifier] = $value;
131  ilObjLanguage::replaceLangEntry($prefix, $identifier, $lang["key"], $value);
132  }
133  //echo "<br>-$prefix-".$prefix."_".trim($a[0])."-".$lang["key"]."-";
134  }
135  }
136  }
137 
138  ilObjLanguage::replaceLangModule($lang["key"], $prefix, $lang_array);
139  }
140  }
141 
142  public function uninstall(): void
143  {
144  global $DIC;
145  $ilDB = $DIC->database();
146 
147  // remove all language entries (see ilObjLanguage)
148  // see updateLanguages
149  $prefix = $this->getPrefix();
150  if ($prefix) {
151  $ilDB->manipulate(
152  "DELETE FROM lng_data" .
153  " WHERE module = " . $ilDB->quote($prefix, "text")
154  );
155  $ilDB->manipulate(
156  "DELETE FROM lng_modules" .
157  " WHERE module = " . $ilDB->quote($prefix, "text")
158  );
159  }
160  }
161 
165  public function loadLanguageModule(): void
166  {
167  global $DIC;
168  $lng = $DIC->language();
169 
170  if (is_object($lng)) {
171  $lng->loadLanguageModule($this->getPrefix());
172  }
173  }
174 
178  public function txt(string $a_var): string
179  {
180  global $DIC;
181  $lng = $DIC->language();
182  $this->loadLanguageModule();
183 
184  return $lng->txt($this->getPrefix() . "_" . $a_var, $this->getPrefix());
185  }
186 }
txt(string $a_var)
Get Language Variable (prefix will be prepended automatically)
static getInstalledLanguages()
Get the language objects of the installed languages.
__construct(ilPluginInfo $plugin_info)
getAvailableLangFiles()
Get array of all language files in the plugin.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
updateLanguages(?array $a_lang_keys=null)
global $DIC
Definition: shib_login.php:22
Simple value class for information about a plugin.
$txt
Definition: error.php:31
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.
loadLanguageModule()
Load language module for plugin.
$lang
Definition: xapiexit.php:25
global $lng
Definition: privfeed.php:31
static replaceLangModule(string $a_key, string $a_module, array $a_array)
Replace language module array.
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
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...