ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilLanguageFile.php
Go to the documentation of this file.
1<?php
2/*
3 +-----------------------------------------------------------------------------+
4 | ILIAS open source |
5 +-----------------------------------------------------------------------------+
6 | Copyright (c) 1998-2008 ILIAS open source, University of Cologne |
7 | |
8 | This program is free software; you can redistribute it and/or |
9 | modify it under the terms of the GNU General Public License |
10 | as published by the Free Software Foundation; either version 2 |
11 | of the License, or (at your option) any later version. |
12 | |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software |
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21 +-----------------------------------------------------------------------------+
22*/
23
36{
41 private static $global_file_objects = array();
42
47 private $lang_file;
48
53 private $lang_key;
54
59 private $scope;
60
61
66 private $header;
67
72 private $file_start = "<!-- language file start -->";
73
74
79 private $separator;
80
86
91 private $params = array();
92
97 private $values = array();
98
103 private $comments = array();
104
109 private $error_message = "";
110
117 public function __construct($a_file, $a_key = "", $a_scope = 'global')
118 {
119 global $DIC;
120 $lng = $DIC->language();
121
122 $this->separator = $lng->separator;
123 $this->comment_separator = $lng->comment_separator;
124
125 $this->lang_file = $a_file;
126 $this->lang_key = $a_key;
127 $this->scope = $a_scope;
128
129 // initialize the header of a blank file
130 $this->header = $file_start;
131
132 // Set the default parameters to be written in a new file.
133 // This ensures the correct order of parameters
134
135 $this->params["module"] = "language file";
136 $this->params["modulegroup"] = "language";
137
138 if ($this->scope == "local") {
139 $this->params["based_on"] = "";
140 } else {
141 $this->params["author"] = "";
142 $this->params["version"] = "";
143 }
144
145 $this->params["il_server"] = ILIAS_HTTP_PATH;
146 $this->params["il_version"] = ILIAS_VERSION;
147 $this->params["created"] = "";
148 $this->params["created_by"] = "";
149 }
150
155 public function read()
156 {
157 global $DIC;
158 $lng = $DIC->language();
159
160 $this->header = '';
161 $this->params = array();
162 $this->values = array();
163 $this->comments = array();
164 $this->error_message = "";
165
166 $content = file($this->lang_file);
167 $in_header = true;
168
169 foreach ($content as $line_num => $line) {
170 if ($in_header) {
171 // store the header line
172 $this->header .= $line . "\n";
173
174 // check header end
175 if (trim($line) == $this->file_start) {
176 $in_header = false;
177 continue;
178 } else {
179 // get header params
180 $pos_par = strpos($line, "* @");
181
182 if ($pos_par !== false) {
183 $pos_par += 3;
184 $pos_space = strpos($line, " ", $pos_par);
185 $pos_tab = strpos($line, "\t", $pos_par);
186 if ($pos_space !== false and $pos_tab !== false) {
187 $pos_white = min($pos_space, $pos_tab);
188 } elseif ($pos_space !== false) {
189 $pos_white = $pos_space;
190 } elseif ($pos_tab !== false) {
191 $pos_white = $pos_tab;
192 } else {
193 $pos_white = false;
194 }
195 if ($pos_white) {
196 $param = substr($line, $pos_par, $pos_white - $pos_par);
197 $value = trim(substr($line, $pos_white));
198
199 $this->params[$param] = $value;
200 }
201 }
202 }
203 } else {
204 // separate the lang file entry
205 $separated = explode($this->separator, trim($line));
206
207 // not a valid line with module, identifier and value?
208 if (count($separated) != 3) {
209 $this->error_message =
210 $lng->txt("file_not_valid") . " "
211 . $lng->txt("err_in_line") . " " . $line_num . ". "
212 . $lng->txt("err_count_param");
213 return false;
214 } else {
215 $key = $separated[0] . $this->separator . $separated[1];
216 $value = $separated[2];
217
218 // cut off comment
219 $pos = strpos($value, $this->comment_separator);
220 if ($pos !== false) {
221 $this->comments[$key]
222 = substr($value, $pos + strlen($this->comment_separator));
223
224 $value = substr($value, 0, $pos);
225 }
226 $this->values[$key] = $value;
227 }
228 }
229 }
230 // still in header after parsing the whole file?
231 if ($in_header) {
232 $this->error_message = $lng->txt("file_not_valid") . " " . $lng->txt("err_wrong_header");
233 return false;
234 } else {
235 return true;
236 }
237 }
238
244 public function write($a_header = '')
245 {
246 $fp = fopen($this->lang_file, "w");
247 fwrite($fp, $this->build($a_header));
248 fclose($fp);
249 }
250
257 public function build($a_header = '')
258 {
259 global $DIC;
260 $ilUser = $DIC->user();
261 $lng = $DIC->language();
262
263 if ($a_header) {
264 // take the given header
265 $content = $a_header;
266 } else {
267 // set default params
268 $lng->loadLanguageModule('meta');
269 $lang_name = $lng->txtlng('meta', 'meta_l_' . $this->lang_key, 'en');
270 $this->params["module"] = "language file " . $lang_name;
271 $this->params["created"] = date('Y-m-d H:i:s');
272 $this->params["created_by"] = $ilUser->getFullname() . " <" . $ilUser->getEmail() . ">";
273
274 // build the header
275 $tpl = new ilTemplate("tpl.lang_file_header.html", true, true, "Services/Language");
276 foreach ($this->getAllParams() as $name => $value) {
277 $tabs = ceil((20 - 3 - strlen($name)) / 4);
278 $tabs = $tabs > 0 ? $tabs : 1;
279
280 $tpl->setCurrentBlock('param');
281 $tpl->setVariable('PAR_NAME', $name);
282 $tpl->setVariable('PAR_SPACE', str_repeat("\t", $tabs));
283 $tpl->setVariable('PAR_VALUE', $value);
284 $tpl->parseCurrentBlock();
285 }
286 $txt_scope = $lng->txtlng('administration', 'language_scope_' . $this->scope, 'en');
287 $tpl->setVariable('SCOPE', $txt_scope);
288
289 $content = $tpl->get();
290 }
291
292 // fault tolerant check for adding newline
293 $add_newline = (substr($content, strlen($content) - 1, 1) != "\n");
294
295 // build the content
296 foreach ($this->values as $key => $value) {
297 // add the newline before the line!
298 // a valid lang file should not have a newline at the end!
299 if ($add_newline) {
300 $content .= "\n";
301 }
302 $add_newline = true;
303
304 $content .= $key . $this->separator . $value;
305
306 if ($this->comments[$key]) {
307 $content .= $this->comment_separator . $this->comments[$key];
308 }
309 }
310 return $content;
311 }
312
313
318 public function getErrorMessage()
319 {
321 }
322
323
328 public function getHeader()
329 {
330 return $this->header;
331 }
332
333
338 public function getAllParams()
339 {
340 return $this->params;
341 }
342
347 public function getAllValues()
348 {
349 return $this->values;
350 }
351
356 public function getAllComments()
357 {
358 return $this->comments;
359 }
360
366 public function getParam($a_name)
367 {
368 return $this->params[$a_name];
369 }
370
377 public function getValue($a_module, $a_identifier)
378 {
379 return $this->values[$a_module . $this->separator . $a_identifier];
380 }
381
388 public function getComment($a_module, $a_identifier)
389 {
390 return $this->comments[$a_module . $this->separator . $a_identifier];
391 }
392
398 public function setParam($a_name, $a_value)
399 {
400 $this->params[$a_name] = $a_value;
401 }
402
409 public function setValue($a_module, $a_identifier, $a_value)
410 {
411 $this->values[$a_module . $this->separator . $a_identifier] = $a_value;
412 }
413
418 public function setAllValues($a_values)
419 {
420 $this->values = $a_values;
421 }
422
427 public function setAllComments($a_comments)
428 {
429 $this->comments = $a_comments;
430 }
431
432
439 public function setComment($a_module, $a_identifier, $a_value)
440 {
441 return $this->comments[$a_module . $this->separator . $a_identifier] = $a_comment;
442 }
443
449 public static function _getGlobalLanguageFile($a_lang_key)
450 {
451 global $DIC;
452 $lng = $DIC->language();
453
454 if (!isset(self::$global_file_objects[$a_lang_key])) {
455 $file_object = new ilLanguageFile(
456 $lng->lang_path . "/ilias_" . $a_lang_key . ".lang",
457 $a_lang_key,
458 'global'
459 );
460 $file_object->read();
461
462 self::$global_file_objects[$a_lang_key] = $file_object;
463 }
464
465 return self::$global_file_objects[$a_lang_key];
466 }
467}
An exception for terminatinating execution or to throw for unit testing.
Class ilLanguageFile.
getAllParams()
Get array of all parameters.
setComment($a_module, $a_identifier, $a_value)
Set a single comment.
getComment($a_module, $a_identifier)
Get a single comment.
__construct($a_file, $a_key="", $a_scope='global')
Constructor.
getAllComments()
Get array of all comments.
getAllValues()
Get array of all values.
getValue($a_module, $a_identifier)
Get a single value.
static _getGlobalLanguageFile($a_lang_key)
Read and get a global language file as a singleton object.
read()
Read a language file.
setParam($a_name, $a_value)
Set a parameter.
getHeader()
Get the header of the original file.
setAllValues($a_values)
Set all values.
build($a_header='')
Build and get the file content.
static $global_file_objects
Created global file objects array.
getErrorMessage()
Get the error message of the last read/write operation.
write($a_header='')
Write a language file.
setValue($a_module, $a_identifier, $a_value)
Set a single value.
getParam($a_name)
Get a single parameter.
setAllComments($a_comments)
Set all comments.
special template class to simplify handling of ITX/PEAR
global $DIC
Definition: goto.php:24
$ilUser
Definition: imgupload.php:18
const ILIAS_VERSION
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41
if($format !==null) $name
Definition: metadata.php:230
$lng
$param
Definition: xapitoken.php:29