ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
ilLanguageFile Class Reference

Class ilLanguageFile. More...

+ Collaboration diagram for ilLanguageFile:

Public Member Functions

 __construct (string $a_file, string $a_key="", string $a_scope="global")
 Constructor $a_file language file path and name $a_key (optional) language key $a_scope (optional) scope ('global', 'local' or 'unchanged') More...
 
 read ()
 Read a language file Return true, if reading successful. More...
 
 keepOriginalLines (array $keys)
 build()/write() emit the lines of $keys as read(), without whitespace normalisation, e.g. More...
 
 write (string $a_header="")
 Write a language file. More...
 
 build (string $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 Return array [name => value]. More...
 
 getAllValues ()
 Get array of all values Return array [module.separator.identifier => value]. More...
 
 getAllComments ()
 Get array of all comments Return array [module.separator.identifier => comment]. More...
 
 getParam (string $a_name)
 Get a single parameter $a_name parameter name. More...
 
 getValue (string $a_module, string $a_identifier)
 Get a single value $a_module module name $a_identifier indentifier. More...
 
 getComment (string $a_module, string $a_identifier)
 Get a single comment $a_module module name $a_identifier indentifier. More...
 
 setParam (string $a_name, string $a_value)
 Set a parameter $a_name parameter name $a_value parameter value. More...
 
 setValue (string $a_module, string $a_identifier, string $a_value)
 Set a single value $a_module module name $a_identifier indentifier $a_value value. More...
 
 setAllValues (array $a_values)
 Set all values $a_values [module.separator.identifier => value]. More...
 
 setAllComments (array $a_comments)
 Set all comments $a_comments [module.separator.identifier => comment]. More...
 
 setComment (string $a_module, string $a_identifier, string $a_comment)
 Set a single comment $a_module module name $a_identifier indentifier $a_comment comment. More...
 

Static Public Member Functions

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

Private Member Functions

 buildLine (string $key, string $value, ?string $comment)
 

Private Attributes

string $lang_file
 
string $lang_key
 
string $scope
 
string $header
 
string $file_start = "<!-- language file start -->"
 
string $separator
 
string $comment_separator
 
array $params
 
array $values
 
array $comments
 
string $error_message = ""
 
array $read_lines = []
 
array $read_values = []
 
array $read_comments = []
 
array $kept_keys = []
 

Static Private Attributes

static array $global_file_objects
 

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

Constructor & Destructor Documentation

◆ __construct()

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

Constructor $a_file language file path and name $a_key (optional) language key $a_scope (optional) scope ('global', 'local' or 'unchanged')

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

62 {
63 global $DIC;
64 $lng = $DIC->language();
65
66 $this->separator = $lng->separator;
67 $this->comment_separator = $lng->comment_separator;
68
69 $this->lang_file = $a_file;
70 $this->lang_key = $a_key;
71 $this->scope = $a_scope;
72
73 // initialize the header of a blank file
75
76 // Set the default parameters to be written in a new file.
77 // This ensures the correct order of parameters
78
79 $this->params["module"] = "language file";
80 $this->params["modulegroup"] = "language";
81
82 if ($this->scope === "local") {
83 $this->params["based_on"] = "";
84 } else {
85 $this->params["author"] = "";
86 $this->params["version"] = "";
87 }
88
89 $this->params["il_server"] = ILIAS_HTTP_PATH;
90 $this->params["il_version"] = ILIAS_VERSION;
91 $this->params["created"] = "";
92 $this->params["created_by"] = "";
93 }
const ILIAS_VERSION
global $lng
Definition: privfeed.php:26
global $DIC
Definition: shib_login.php:26

References $DIC, $file_start, $lng, ILIAS\UI\examples\Symbol\Glyph\Header\header(), and ILIAS_VERSION.

+ Here is the call graph for this function:

Member Function Documentation

◆ _getGlobalLanguageFile()

static ilLanguageFile::_getGlobalLanguageFile ( string  $a_lang_key)
static

Read and get a global language file as a singleton object $a_lang_key language key.

Lines of modules maintained in PO files are not that module's source, see ilObjLanguageExt::getShippedValues().

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

428 : self
429 {
430 global $DIC;
431 $lng = $DIC->language();
432
433 if (!isset(self::$global_file_objects[$a_lang_key])) {
434 $file_object = new ilLanguageFile(
435 $lng->lang_path . "/ilias_" . $a_lang_key . ".lang",
436 $a_lang_key,
437 "global"
438 );
439 $file_object->read();
440
441 self::$global_file_objects[$a_lang_key] = $file_object;
442 }
443
444 return self::$global_file_objects[$a_lang_key];
445 }
Class ilLanguageFile.

References $DIC, and $lng.

Referenced by ilObjLanguageExt\findInvalidMarkupOfChangedValues(), ilObjLanguageExt\getGlobalLanguageFile(), and ShippedValuesOfMigratedModulesTest\testWithoutDirectoryManagerTheShippedValuesAreThePlainGlobalLanguageFile().

+ Here is the caller graph for this function:

◆ build()

ilLanguageFile::build ( string  $a_header = '')

Build and get the file content.

$a_header (optional) fixed header for the new file

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

225 : string
226 {
227 global $DIC;
228 $ilUser = $DIC->user();
229 $lng = $DIC->language();
230
231 if ($a_header) {
232 // take the given header
233 $content = $a_header;
234 } else {
235 // set default params
236 $lng->loadLanguageModule("meta");
237 $lang_name = $lng->txtlng("meta", "meta_l_" . $this->lang_key, "en");
238 $this->params["module"] = "language file " . $lang_name;
239 $this->params["created"] = gmdate("Y-m-d H:i:s");
240 $this->params["created_by"] = $ilUser->getFullname() . " <" . $ilUser->getEmail() . ">";
241
242 // build the header
243 $tpl = new ilTemplate("tpl.lang_file_header.html", true, true, "components/ILIAS/Language");
244 foreach ($this->getAllParams() as $name => $value) {
245 $tabs = intval(ceil((20 - 3 - strlen($name)) / 4));
246 $tabs = $tabs > 0 ? $tabs : 1;
247
248 $tpl->setCurrentBlock("param");
249 $tpl->setVariable("PAR_NAME", $name);
250 $tpl->setVariable("PAR_SPACE", str_repeat("\t", $tabs));
251 $tpl->setVariable("PAR_VALUE", $value);
253 }
254 $txt_scope = $lng->txtlng("administration", "language_scope_" . $this->scope, "en");
255 $tpl->setVariable("SCOPE", $txt_scope);
256
257 $content = $tpl->get();
258 }
259
260 // fault tolerant check for adding newline
261 $add_newline = (substr($content, strlen($content) - 1, 1) !== "\n");
262
263 // build the content
264 foreach ($this->values as $key => $value) {
265 // add the newline before the line!
266 // a valid lang file should not have a newline at the end!
267 if ($add_newline) {
268 $content .= "\n";
269 }
270 $add_newline = true;
271
272 if (isset($this->kept_keys[$key])) {
273 $content .= $this->read_lines[$key] ?? $this->buildLine(
274 (string) $key,
275 $this->read_values[$key],
276 $this->read_comments[$key] ?? null
277 );
278 continue;
279 }
280
281 $content .= $key . $this->separator . $value;
282
283 if (isset($this->comments[$key])) {
284 $content .= $this->comment_separator . $this->comments[$key];
285 }
286 }
287 return $content;
288 }
getAllParams()
Get array of all parameters Return array [name => value].
buildLine(string $key, string $value, ?string $comment)
special template class to simplify handling of ITX/PEAR
setVariable(string $variable, $value='')
Sets the given variable to the given value.
parseCurrentBlock(string $block_name=self::DEFAULT_BLOCK)
Parses the given block.
setCurrentBlock(string $part=self::DEFAULT_BLOCK)
Sets the template to the given block.
get(string $part=self::DEFAULT_BLOCK)
Renders the given block and returns the html string.
readonly ilGlobalTemplateInterface $tpl

References $DIC, $lng, ILIAS\News\Common\Table\$tpl, buildLine(), ILIAS\UICore\GlobalTemplate\get(), getAllParams(), ILIAS\UICore\GlobalTemplate\parseCurrentBlock(), ILIAS\UICore\GlobalTemplate\setCurrentBlock(), ILIAS\UICore\GlobalTemplate\setVariable(), and ILIAS\Scripts\PHPStan\str_repeat.

Referenced by ilLanguageFileTest\builtLines(), and write().

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

◆ buildLine()

ilLanguageFile::buildLine ( string  $key,
string  $value,
?string  $comment 
)
private

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

291 : string
292 {
293 return $key . $this->separator . $value
294 . ($comment !== null ? $this->comment_separator . $comment : '');
295 }
$comment
Definition: buildRTE.php:72

References $comment.

Referenced by build().

+ Here is the caller graph for this function:

◆ getAllComments()

ilLanguageFile::getAllComments ( )

Get array of all comments Return array [module.separator.identifier => comment].

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

337 : array
338 {
339 return $this->comments;
340 }

References $comments.

Referenced by ilObjLanguageExt\shippedValuesAndComments().

+ Here is the caller graph for this function:

◆ getAllParams()

ilLanguageFile::getAllParams ( )

Get array of all parameters Return array [name => value].

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

319 : array
320 {
321 return $this->params;
322 }

References $params.

Referenced by build().

+ Here is the caller graph for this function:

◆ getAllValues()

ilLanguageFile::getAllValues ( )

Get array of all values Return array [module.separator.identifier => value].

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

328 : array
329 {
330 return $this->values;
331 }

References $values.

Referenced by ilObjLanguageExt\getShippedChangesSinceBackup(), and ilObjLanguageExt\shippedValuesAndComments().

+ Here is the caller graph for this function:

◆ getComment()

ilLanguageFile::getComment ( string  $a_module,
string  $a_identifier 
)

Get a single comment $a_module module name $a_identifier indentifier.

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

366 : string
367 {
368 return $this->comments[$a_module . $this->separator . $a_identifier];
369 }

◆ getErrorMessage()

ilLanguageFile::getErrorMessage ( )

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

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

300 : string
301 {
303 }

References $error_message.

◆ getHeader()

ilLanguageFile::getHeader ( )

Get the header of the original file.

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

309 : string
310 {
311 return $this->header;
312 }

References $header.

◆ getParam()

ilLanguageFile::getParam ( string  $a_name)

Get a single parameter $a_name parameter name.

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

346 : string
347 {
348 return $this->params[$a_name] ?? '';
349 }

◆ getValue()

ilLanguageFile::getValue ( string  $a_module,
string  $a_identifier 
)

Get a single value $a_module module name $a_identifier indentifier.

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

356 : string
357 {
358 return $this->values[$a_module . $this->separator . $a_identifier];
359 }

◆ keepOriginalLines()

ilLanguageFile::keepOriginalLines ( array  $keys)

build()/write() emit the lines of $keys as read(), without whitespace normalisation, e.g.

to leave the lines of modules maintained in PO files untouched. Keys not read from the file are ignored.

Parameters
list<string>$keys module.separator.identifier

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

199 : void
200 {
201 foreach ($keys as $key) {
202 if (array_key_exists($key, $this->read_values)) {
203 $this->kept_keys[$key] = true;
204 }
205 }
206 }

◆ read()

ilLanguageFile::read ( )

Read a language file Return true, if reading successful.

Otherwise return false

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

99 : bool
100 {
101 global $DIC;
102 $lng = $DIC->language();
103
104 $this->header = '';
105 $this->params = array();
106 $this->values = array();
107 $this->comments = array();
108 $this->error_message = "";
109 $this->read_lines = [];
110 $this->kept_keys = [];
111
112 $content = file($this->lang_file);
113 $in_header = true;
114
115 foreach ($content as $line_num => $line) {
116 if ($in_header) {
117 // store the header line
118 $this->header .= $line . "\n";
119
120 // check header end
121 if (trim($line) === $this->file_start) {
122 $in_header = false;
123 } else {
124 // get header params
125 $pos_par = strpos($line, "* @");
126
127 if (strpos($line, "* @") !== false) {
128 $pos_par += 3;
129 $pos_space = strpos($line, " ", $pos_par);
130 $pos_tab = strpos($line, "\t", $pos_par);
131 if ($pos_space !== false && $pos_tab !== false) {
132 $pos_white = min($pos_space, $pos_tab);
133 } elseif ($pos_space !== false) {
134 $pos_white = $pos_space;
135 } elseif ($pos_tab !== false) {
136 $pos_white = $pos_tab;
137 } else {
138 $pos_white = false;
139 }
140 if ($pos_white) {
141 $param = substr($line, $pos_par, $pos_white - $pos_par);
142 $value = trim(substr($line, $pos_white));
143
144 $this->params[$param] = $value;
145 }
146 }
147 }
148 } else {
149 // separate the lang file entry
150 $separated = explode($this->separator, trim($line));
151
152 // not a valid line with module, identifier and value?
153 if (count($separated) !== 3) {
154 $this->error_message =
155 $lng->txt("file_not_valid") . " "
156 . $lng->txt("err_in_line") . " " . $line_num . ". "
157 . $lng->txt("err_count_param");
158 return false;
159 } else {
160 $key = $separated[0] . $this->separator . $separated[1];
161 $value = $separated[2];
162
163 // cut off comment
164 $pos = strpos($value, $this->comment_separator);
165 if ($pos !== false) {
166 $this->comments[$key]
167 = substr($value, $pos + strlen($this->comment_separator));
168
169 $value = substr($value, 0, $pos);
170 }
171 $this->values[$key] = $value;
172
173 $line_as_read = rtrim($line, "\r\n");
174 if ($line_as_read !== trim($line)) {
175 $this->read_lines[$key] = $line_as_read;
176 } else {
177 unset($this->read_lines[$key]);
178 }
179 }
180 }
181 }
182 $this->read_values = $this->values;
183 $this->read_comments = $this->comments;
184 // still in header after parsing the whole file?
185 if ($in_header) {
186 $this->error_message = $lng->txt("file_not_valid") . " " . $lng->txt("err_wrong_header");
187 return false;
188 }
189
190 return true;
191 }
$param
Definition: xapitoken.php:44

References $comments, $DIC, $lng, $param, $values, and ILIAS\UI\examples\Symbol\Glyph\Header\header().

+ Here is the call graph for this function:

◆ setAllComments()

ilLanguageFile::setAllComments ( array  $a_comments)

Set all comments $a_comments [module.separator.identifier => comment].

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

405 : void
406 {
407 $this->comments = $a_comments;
408 }

◆ setAllValues()

ilLanguageFile::setAllValues ( array  $a_values)

Set all values $a_values [module.separator.identifier => value].

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

396 : void
397 {
398 $this->values = $a_values;
399 }

◆ setComment()

ilLanguageFile::setComment ( string  $a_module,
string  $a_identifier,
string  $a_comment 
)

Set a single comment $a_module module name $a_identifier indentifier $a_comment comment.

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

417 : string
418 {
419 return $this->comments[$a_module . $this->separator . $a_identifier] = $a_comment;
420 }

◆ setParam()

ilLanguageFile::setParam ( string  $a_name,
string  $a_value 
)

Set a parameter $a_name parameter name $a_value parameter value.

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

376 : void
377 {
378 $this->params[$a_name] = $a_value;
379 }

◆ setValue()

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

Set a single value $a_module module name $a_identifier indentifier $a_value value.

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

387 : void
388 {
389 $this->values[$a_module . $this->separator . $a_identifier] = $a_value;
390 }

◆ write()

ilLanguageFile::write ( string  $a_header = "")

Write a language file.

$a_header (optional) fixed header for the new file

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

213 : void
214 {
215 $fp = fopen($this->lang_file, 'wb');
216 fwrite($fp, $this->build($a_header));
217 fclose($fp);
218 }
build(string $a_header='')
Build and get the file content.

References build().

+ Here is the call graph for this function:

Field Documentation

◆ $comment_separator

string ilLanguageFile::$comment_separator
private

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

◆ $comments

array ilLanguageFile::$comments
private

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

Referenced by getAllComments(), and read().

◆ $error_message

string ilLanguageFile::$error_message = ""
private

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

Referenced by getErrorMessage().

◆ $file_start

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

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

Referenced by __construct().

◆ $global_file_objects

array ilLanguageFile::$global_file_objects
staticprivate

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

◆ $header

string ilLanguageFile::$header
private

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

Referenced by getHeader().

◆ $kept_keys

array ilLanguageFile::$kept_keys = []
private

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

◆ $lang_file

string ilLanguageFile::$lang_file
private

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

◆ $lang_key

string ilLanguageFile::$lang_key
private

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

◆ $params

array ilLanguageFile::$params
private

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

Referenced by getAllParams().

◆ $read_comments

array ilLanguageFile::$read_comments = []
private

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

◆ $read_lines

array ilLanguageFile::$read_lines = []
private

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

◆ $read_values

array ilLanguageFile::$read_values = []
private

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

◆ $scope

string ilLanguageFile::$scope
private

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

◆ $separator

string ilLanguageFile::$separator
private

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

◆ $values

array ilLanguageFile::$values
private

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

Referenced by getAllValues(), and read().


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