ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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...
 
 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 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 = ""
 

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

54 {
55 global $DIC;
56 $lng = $DIC->language();
57
58 $this->separator = $lng->separator;
59 $this->comment_separator = $lng->comment_separator;
60
61 $this->lang_file = $a_file;
62 $this->lang_key = $a_key;
63 $this->scope = $a_scope;
64
65 // initialize the header of a blank file
67
68 // Set the default parameters to be written in a new file.
69 // This ensures the correct order of parameters
70
71 $this->params["module"] = "language file";
72 $this->params["modulegroup"] = "language";
73
74 if ($this->scope === "local") {
75 $this->params["based_on"] = "";
76 } else {
77 $this->params["author"] = "";
78 $this->params["version"] = "";
79 }
80
81 $this->params["il_server"] = ILIAS_HTTP_PATH;
82 $this->params["il_version"] = ILIAS_VERSION;
83 $this->params["created"] = "";
84 $this->params["created_by"] = "";
85 }
const ILIAS_VERSION
global $lng
Definition: privfeed.php:31
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.

Returns
object language file object (with contents)

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

379 {
380 global $DIC;
381 $lng = $DIC->language();
382
383 if (!isset(self::$global_file_objects[$a_lang_key])) {
384 $file_object = new ilLanguageFile(
385 $lng->lang_path . "/ilias_" . $a_lang_key . ".lang",
386 $a_lang_key,
387 "global"
388 );
389 $file_object->read();
390
391 self::$global_file_objects[$a_lang_key] = $file_object;
392 }
393
394 return self::$global_file_objects[$a_lang_key];
395 }
Class ilLanguageFile.

References $DIC, and $lng.

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

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

191 : string
192 {
193 global $DIC;
194 $ilUser = $DIC->user();
195 $lng = $DIC->language();
196
197 if ($a_header) {
198 // take the given header
199 $content = $a_header;
200 } else {
201 // set default params
202 $lng->loadLanguageModule("meta");
203 $lang_name = $lng->txtlng("meta", "meta_l_" . $this->lang_key, "en");
204 $this->params["module"] = "language file " . $lang_name;
205 $this->params["created"] = gmdate("Y-m-d H:i:s");
206 $this->params["created_by"] = $ilUser->getFullname() . " <" . $ilUser->getEmail() . ">";
207
208 // build the header
209 $tpl = new ilTemplate("tpl.lang_file_header.html", true, true, "components/ILIAS/Language");
210 foreach ($this->getAllParams() as $name => $value) {
211 $tabs = intval(ceil((20 - 3 - strlen($name)) / 4));
212 $tabs = $tabs > 0 ? $tabs : 1;
213
214 $tpl->setCurrentBlock("param");
215 $tpl->setVariable("PAR_NAME", $name);
216 $tpl->setVariable("PAR_SPACE", str_repeat("\t", $tabs));
217 $tpl->setVariable("PAR_VALUE", $value);
218 $tpl->parseCurrentBlock();
219 }
220 $txt_scope = $lng->txtlng("administration", "language_scope_" . $this->scope, "en");
221 $tpl->setVariable("SCOPE", $txt_scope);
222
223 $content = $tpl->get();
224 }
225
226 // fault tolerant check for adding newline
227 $add_newline = (substr($content, strlen($content) - 1, 1) !== "\n");
228
229 // build the content
230 foreach ($this->values as $key => $value) {
231 // add the newline before the line!
232 // a valid lang file should not have a newline at the end!
233 if ($add_newline) {
234 $content .= "\n";
235 }
236 $add_newline = true;
237
238 $content .= $key . $this->separator . $value;
239
240 if (isset($this->comments[$key])) {
241 $content .= $this->comment_separator . $this->comments[$key];
242 }
243 }
244 return $content;
245 }
getAllParams()
Get array of all parameters Return array [name => value].
special template class to simplify handling of ITX/PEAR

References $DIC, $lng, 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 Return array [module.separator.identifier => comment].

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

288 : array
289 {
290 return $this->comments;
291 }

References $comments.

◆ getAllParams()

ilLanguageFile::getAllParams ( )

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

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

270 : array
271 {
272 return $this->params;
273 }

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

279 : array
280 {
281 return $this->values;
282 }

References $values.

◆ getComment()

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

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

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

317 : string
318 {
319 return $this->comments[$a_module . $this->separator . $a_identifier];
320 }

◆ getErrorMessage()

ilLanguageFile::getErrorMessage ( )

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

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

251 : string
252 {
254 }

References $error_message.

◆ getHeader()

ilLanguageFile::getHeader ( )

Get the header of the original file.

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

260 : string
261 {
262 return $this->header;
263 }

References $header.

◆ getParam()

ilLanguageFile::getParam ( string  $a_name)

Get a single parameter $a_name parameter name.

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

297 : string
298 {
299 return $this->params[$a_name] ?? '';
300 }

◆ getValue()

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

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

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

307 : string
308 {
309 return $this->values[$a_module . $this->separator . $a_identifier];
310 }

◆ read()

ilLanguageFile::read ( )

Read a language file Return true, if reading successful.

Otherwise return false

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

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

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

356 : void
357 {
358 $this->comments = $a_comments;
359 }

◆ setAllValues()

ilLanguageFile::setAllValues ( array  $a_values)

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

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

347 : void
348 {
349 $this->values = $a_values;
350 }

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

368 : string
369 {
370 return $this->comments[$a_module . $this->separator . $a_identifier] = $a_comment;
371 }

◆ setParam()

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

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

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

327 : void
328 {
329 $this->params[$a_name] = $a_value;
330 }

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

338 : void
339 {
340 $this->values[$a_module . $this->separator . $a_identifier] = $a_value;
341 }

◆ write()

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

Write a language file.

$a_header (optional) fixed header for the new file

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

179 : void
180 {
181 $fp = fopen($this->lang_file, 'wb');
182 fwrite($fp, $this->build($a_header));
183 fclose($fp);
184 }
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().

◆ $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().

◆ $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().

◆ $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().


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