ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilLanguageFile.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
33{
34 private static array $global_file_objects;
35 private string $lang_file;
36 private string $lang_key;
37 private string $scope;
38 private string $header;
39 private string $file_start = "<!-- language file start -->";
40 private string $separator;
41 private string $comment_separator;
42 private array $params;
43 private array $values;
44 private array $comments;
45 private string $error_message = "";
46
53 public function __construct(string $a_file, string $a_key = "", string $a_scope = "global")
54 {
55 global $DIC;
56 $lng = $DIC->language();
57
58 $this->separator = $lng->separator;
59 $this->comment_separator = $lng->comment_separator;
60
61 $this->lang_file = $a_file;
62 $this->lang_key = $a_key;
63 $this->scope = $a_scope;
64
65 // initialize the header of a blank file
67
68 // Set the default parameters to be written in a new file.
69 // This ensures the correct order of parameters
70
71 $this->params["module"] = "language file";
72 $this->params["modulegroup"] = "language";
73
74 if ($this->scope === "local") {
75 $this->params["based_on"] = "";
76 } else {
77 $this->params["author"] = "";
78 $this->params["version"] = "";
79 }
80
81 $this->params["il_server"] = ILIAS_HTTP_PATH;
82 $this->params["il_version"] = ILIAS_VERSION;
83 $this->params["created"] = "";
84 $this->params["created_by"] = "";
85 }
86
91 public function read(): bool
92 {
93 global $DIC;
94 $lng = $DIC->language();
95
96 $this->header = '';
97 $this->params = array();
98 $this->values = array();
99 $this->comments = array();
100 $this->error_message = "";
101
102 $content = file($this->lang_file);
103 $in_header = true;
104
105 foreach ($content as $line_num => $line) {
106 if ($in_header) {
107 // store the header line
108 $this->header .= $line . "\n";
109
110 // check header end
111 if (trim($line) === $this->file_start) {
112 $in_header = false;
113 } else {
114 // get header params
115 $pos_par = strpos($line, "* @");
116
117 if (strpos($line, "* @") !== false) {
118 $pos_par += 3;
119 $pos_space = strpos($line, " ", $pos_par);
120 $pos_tab = strpos($line, "\t", $pos_par);
121 if ($pos_space !== false && $pos_tab !== false) {
122 $pos_white = min($pos_space, $pos_tab);
123 } elseif ($pos_space !== false) {
124 $pos_white = $pos_space;
125 } elseif ($pos_tab !== false) {
126 $pos_white = $pos_tab;
127 } else {
128 $pos_white = false;
129 }
130 if ($pos_white) {
131 $param = substr($line, $pos_par, $pos_white - $pos_par);
132 $value = trim(substr($line, $pos_white));
133
134 $this->params[$param] = $value;
135 }
136 }
137 }
138 } else {
139 // separate the lang file entry
140 $separated = explode($this->separator, trim($line));
141
142 // not a valid line with module, identifier and value?
143 if (count($separated) !== 3) {
144 $this->error_message =
145 $lng->txt("file_not_valid") . " "
146 . $lng->txt("err_in_line") . " " . $line_num . ". "
147 . $lng->txt("err_count_param");
148 return false;
149 } else {
150 $key = $separated[0] . $this->separator . $separated[1];
151 $value = $separated[2];
152
153 // cut off comment
154 $pos = strpos($value, $this->comment_separator);
155 if ($pos !== false) {
156 $this->comments[$key]
157 = substr($value, $pos + strlen($this->comment_separator));
158
159 $value = substr($value, 0, $pos);
160 }
161 $this->values[$key] = $value;
162 }
163 }
164 }
165 // still in header after parsing the whole file?
166 if ($in_header) {
167 $this->error_message = $lng->txt("file_not_valid") . " " . $lng->txt("err_wrong_header");
168 return false;
169 }
170
171 return true;
172 }
173
179 public function write(string $a_header = ""): void
180 {
181 $fp = fopen($this->lang_file, 'wb');
182 fwrite($fp, $this->build($a_header));
183 fclose($fp);
184 }
185
191 public function build(string $a_header = ''): string
192 {
193 global $DIC;
194 $ilUser = $DIC->user();
195 $lng = $DIC->language();
196
197 if ($a_header) {
198 // take the given header
199 $content = $a_header;
200 } else {
201 // set default params
202 $lng->loadLanguageModule("meta");
203 $lang_name = $lng->txtlng("meta", "meta_l_" . $this->lang_key, "en");
204 $this->params["module"] = "language file " . $lang_name;
205 $this->params["created"] = gmdate("Y-m-d H:i:s");
206 $this->params["created_by"] = $ilUser->getFullname() . " <" . $ilUser->getEmail() . ">";
207
208 // build the header
209 $tpl = new ilTemplate("tpl.lang_file_header.html", true, true, "components/ILIAS/Language");
210 foreach ($this->getAllParams() as $name => $value) {
211 $tabs = intval(ceil((20 - 3 - strlen($name)) / 4));
212 $tabs = $tabs > 0 ? $tabs : 1;
213
214 $tpl->setCurrentBlock("param");
215 $tpl->setVariable("PAR_NAME", $name);
216 $tpl->setVariable("PAR_SPACE", str_repeat("\t", $tabs));
217 $tpl->setVariable("PAR_VALUE", $value);
218 $tpl->parseCurrentBlock();
219 }
220 $txt_scope = $lng->txtlng("administration", "language_scope_" . $this->scope, "en");
221 $tpl->setVariable("SCOPE", $txt_scope);
222
223 $content = $tpl->get();
224 }
225
226 // fault tolerant check for adding newline
227 $add_newline = (substr($content, strlen($content) - 1, 1) !== "\n");
228
229 // build the content
230 foreach ($this->values as $key => $value) {
231 // add the newline before the line!
232 // a valid lang file should not have a newline at the end!
233 if ($add_newline) {
234 $content .= "\n";
235 }
236 $add_newline = true;
237
238 $content .= $key . $this->separator . $value;
239
240 if (isset($this->comments[$key])) {
241 $content .= $this->comment_separator . $this->comments[$key];
242 }
243 }
244 return $content;
245 }
246
247
251 public function getErrorMessage(): string
252 {
254 }
255
256
260 public function getHeader(): string
261 {
262 return $this->header;
263 }
264
265
270 public function getAllParams(): array
271 {
272 return $this->params;
273 }
274
279 public function getAllValues(): array
280 {
281 return $this->values;
282 }
283
288 public function getAllComments(): array
289 {
290 return $this->comments;
291 }
292
297 public function getParam(string $a_name): string
298 {
299 return $this->params[$a_name] ?? '';
300 }
301
307 public function getValue(string $a_module, string $a_identifier): string
308 {
309 return $this->values[$a_module . $this->separator . $a_identifier];
310 }
311
317 public function getComment(string $a_module, string $a_identifier): string
318 {
319 return $this->comments[$a_module . $this->separator . $a_identifier];
320 }
321
327 public function setParam(string $a_name, string $a_value): void
328 {
329 $this->params[$a_name] = $a_value;
330 }
331
338 public function setValue(string $a_module, string $a_identifier, string $a_value): void
339 {
340 $this->values[$a_module . $this->separator . $a_identifier] = $a_value;
341 }
342
347 public function setAllValues(array $a_values): void
348 {
349 $this->values = $a_values;
350 }
351
356 public function setAllComments(array $a_comments): void
357 {
358 $this->comments = $a_comments;
359 }
360
361
368 public function setComment(string $a_module, string $a_identifier, string $a_comment): string
369 {
370 return $this->comments[$a_module . $this->separator . $a_identifier] = $a_comment;
371 }
372
378 public static function _getGlobalLanguageFile(string $a_lang_key)
379 {
380 global $DIC;
381 $lng = $DIC->language();
382
383 if (!isset(self::$global_file_objects[$a_lang_key])) {
384 $file_object = new ilLanguageFile(
385 $lng->lang_path . "/ilias_" . $a_lang_key . ".lang",
386 $a_lang_key,
387 "global"
388 );
389 $file_object->read();
390
391 self::$global_file_objects[$a_lang_key] = $file_object;
392 }
393
394 return self::$global_file_objects[$a_lang_key];
395 }
396}
Class ilLanguageFile.
write(string $a_header="")
Write a language file.
getAllParams()
Get array of all parameters Return array [name => value].
static _getGlobalLanguageFile(string $a_lang_key)
Read and get a global language file as a singleton object $a_lang_key language key.
__construct(string $a_file, string $a_key="", string $a_scope="global")
Constructor $a_file language file path and name $a_key (optional) language key $a_scope (optional) sc...
setValue(string $a_module, string $a_identifier, string $a_value)
Set a single value $a_module module name $a_identifier indentifier $a_value value.
getAllComments()
Get array of all comments Return array [module.separator.identifier => comment].
getComment(string $a_module, string $a_identifier)
Get a single comment $a_module module name $a_identifier indentifier.
getParam(string $a_name)
Get a single parameter $a_name parameter name.
getAllValues()
Get array of all values Return array [module.separator.identifier => value].
setComment(string $a_module, string $a_identifier, string $a_comment)
Set a single comment $a_module module name $a_identifier indentifier $a_comment comment.
static array $global_file_objects
read()
Read a language file Return true, if reading successful.
getHeader()
Get the header of the original file.
setAllValues(array $a_values)
Set all values $a_values [module.separator.identifier => value].
getValue(string $a_module, string $a_identifier)
Get a single value $a_module module name $a_identifier indentifier.
setAllComments(array $a_comments)
Set all comments $a_comments [module.separator.identifier => comment].
setParam(string $a_name, string $a_value)
Set a parameter $a_name parameter name $a_value parameter value.
getErrorMessage()
Get the error message of the last read/write operation.
build(string $a_header='')
Build and get the file content.
special template class to simplify handling of ITX/PEAR
const ILIAS_VERSION
global $lng
Definition: privfeed.php:31
global $DIC
Definition: shib_login.php:26
$param
Definition: xapitoken.php:46