ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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. 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.

References $file_start, $lng, header, and ILIAS_VERSION.

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  }
const ILIAS_VERSION
Add a drawing to the header
Definition: 04printing.php:69
global $lng
Definition: privfeed.php:17

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 444 of file class.ilLanguageFile.php.

References $lng.

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

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  }
Class ilLanguageFile.
global $lng
Definition: privfeed.php:17
+ 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 254 of file class.ilLanguageFile.php.

References $ilUser, $key, $lng, $name, $tabs, $tpl, comments, date, and getAllParams().

Referenced by write().

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  }
$tpl
Definition: ilias.php:10
if($format !==null) $name
Definition: metadata.php:146
date( 'd-M-Y', $objPHPExcel->getProperties() ->getCreated())
special template class to simplify handling of ITX/PEAR
$ilUser
Definition: imgupload.php:18
getAllParams()
Get array of all parameters.
if(!empty($this->data['faventry'])) $tabs
Definition: disco.tpl.php:124
global $lng
Definition: privfeed.php:17
Add comments
$key
Definition: croninfo.php:18
+ 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 351 of file class.ilLanguageFile.php.

References $comments.

352  {
353  return $this->comments;
354  }

◆ getAllParams()

ilLanguageFile::getAllParams ( )

Get array of all parameters.

Returns
array name => value

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

References $params.

Referenced by build().

334  {
335  return $this->params;
336  }
+ 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 342 of file class.ilLanguageFile.php.

References $values.

343  {
344  return $this->values;
345  }

◆ getComment()

ilLanguageFile::getComment (   $a_module,
  $a_identifier 
)

Get a single comment.

Parameters
stringmodule name
stringindentifier
Returns
string value

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

References comments.

384  {
385  return $this->comments[$a_module . $this->separator . $a_identifier];
386  }
Add comments

◆ getErrorMessage()

ilLanguageFile::getErrorMessage ( )

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

Returns
string error message

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

References $error_message.

314  {
315  return $this->error_message;
316  }

◆ getHeader()

ilLanguageFile::getHeader ( )

Get the header of the original file.

Returns
string

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

References $header.

324  {
325  return $this->header;
326  }

◆ getParam()

ilLanguageFile::getParam (   $a_name)

Get a single parameter.

Parameters
stringparameter name
Returns
string parameter value

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

362  {
363  return $this->params[$a_name];
364  }

◆ getValue()

ilLanguageFile::getValue (   $a_module,
  $a_identifier 
)

Get a single value.

Parameters
stringmodule name
stringindentifier
Returns
string value

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

373  {
374  return $this->values[$a_module . $this->separator . $a_identifier];
375  }

◆ read()

ilLanguageFile::read ( )

Read a language file.

Returns
boolean reading successful

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

References $key, $lng, array, comments, file, and header.

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  }
Reload workbook from saved file
Add a drawing to the header
Definition: 04printing.php:69
Create styles array
The data for the language used.
global $lng
Definition: privfeed.php:17
Add comments
$key
Definition: croninfo.php:18

◆ setAllComments()

ilLanguageFile::setAllComments (   $a_comments)

Set all comments.

Parameters
arraymodule.separator.identifier => comment

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

References comments.

423  {
424  $this->comments = $a_comments;
425  }
Add comments

◆ setAllValues()

ilLanguageFile::setAllValues (   $a_values)

Set all values.

Parameters
arraymodule.separator.identifier => value

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

414  {
415  $this->values = $a_values;
416  }

◆ setComment()

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

Set a single comment.

Parameters
stringmodule name
stringindentifier
stringcomment

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

References comments.

435  {
436  return $this->comments[$a_module . $this->separator . $a_identifier] = $a_comment;
437  }
Add comments

◆ setParam()

ilLanguageFile::setParam (   $a_name,
  $a_value 
)

Set a parameter.

Parameters
stringparameter name
stringparameter value

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

394  {
395  $this->params[$a_name] = $a_value;
396  }

◆ setValue()

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

Set a single value.

Parameters
stringmodule name
stringindentifier
stringvalue

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

405  {
406  $this->values[$a_module . $this->separator . $a_identifier] = $a_value;
407  }

◆ write()

ilLanguageFile::write (   $a_header = '')

Write a language file.

Parameters
string(optional) fixed header for the new file

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

References build().

242  {
243  $fp = fopen($this->lang_file, "w");
244  fwrite($fp, $this->build($a_header));
245  fclose($fp);
246  }
build($a_header='')
Build and get the file content.
+ 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: