ILIAS  release_7 Revision v7.30-3-g800a261c036
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 $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 }
global $DIC
Definition: goto.php:24
const ILIAS_VERSION
$lng

References $DIC, $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 449 of file class.ilLanguageFile.php.

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 }
Class ilLanguageFile.

References $DIC, and $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 257 of file class.ilLanguageFile.php.

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 }
getAllParams()
Get array of all parameters.
special template class to simplify handling of ITX/PEAR
$ilUser
Definition: imgupload.php:18
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

References $DIC, $ilUser, $lng, $name, $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 356 of file class.ilLanguageFile.php.

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

References $comments.

◆ getAllParams()

ilLanguageFile::getAllParams ( )

Get array of all parameters.

Returns
array name => value

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

339 {
340 return $this->params;
341 }

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

348 {
349 return $this->values;
350 }

References $values.

◆ getComment()

ilLanguageFile::getComment (   $a_module,
  $a_identifier 
)

Get a single comment.

Parameters
stringmodule name
stringindentifier
Returns
string value

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

389 {
390 return $this->comments[$a_module . $this->separator . $a_identifier];
391 }

◆ getErrorMessage()

ilLanguageFile::getErrorMessage ( )

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

Returns
string error message

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

319 {
321 }

References $error_message.

◆ getHeader()

ilLanguageFile::getHeader ( )

Get the header of the original file.

Returns
string

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

329 {
330 return $this->header;
331 }

References $header.

◆ getParam()

ilLanguageFile::getParam (   $a_name)

Get a single parameter.

Parameters
stringparameter name
Returns
string parameter value

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

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

◆ getValue()

ilLanguageFile::getValue (   $a_module,
  $a_identifier 
)

Get a single value.

Parameters
stringmodule name
stringindentifier
Returns
string value

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

378 {
379 return $this->values[$a_module . $this->separator . $a_identifier];
380 }

◆ read()

ilLanguageFile::read ( )

Read a language file.

Returns
boolean reading successful

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

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 }
$param
Definition: xapitoken.php:29

References $DIC, $lng, and $param.

◆ setAllComments()

ilLanguageFile::setAllComments (   $a_comments)

Set all comments.

Parameters
arraymodule.separator.identifier => comment

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

428 {
429 $this->comments = $a_comments;
430 }

◆ setAllValues()

ilLanguageFile::setAllValues (   $a_values)

Set all values.

Parameters
arraymodule.separator.identifier => value

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

419 {
420 $this->values = $a_values;
421 }

◆ setComment()

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

Set a single comment.

Parameters
stringmodule name
stringindentifier
stringcomment

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

440 {
441 return $this->comments[$a_module . $this->separator . $a_identifier] = $a_comment;
442 }

◆ setParam()

ilLanguageFile::setParam (   $a_name,
  $a_value 
)

Set a parameter.

Parameters
stringparameter name
stringparameter value

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

399 {
400 $this->params[$a_name] = $a_value;
401 }

◆ setValue()

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

Set a single value.

Parameters
stringmodule name
stringindentifier
stringvalue

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

410 {
411 $this->values[$a_module . $this->separator . $a_identifier] = $a_value;
412 }

◆ write()

ilLanguageFile::write (   $a_header = '')

Write a language file.

Parameters
string(optional) fixed header for the new file

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

245 {
246 $fp = fopen($this->lang_file, "w");
247 fwrite($fp, $this->build($a_header));
248 fclose($fp);
249 }
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: