ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
ilLanguageFile Class Reference

Class ilLanguageFile. More...

+ Collaboration diagram for ilLanguageFile:

Public Member Functions

 __construct ($a_file, $a_key="", $a_scope='global')
 Constructor. More...
 
 read ()
 Read a language file. More...
 
 write ($a_header='')
 Write a language file. More...
 
 build ($a_header='')
 Build and get the file content. More...
 
 getErrorMessage ()
 Get the error message of the last read/write operation. More...
 
 getHeader ()
 Get the header of the original file. More...
 
 getAllParams ()
 Get array of all parameters. More...
 
 getAllValues ()
 Get array of all values. More...
 
 getAllComments ()
 Get array of all comments. More...
 
 getParam ($a_name)
 Get a single parameter. More...
 
 getValue ($a_module, $a_identifier)
 Get a single value. More...
 
 getComment ($a_module, $a_identifier)
 Get a single comment. More...
 
 setParam ($a_name, $a_value)
 Set a parameter. More...
 
 setValue ($a_module, $a_identifier, $a_value)
 Set a single value. More...
 
 setAllValues ($a_values)
 Set all values. More...
 
 setAllComments ($a_comments)
 Set all comments. More...
 
 setComment ($a_module, $a_identifier, $a_value)
 Set a single comment. More...
 

Static Public Member Functions

static _getGlobalLanguageFile ($a_lang_key)
 Read and get a global language file as a singleton object. More...
 

Private Attributes

 $lang_file
 
 $lang_key
 
 $scope
 
 $header
 
 $file_start = "<!-- language file start -->"
 
 $separator
 
 $comment_separator
 
 $params = array()
 
 $values = array()
 
 $comments = array()
 
 $error_message = ""
 

Static Private Attributes

static $global_file_objects = array()
 Created global file objects array. More...
 

Detailed Description

Class ilLanguageFile.

Provides methods for working with language files: read, check and write content, comments and parameters

Author
Fred Neumann fred..nosp@m.neum.nosp@m.ann@f.nosp@m.im.u.nosp@m.ni-er.nosp@m.lang.nosp@m.en.de
Version
$Id$

Definition at line 35 of file class.ilLanguageFile.php.

Constructor & Destructor Documentation

◆ __construct()

ilLanguageFile::__construct (   $a_file,
  $a_key = "",
  $a_scope = 'global' 
)

Constructor.

Parameters
stringlanguage file path and name
string(optional) language key
string(optional) scope ('global', 'local' or 'unchanged')

Definition at line 117 of file class.ilLanguageFile.php.

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 {
138 $this->params["based_on"] = "";
139 }
140 else
141 {
142 $this->params["author"] = "";
143 $this->params["version"] = "";
144 }
145
146 $this->params["il_server"] = ILIAS_HTTP_PATH;
147 $this->params["il_version"] = ILIAS_VERSION;
148 $this->params["created"] = "";
149 $this->params["created_by"] = "";
150 }
const ILIAS_VERSION
global $lng
Definition: privfeed.php:40

References $file_start, $lng, and ILIAS_VERSION.

Member Function Documentation

◆ _getGlobalLanguageFile()

static ilLanguageFile::_getGlobalLanguageFile (   $a_lang_key)
static

Read and get a global language file as a singleton object.

Parameters
stringlanguage key
Returns
object language file object (with contents)

Definition at line 477 of file class.ilLanguageFile.php.

478 {
479 global $lng;
480
481 if (!isset(self::$global_file_objects[$a_lang_key]))
482 {
483 $file_object = new ilLanguageFile(
484 $lng->lang_path . "/ilias_" . $a_lang_key . ".lang",
485 $a_lang_key, 'global');
486 $file_object->read();
487
488 self::$global_file_objects[$a_lang_key] = $file_object;
489 }
490
491 return self::$global_file_objects[$a_lang_key];
492 }
Class ilLanguageFile.

References $lng.

Referenced by ilObjLanguageExt\_saveValues(), and ilObjLanguageExt\getGlobalLanguageFile().

+ Here is the caller graph for this function:

◆ build()

ilLanguageFile::build (   $a_header = '')

Build and get the file content.

Parameters
string(optional) fixed header for the new file
Returns
string language file content

Definition at line 280 of file class.ilLanguageFile.php.

