ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 $lng;
120 $this->separator = $lng->separator;
121 $this->comment_separator = $lng->comment_separator;
122
123 $this->lang_file = $a_file;
124 $this->lang_key = $a_key;
125 $this->scope = $a_scope;
126
127 // initialize the header of a blank file
128 $this->header = $file_start;
129
130 // Set the default parameters to be written in a new file.
131 // This ensures the correct order of parameters
132
133 $this->params["module"] = "language file";
134 $this->params["modulegroup"] = "language";
135
136 if ($this->scope == "local") {
137 $this->params["based_on"] = "";
138 } else {
139 $this->params["author"] = "";
140 $this->params["version"] = "";
141 }
142
143 $this->params["il_server"] = ILIAS_HTTP_PATH;
144 $this->params["il_version"] = ILIAS_VERSION;
145 $this->params["created"] = "";
146 $this->params["created_by"] = "";
147 }
148
153 public function read()
154 {
155 global $lng;
156
157 $this->header = '';
158 $this->params = array();
159 $this->values = array();
160 $this->comments = array();
161 $this->error_message = "";
162
163 $content = file($this->lang_file);
164 $in_header = true;
165
166 foreach ($content as $line_num => $line) {
167 if ($in_header) {
168 // store the header line
169 $this->header .= $line . "\n";
170
171 // check header end
172 if (trim($line) == $this->file_start) {
173 $in_header = false;
174 continue;
175 } else {
176 // get header params
177 $pos_par = strpos($line, "* @");
178
179 if ($pos_par !== false) {
180 $pos_par += 3;
181 $pos_space = strpos($line, " ", $pos_par);
182 $pos_tab = strpos($line, "\t", $pos_par);
183 if ($pos_space !== false and $pos_tab !== false) {
184 $pos_white = min($pos_space, $pos_tab);
185 } elseif ($pos_space !== false) {
186 $pos_white = $pos_space;
187 } elseif ($pos_tab !== false) {
188 $pos_white = $pos_tab;
189 } else {
190 $pos_white = false;
191 }
192 if ($pos_white) {
193 $param = substr($line, $pos_par, $pos_white-$pos_par);
194 $value = trim(substr($line, $pos_white));
195
196 $this->params[$param] = $value;
197 }
198 }
199 }
200 } else {
201 // separate the lang file entry
202 $separated = explode($this->separator, trim($line));
203
204 // not a valid line with module, identifier and value?
205 if (count($separated) != 3) {
206 $this->error_message =
207 $lng->txt("file_not_valid") . " "
208 . $lng->txt("err_in_line") . " " . $line_num . ". "
209 . $lng->txt("err_count_param");
210 return false;
211 } else {
212 $key = $separated[0] . $this->separator . $separated[1];
213 $value = $separated[2];
214
215 // cut off comment
216 $pos = strpos($value, $this->comment_separator);
217 if ($pos !== false) {
218 $this->comments[$key]
219 = substr($value, $pos + strlen($this->comment_separator));
220
221 $value = substr($value, 0, $pos);
222 }
223 $this->values[$key] = $value;
224 }
225 }
226 }
227 // still in header after parsing the whole file?
228 if ($in_header) {
229 $this->error_message = $lng->txt("file_not_valid") . " " . $lng->txt("err_wrong_header");
230 return false;
231 } else {
232 return true;
233 }
234 }
235
241 public function write($a_header = '')
242 {
243 $fp = fopen($this->lang_file, "w");
244 fwrite($fp, $this->build($a_header));
245 fclose($fp);
246 }
247
254 public function build($a_header = '')
255 {
256 global $ilUser, $lng;
257
258 if ($a_header) {
259 // take the given header
260 $content = $a_header;
261 } else {
262 // set default params
263 $lng->loadLanguageModule('meta');
264 $lang_name = $lng->txtlng('meta', 'meta_l_' . $this->lang_key, 'en');
265 $this->params["module"] = "language file " . $lang_name;
266 $this->params["created"] = date('Y-m-d H:i:s');
267 $this->params["created_by"] = $ilUser->getFullname() . " <" . $ilUser->getEmail() . ">";
268
269 // build the header
270 $tpl = new ilTemplate("tpl.lang_file_header.html", true, true, "Services/Language");
271 foreach ($this->getAllParams() as $name => $value) {
272 $tabs = ceil((20 - 3 - strlen($name)) / 4);
273 $tabs = $tabs > 0 ? $tabs : 1;
274
275 $tpl->setCurrentBlock('param');
276 $tpl->setVariable('PAR_NAME', $name);
277 $tpl->setVariable('PAR_SPACE', str_repeat("\t", $tabs));
278 $tpl->setVariable('PAR_VALUE', $value);
279 $tpl->parseCurrentBlock();
280 }
281 $txt_scope = $lng->txtlng('administration', 'language_scope_' . $this->scope, 'en');
282 $tpl->setVariable('SCOPE', $txt_scope);
283
284 $content = $tpl->get();
285 }
286
287 // fault tolerant check for adding newline
288 $add_newline = (substr($content, strlen($content)-1, 1) != "\n");
289
290 // build the content
291 foreach ($this->values as $key => $value) {
292 // add the newline before the line!
293 // a valid lang file should not have a newline at the end!
294 if ($add_newline) {
295 $content .= "\n";
296 }
297 $add_newline = true;
298
299 $content .= $key . $this->separator . $value;
300
301 if ($this->comments[$key]) {
302 $content .= $this->comment_separator . $this->comments[$key];
303 }
304 }
305 return $content;
306 }
307
308
313 public function getErrorMessage()
314 {
316 }
317
318
323 public function getHeader()
324 {
325 return $this->header;
326 }
327
328
333 public function getAllParams()
334 {
335 return $this->params;
336 }
337
342 public function getAllValues()
343 {
344 return $this->values;
345 }
346
351 public function getAllComments()
352 {
353 return $this->comments;
354 }
355
361 public function getParam($a_name)
362 {
363 return $this->params[$a_name];
364 }
365
372 public function getValue($a_module, $a_identifier)
373 {
374 return $this->values[$a_module . $this->separator . $a_identifier];
375 }
376
383 public function getComment($a_module, $a_identifier)
384 {
385 return $this->comments[$a_module . $this->separator . $a_identifier];
386 }
387
393 public function setParam($a_name, $a_value)
394 {
395 $this->params[$a_name] = $a_value;
396 }
397
404 public function setValue($a_module, $a_identifier, $a_value)
405 {
406 $this->values[$a_module . $this->separator . $a_identifier] = $a_value;
407 }
408
413 public function setAllValues($a_values)
414 {
415 $this->values = $a_values;
416 }
417
422 public function setAllComments($a_comments)
423 {
424 $this->comments = $a_comments;
425 }
426
427
434 public function setComment($a_module, $a_identifier, $a_value)
435 {
436 return $this->comments[$a_module . $this->separator . $a_identifier] = $a_comment;
437 }
438
444 public static function _getGlobalLanguageFile($a_lang_key)
445 {
446 global $lng;
447
448 if (!isset(self::$global_file_objects[$a_lang_key])) {
449 $file_object = new ilLanguageFile(
450 $lng->lang_path . "/ilias_" . $a_lang_key . ".lang",
451 $a_lang_key,
452 'global'
453 );
454 $file_object->read();
455
456 self::$global_file_objects[$a_lang_key] = $file_object;
457 }
458
459 return self::$global_file_objects[$a_lang_key];
460 }
461}
date( 'd-M-Y', $objPHPExcel->getProperties() ->getCreated())
$tpl
Definition: ilias.php:10
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
$key
Definition: croninfo.php:18
if(!empty($this->data['faventry'])) $tabs
Definition: disco.tpl.php:124
const ILIAS_VERSION
if($format !==null) $name
Definition: metadata.php:146
global $lng
Definition: privfeed.php:17
$ilUser
Definition: imgupload.php:18