ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilPluginLanguage.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
26 {
28 
29  public function __construct(ilPluginInfo $plugin_info)
30  {
31  $this->plugin_info = $plugin_info;
32  }
33 
34  protected function getLanguageDirectory(): string
35  {
36  return $this->plugin_info->getPath() . "/lang";
37  }
38 
44  public function getAvailableLangFiles(): array
45  {
46  $directory = $this->getLanguageDirectory();
47  if (!@is_dir($directory)) {
48  return [];
49  }
50 
51  $langs = [];
52 
53  $dir = opendir($directory);
54  while ($file = readdir($dir)) {
55  if ($file === "." || $file === "..") {
56  continue;
57  }
58 
59  // directories
60  if (@is_file($directory . "/" . $file) &&
61  strpos($file, "ilias_") === 0 &&
62  substr($file, strlen($file) - 5) === ".lang") {
63  $langs[] = [
64  "key" => substr($file, 6, 2),
65  "file" => $file
66  ];
67  }
68  }
69 
70  return $langs;
71  }
72 
73  public function hasAvailableLangFiles(): bool
74  {
75  return count($this->getAvailableLangFiles()) > 0;
76  }
77 
78  public function getPrefix(): string
79  {
81  $component = $plugin->getComponent();
82  $slot = $plugin->getPluginSlot();
83 
84  return $component->getId() . "_" . $slot->getId() . "_" . $plugin->getId();
85  }
86 
92  public function updateLanguages(?array $a_lang_keys = null): void
93  {
94  ilGlobalCache::flushAll();
95 
96  // get the keys of all installed languages if keys are not provided
97  if (!isset($a_lang_keys)) {
98  $a_lang_keys = [];
99  foreach (ilObjLanguage::getInstalledLanguages() as $langObj) {
100  if ($langObj->isInstalled()) {
101  $a_lang_keys[] = $langObj->getKey();
102  }
103  }
104  }
105 
106  $langs = $this->getAvailableLangFiles();
107 
108  $prefix = $this->getPrefix();
109 
110  foreach ($langs as $lang) {
111  // check if the language should be updated, otherwise skip it
112  if (!in_array($lang['key'], $a_lang_keys, true)) {
113  continue;
114  }
115 
116  $txt = file($this->getLanguageDirectory() . "/" . $lang["file"]);
117  $lang_array = [];
118 
119  // get locally changed variables of the module (these should be kept)
120  $local_changes = ilObjLanguage::_getLocalChangesByModule($lang['key'], $prefix);
121 
122  // get language data
123  if (is_array($txt)) {
124  foreach ($txt as $row) {
125  if ($row[0] !== "#" && strpos($row, "#:#") > 0) {
126  $a = explode("#:#", trim($row));
127  $identifier = $prefix . "_" . trim($a[0]);
128  $value = trim($a[1]);
129 
130  if (isset($local_changes[$identifier])) {
131  $lang_array[$identifier] = $local_changes[$identifier];
132  } else {
133  $lang_array[$identifier] = $value;
134  ilObjLanguage::replaceLangEntry($prefix, $identifier, $lang["key"], $value);
135  }
136  //echo "<br>-$prefix-".$prefix."_".trim($a[0])."-".$lang["key"]."-";
137  }
138  }
139  }
140 
141  ilObjLanguage::replaceLangModule($lang["key"], $prefix, $lang_array);
142  }
143  }
144 
145  public function uninstall(): void
146  {
147  global $DIC;
148  $ilDB = $DIC->database();
149 
150  // remove all language entries (see ilObjLanguage)
151  // see updateLanguages
152  $prefix = $this->getPrefix();
153  if ($prefix) {
154  $ilDB->manipulate(
155  "DELETE FROM lng_data" .
156  " WHERE module = " . $ilDB->quote($prefix, "text")
157  );
158  $ilDB->manipulate(
159  "DELETE FROM lng_modules" .
160  " WHERE module = " . $ilDB->quote($prefix, "text")
161  );
162  }
163  }
164 
168  public function loadLanguageModule(): void
169  {
170  global $DIC;
171  $lng = $DIC->language();
172 
173  if (is_object($lng)) {
174  $lng->loadLanguageModule($this->getPrefix());
175  }
176  }
177 
181  public function txt(string $a_var): string
182  {
183  global $DIC;
184  $lng = $DIC->language();
185  $this->loadLanguageModule();
186 
187  return $lng->txt($this->getPrefix() . "_" . $a_var, $this->getPrefix());
188  }
189 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
txt(string $a_var)
Get Language Variable (prefix will be prepended automatically)
$lng
static getInstalledLanguages()
Get the language objects of the installed languages.
__construct(ilPluginInfo $plugin_info)
getAvailableLangFiles()
Get array of all language files in the plugin.
global $DIC
Definition: feed.php:28
updateLanguages(?array $a_lang_keys=null)
Simple value class for information about a plugin.
$txt
Definition: error.php:13
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:26
$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...