281 {
282 global $ilUser, $lng;
283
284 if ($a_header)
285 {
286 // take the given header
287 $content = $a_header;
288 }
289 else
290 {
291 // set default params
292 $lng->loadLanguageModule('meta');
293 $lang_name = $lng->txtlng('meta','meta_l_'.$this->lang_key,'en');
294 $this->params["module"] = "language file ". $lang_name;
295 $this->params["created"] = date('Y-m-d H:i:s');
296 $this->params["created_by"] = $ilUser->getFullname()." <".$ilUser->getEmail().">";
297
298 // build the header
299 $tpl = new ilTemplate("tpl.lang_file_header.html", true,true, "Services/Language");
300 foreach ($this->getAllParams() as $name => $value)
301 {
302 $tabs = ceil((20 - 3 - strlen($name)) / 4);
303 $tabs = $tabs > 0 ? $tabs : 1;
304
305 $tpl->setCurrentBlock('param');
306 $tpl->setVariable('PAR_NAME', $name);
307 $tpl->setVariable('PAR_SPACE', str_repeat("\t", $tabs));
308 $tpl->setVariable('PAR_VALUE', $value);
309 $tpl->parseCurrentBlock();
310 }
311 $txt_scope = $lng->txtlng('administration','language_scope_'.$this->scope, 'en');
312 $tpl->setVariable('SCOPE', $txt_scope);
313
314 $content = $tpl->get();
315 }
316
317 // fault tolerant check for adding newline
318 $add_newline = (substr($content, strlen($content)-1, 1) != "\n");
319
320 // build the content
321 foreach ($this->values as $key => $value)
322 {
323 // add the newline before the line!
324 // a valid lang file should not have a newline at the end!
325 if ($add_newline)
326 {
327 $content .= "\n";
328 }
329 $add_newline = true;
330
331 $content .= $key . $this->separator . $value;
332
333 if ($this->comments[$key])
334 {
335 $content .= $this->comment_separator . $this->comments[$key];
336 }
337 }
338 return $content;
339 }
global $tpl
Definition: ilias.php:8
getAllParams()
Get array of all parameters.
special template class to simplify handling of ITX/PEAR
global $ilUser
Definition: imgupload.php:15

References $ilUser, $lng, $tpl, and getAllParams().

Referenced by write().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getAllComments()

ilLanguageFile::getAllComments ( )

Get array of all comments.

Returns
array module.separator.identifier => comment

Definition at line 384 of file class.ilLanguageFile.php.

385 {
386 return $this->comments;
387 }

References $comments.

◆ getAllParams()

ilLanguageFile::getAllParams ( )

Get array of all parameters.

Returns
array name => value

Definition at line 366 of file class.ilLanguageFile.php.

367 {
368 return $this->params;
369 }

References $params.

Referenced by build().

+ Here is the caller graph for this function:

◆ getAllValues()

ilLanguageFile::getAllValues ( )

Get array of all values.

Returns
array module.separator.identifier => value

Definition at line 375 of file class.ilLanguageFile.php.

376 {
377 return $this->values;
378 }

References $values.

◆ getComment()

ilLanguageFile::getComment (   $a_module,
  $a_identifier 
)

Get a single comment.

Parameters
stringmodule name
stringindentifier
Returns
string value

Definition at line 416 of file class.ilLanguageFile.php.

417 {
418 return $this->comments[$a_module.$this->separator.$a_identifier];
419 }

◆ getErrorMessage()

ilLanguageFile::getErrorMessage ( )

Get the error message of the last read/write operation.

Returns
string error message

Definition at line 346 of file class.ilLanguageFile.php.

347 {
349 }

References $error_message.

◆ getHeader()

ilLanguageFile::getHeader ( )

Get the header of the original file.

Returns
string

Definition at line 356 of file class.ilLanguageFile.php.

357 {
358 return $this->header;
359 }

References $header.

◆ getParam()

ilLanguageFile::getParam (   $a_name)

Get a single parameter.

Parameters
stringparameter name
Returns
string parameter value

Definition at line 394 of file class.ilLanguageFile.php.

395 {
396 return $this->params[$a_name];
397 }

◆ getValue()

ilLanguageFile::getValue (   $a_module,
  $a_identifier 
)

Get a single value.

Parameters
stringmodule name
stringindentifier
Returns
string value

Definition at line 405 of file class.ilLanguageFile.php.

406 {
407 return $this->values[$a_module.$this->separator.$a_identifier];
408 }

◆ read()

ilLanguageFile::read ( )

Read a language file.

Returns
boolean reading successful

Definition at line 156 of file class.ilLanguageFile.php.

157 {
158 global $lng;
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 {
171 if ($in_header)
172 {
173 // store the header line
174 $this->header .= $line . "\n";
175
176 // check header end
177 if (trim($line) == $this->file_start)
178 {
179 $in_header = false;
180 continue;
181 }
182 else
183 {
184 // get header params
185 $pos_par = strpos($line, "* @");
186
187 if ($pos_par !== false)
188 {
189 $pos_par += 3;
190 $pos_space = strpos($line, " ", $pos_par);
191 $pos_tab = strpos($line, "\t", $pos_par);
192 if ($pos_space !== false and $pos_tab !== false)
193 {
194 $pos_white = min($pos_space, $pos_tab);
195 }
196 elseif ($pos_space !== false)
197 {
198 $pos_white = $pos_space;
199 }
200 elseif ($pos_tab !== false)
201 {
202 $pos_white = $pos_tab;
203 }
204 else
205 {
206 $pos_white = false;
207 }
208 if ($pos_white)
209 {
210 $param = substr($line, $pos_par, $pos_white-$pos_par);
211 $value = trim(substr($line, $pos_white));
212
213 $this->params[$param] = $value;
214 }
215 }
216 }
217 }
218 else
219 {
220 // separate the lang file entry
221 $separated = explode($this->separator, trim($line));
222
223 // not a valid line with module, identifier and value?
224 if (count($separated) != 3)
225 {
226 $this->error_message =
227 $lng->txt("file_not_valid"). " "
228 .$lng->txt("err_in_line")." ". $line_num . ". "
229 .$lng->txt("err_count_param");
230 return false;
231 }
232 else
233 {
234 $key = $separated[0].$this->separator.$separated[1];
235 $value = $separated[2];
236
237 // cut off comment
238 $pos = strpos($value, $this->comment_separator);
239 if ($pos !== false)
240 {
241 $this->comments[$key]
242 = substr($value , $pos + strlen($this->comment_separator));
243
244 $value = substr($value , 0 , $pos);
245 }
246 $this->values[$key] = $value;
247 }
248 }
249 }
250 // still in header after parsing the whole file?
251 if ($in_header)
252 {
253 $this->error_message = $lng->txt("file_not_valid")." ".$lng->txt("err_wrong_header");
254 return false;
255 }
256 else
257 {
258 return true;
259 }
260 }

References $lng.

◆ setAllComments()

ilLanguageFile::setAllComments (   $a_comments)

Set all comments.

Parameters
arraymodule.separator.identifier => comment

Definition at line 455 of file class.ilLanguageFile.php.

456 {
457 $this->comments = $a_comments;
458 }

◆ setAllValues()

ilLanguageFile::setAllValues (   $a_values)

Set all values.

Parameters
arraymodule.separator.identifier => value

Definition at line 446 of file class.ilLanguageFile.php.

447 {
448 $this->values = $a_values;
449 }

◆ setComment()

ilLanguageFile::setComment (   $a_module,
  $a_identifier,
  $a_value 
)

Set a single comment.

Parameters
stringmodule name
stringindentifier
stringcomment

Definition at line 467 of file class.ilLanguageFile.php.

468 {
469 return $this->comments[$a_module.$this->separator.$a_identifier] = $a_comment;
470 }

◆ setParam()

ilLanguageFile::setParam (   $a_name,
  $a_value 
)

Set a parameter.

Parameters
stringparameter name
stringparameter value

Definition at line 426 of file class.ilLanguageFile.php.

427 {
428 $this->params[$a_name] = $a_value;
429 }

◆ setValue()

ilLanguageFile::setValue (   $a_module,
  $a_identifier,
  $a_value 
)

Set a single value.

Parameters
stringmodule name
stringindentifier
stringvalue

Definition at line 437 of file class.ilLanguageFile.php.

438 {
439 $this->values[$a_module.$this->separator.$a_identifier] = $a_value;
440 }

◆ write()

ilLanguageFile::write (   $a_header = '')

Write a language file.

Parameters
string(optional) fixed header for the new file

Definition at line 267 of file class.ilLanguageFile.php.

268 {
269 $fp = fopen($this->lang_file, "w");
270 fwrite($fp, $this->build($a_header));
271 fclose($fp);
272 }
build($a_header='')
Build and get the file content.

References build().

+ Here is the call graph for this function:

Field Documentation

◆ $comment_separator

ilLanguageFile::$comment_separator
private

Definition at line 85 of file class.ilLanguageFile.php.

◆ $comments

ilLanguageFile::$comments = array()
private

Definition at line 103 of file class.ilLanguageFile.php.

Referenced by getAllComments().

◆ $error_message

ilLanguageFile::$error_message = ""
private

Definition at line 109 of file class.ilLanguageFile.php.

Referenced by getErrorMessage().

◆ $file_start

ilLanguageFile::$file_start = "<!-- language file start -->"
private

Definition at line 72 of file class.ilLanguageFile.php.

Referenced by __construct().

◆ $global_file_objects

ilLanguageFile::$global_file_objects = array()
staticprivate

Created global file objects array.

Definition at line 41 of file class.ilLanguageFile.php.

◆ $header

ilLanguageFile::$header
private

Definition at line 66 of file class.ilLanguageFile.php.

Referenced by getHeader().

◆ $lang_file

ilLanguageFile::$lang_file
private

Definition at line 47 of file class.ilLanguageFile.php.

◆ $lang_key

ilLanguageFile::$lang_key
private

Definition at line 53 of file class.ilLanguageFile.php.

◆ $params

ilLanguageFile::$params = array()
private

Definition at line 91 of file class.ilLanguageFile.php.

Referenced by getAllParams().

◆ $scope

ilLanguageFile::$scope
private

Definition at line 59 of file class.ilLanguageFile.php.

◆ $separator

ilLanguageFile::$separator
private

Definition at line 79 of file class.ilLanguageFile.php.

◆ $values

ilLanguageFile::$values = array()
private

Definition at line 97 of file class.ilLanguageFile.php.

Referenced by getAllValues().


The documentation for this class was generated from the following file: