[FEATURE] Add Parser for PHP locallang array
[Packages/TYPO3.CMS.git] / typo3 / sysext / lang / lang.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Contains the TYPO3 Backend Language class
29 *
30 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
31 */
32 /**
33 * [CLASS/FUNCTION INDEX of SCRIPT]
34 *
35 *
36 *
37 * 88: class language
38 * 138: function init($lang,$altPath='')
39 * 183: function addModuleLabels($arr,$prefix)
40 * 209: function hscAndCharConv($lStr,$hsc)
41 * 224: function makeEntities($str)
42 * 241: function JScharCode($str)
43 * 260: function getLL($index,$hsc=0)
44 * 278: function getLLL($index,$LOCAL_LANG,$hsc=0)
45 * 299: function sL($input,$hsc=0)
46 * 344: function loadSingleTableDescription($table)
47 * 396: function includeLLFile($fileRef,$setGlobal=1,$mergeLocalOntoDefault=0)
48 * 441: function readLLfile($fileRef)
49 * 451: function localizedFileRef($fileRef)
50 *
51 * TOTAL FUNCTIONS: 12
52 * (This index is automatically created/updated by the extension "extdeveval")
53 *
54 */
55
56 /**
57 * Contains the TYPO3 Backend Language class
58 *
59 * For detailed information about how localization is handled,
60 * please refer to the 'Inside TYPO3' document which descibes this.
61 *
62 * This class is normally instantiated as the global variable $LANG in typo3/template.php
63 * It's only available in the backend and under certain circumstances in the frontend
64 *
65 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
66 * @package TYPO3
67 * @subpackage core
68 * @see typo3/template.php, template
69 */
70 class language {
71
72 /**
73 * This is set to the language that is currently running for the user
74 *
75 * @var string
76 */
77 public $lang = 'default';
78
79 /**
80 * Values like the labels in the tables.php-document are split by '|'.
81 * This values defines which language is represented by which position
82 * in the resulting array after splitting a value. (NOTICE: Obsolete concept!)
83 *
84 * @var string
85 */
86 public $langSplit = 'default';
87
88 /**
89 * Default charset in backend
90 *
91 * @var string
92 */
93 public $charSet = 'utf-8';
94
95 /**
96 * Array with alternative charsets for other languages.
97 * Moved to t3lib_cs, set internally from csConvObj!
98 *
99 * @var array
100 */
101 public $charSetArray = array();
102
103 /**
104 * This is the url to the TYPO3 manual
105 *
106 * @var string
107 */
108 public $typo3_help_url= 'http://typo3.org/documentation/document-library/';
109
110 /**
111 * If TRUE, will show the key/location of labels in the backend.
112 * @var bool
113 */
114 public $debugKey = FALSE;
115
116 /**
117 * Can contain labels and image references from the backend modules.
118 * Relies on t3lib_loadmodules to initialize modules after a global instance of $LANG has been created.
119 *
120 * @var array
121 */
122 public $moduleLabels = array();
123
124 /**
125 * Internal, Points to the position of the current language key as found in constant TYPO3_languages
126 * @var int
127 */
128 public $langSplitIndex = 0;
129
130 /**
131 * Internal cache for read LL-files
132 *
133 * @var array
134 */
135 public $LL_files_cache = array();
136
137 /**
138 * Internal cache for ll-labels (filled as labels are requested)
139 *
140 * @var array
141 */
142 public $LL_labels_cache = array();
143
144 /**
145 * instance of the "t3lib_cs" class. May be used by any application.
146 *
147 * @var t3lib_cs
148 */
149 public $csConvObj;
150
151 /**
152 * instance of the parser factory
153 * @var tx_lang_Factory
154 */
155 public $parserFactory;
156
157 /**
158 * Initializes the backend language.
159 * This is for example done in typo3/template.php with lines like these:
160 *
161 * require (PATH_typo3 . 'sysext/lang/lang.php');
162 * $LANG = t3lib_div::makeInstance('language');
163 * $LANG->init($GLOBALS['BE_USER']->uc['lang']);
164 *
165 * @throws RuntimeException
166 * @param string $lang The language key (two character string from backend users profile)
167 * @return void
168 */
169 public function init($lang) {
170
171 // Initialize the conversion object:
172 $this->csConvObj = t3lib_div::makeInstance('t3lib_cs');
173 $this->charSetArray = $this->csConvObj->charSetArray;
174
175 // Initialize the parser factory object
176 $this->parserFactory = t3lib_div::makeInstance('tx_lang_Factory');
177
178 // Internally setting the list of TYPO3 backend languages.
179 $this->langSplit = TYPO3_languages;
180
181 // Finding the requested language in this list based
182 // on the $lang key being inputted to this function.
183 $ls = explode('|', $this->langSplit);
184
185 foreach ($ls as $i => $v) {
186 // Language is found. Configure it:
187 if ($v == $lang) {
188 // The index of the language as found in the TYPO3_languages list
189 $this->langSplitIndex = $i;
190 // The current language key
191 $this->lang = $lang;
192 if ($this->charSetArray[$this->lang]) {
193 // The charset if different from the default.
194 $this->charSet = $this->charSetArray[$this->lang];
195 }
196 }
197 }
198
199 if ($GLOBALS['TYPO3_CONF_VARS']['BE']['lang']['debug']) {
200 $this->debugKey = TRUE;
201 }
202
203 // If a forced charset is used and different from the charset otherwise used:
204 if ($GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'] && $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'] != $this->charSet) {
205 // Set the forced charset:
206 $this->charSet = $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'];
207
208 if ($this->charSet != 'utf-8' && !$this->csConvObj->initCharset($this->charSet)) {
209 throw new RuntimeException('Forced charset not found: The forced character set "'. $this->charSet . '" was not found in t3lib/csconvtbl/', 1294587487);
210 }
211 }
212 }
213
214 /**
215 * Gets the parser factory.
216 *
217 * @return tx_lang_Factory
218 */
219 public function getParserFactory() {
220 return $this->parserFactory;
221 }
222
223 /**
224 * Adds labels and image references from the backend modules to the internal moduleLabels array
225 *
226 * @param array $arr Array with references to module labels, keys: ['labels']['tablabel'],
227 * ['labels']['tabdescr'], ['tabs']['tab']
228 * @param string $prefix Module name prefix
229 * @return void
230 * @see t3lib_loadModules
231 */
232 public function addModuleLabels($arr, $prefix) {
233 if (is_array($arr)) {
234 foreach ($arr as $k => $larr) {
235 if (!isset($this->moduleLabels[$k])) {
236 $this->moduleLabels[$k] = array();
237 }
238 if (is_array($larr)) {
239 foreach ($larr as $l => $v) {
240 $this->moduleLabels[$k][$prefix . $l] = $v;
241 }
242 }
243 }
244 }
245 }
246
247 /**
248 * Will htmlspecialchar() the input string and before that any charset conversion
249 * will also have taken place if needed (see init())
250 * Used to pipe language labels through just before they are returned.
251 *
252 * @param string $lStr The string to process
253 * @param boolean $hsc If set, then the string is htmlspecialchars()'ed
254 * @return string The processed string
255 * @see init()
256 * @access public
257 */
258 public function hscAndCharConv($lStr, $hsc) {
259 // labels returned from a locallang file used to be in the language of the charset.
260 // Since TYPO3 4.1 they are always in the charset of the BE.
261 if ($hsc) {
262 return htmlspecialchars($lStr);
263 } else {
264 return $lStr;
265 }
266 }
267
268 /**
269 * Will convert the input strings special chars (all above 127) to entities.
270 * The string is expected to be encoded in the charset, $this->charSet
271 * This function is used to create strings that can be used in the Click Menu
272 * (Context Sensitive Menus). The reason is that the values that are dynamically
273 * written into the <div> layer is decoded as iso-8859-1 no matter what charset
274 * is used in the document otherwise (only MSIE, Mozilla is OK).
275 * So by converting we by-pass this problem.
276 *
277 * @param string $str Input string
278 * @return string Output string
279 * @access public
280 */
281 public function makeEntities($str) {
282 // Convert string to UTF-8:
283 if ($this->charSet != 'utf-8') {
284 $str = $this->csConvObj->utf8_encode($str, $this->charSet);
285 }
286
287 // Convert string back again, but using the full entity conversion:
288 return $this->csConvObj->utf8_to_entities($str);
289 }
290
291 /**
292 * Converts the input string to a JavaScript function returning the same string, but charset-safe.
293 * Used for confirm and alert boxes where we must make sure that any string content
294 * does not break the script AND want to make sure the charset is preserved.
295 * Originally I used the JS function unescape() in combination with PHP function
296 * rawurlencode() in order to pass strings in a safe way. This could still be done
297 * for iso-8859-1 charsets but now I have applied the same method here for all charsets.
298 *
299 * @param string $str Input string, encoded with $this->charSet
300 * @return string Output string, a JavaScript function: "String.fromCharCode(......)"
301 * @access public
302 */
303 public function JScharCode($str) {
304
305 // Convert string to UTF-8:
306 if ($this->charSet != 'utf-8') {
307 $str = $this->csConvObj->utf8_encode($str, $this->charSet);
308 }
309
310 // Convert the UTF-8 string into a array of char numbers:
311 $nArr = $this->csConvObj->utf8_to_numberarray($str);
312
313 return 'String.fromCharCode(' . implode(',', $nArr) . ')';
314 }
315
316 /**
317 * Debugs localization key.
318 *
319 * @param $value Value to debug
320 * @return string
321 */
322 public function debugLL($value) {
323 return ($this->debugKey ? '[' . $value . ']' : '');
324 }
325
326 /**
327 * Returns the label with key $index form the globally loaded $LOCAL_LANG array.
328 * Mostly used from modules with only one LOCAL_LANG file loaded into the global space.
329 *
330 * @param string $index Label key
331 * @param boolean $hsc If set, the return value is htmlspecialchar'ed
332 * @return string
333 * @access public
334 */
335 public function getLL($index, $hsc = FALSE) {
336 // Get Local Language
337 if ($GLOBALS['LOCAL_LANG'][$this->lang][$index][0]['target'] !== '') {
338 $output = $this->hscAndCharConv($GLOBALS['LOCAL_LANG'][$this->lang][$index][0]['target'], $hsc);
339 } else {
340 $output = $this->hscAndCharConv($GLOBALS['LOCAL_LANG']['default'][$index][0]['target'], $hsc);
341 }
342 return $output . $this->debugLL($index);
343 }
344
345 /**
346 * Works like ->getLL() but takes the $LOCAL_LANG array
347 * used as the second argument instead of using the global array.
348 *
349 * @param string $index Label key
350 * @param array $localLanguage $LOCAL_LANG array to get label key from
351 * @param boolean $hsc If set, the return value is htmlspecialchar'ed
352 * @return string
353 * @access public
354 */
355 public function getLLL($index, $localLanguage, $hsc = FALSE) {
356 // Get Local Language. Special handling for all extensions that
357 // read PHP LL files and pass arrays here directly.
358 $value = is_string($localLanguage[$this->lang][$index]) ? $localLanguage[$this->lang][$index] : $localLanguage[$this->lang][$index][0]['target'];
359 if ($value !== '') {
360 $output = $this->hscAndCharConv($value, $hsc);
361 } else {
362 $output = $this->hscAndCharConv($value, $hsc);
363 }
364 return $output . $this->debugLL($index);
365 }
366
367 /**
368 * splitLabel function
369 * Historically labels were exploded by '|' and each part would correspond
370 * to the translation of the language found at the same 'index' in the TYPO3_languages constant.
371 * Today all translations are based on $LOCAL_LANG variables.
372 * 'language-splitted' labels can therefore refer to a local-lang file + index instead!
373 * It's highly recommended to use the 'local_lang' method
374 * (and thereby it's highly deprecated to use 'language-splitted' label strings)
375 * Refer to 'Inside TYPO3' for more details
376 *
377 * @param string $input Label key/reference
378 * @param boolean $hsc If set, the return value is htmlspecialchar'ed
379 * @return string
380 * @access public
381 */
382 public function sL($input, $hsc = FALSE) {
383 // Using obsolete 'language-splitted' labels:
384 if (strcmp(substr($input, 0, 4), 'LLL:')) {
385 $t = explode('|', $input);
386 $out = $t[$this->langSplitIndex] ? $t[$this->langSplitIndex] : $t[0];
387 return $this->hscAndCharConv($out, $hsc);
388 // LOCAL_LANG:
389 } else {
390 // If cached label
391 if (!isset($this->LL_labels_cache[$this->lang][$input])) {
392 $restStr = trim(substr($input, 4));
393 $extPrfx = '';
394
395 // ll-file refered to is found in an extension.
396 if (!strcmp(substr($restStr, 0, 4), 'EXT:')) {
397 $restStr = trim(substr($restStr, 4));
398 $extPrfx = 'EXT:';
399 }
400 $parts = explode(':', $restStr);
401 $parts[0] = $extPrfx . $parts[0];
402
403 // Getting data if not cached
404 if (!isset($this->LL_files_cache[$parts[0]])) {
405 $this->LL_files_cache[$parts[0]] = $this->readLLfile($parts[0]);
406
407 // If the current language is found in another file, load that as well:
408 $lFileRef = $this->localizedFileRef($parts[0]);
409 if ($lFileRef && is_string($this->LL_files_cache[$parts[0]][$this->lang])
410 && $this->LL_files_cache[$parts[0]][$this->lang] == 'EXT') {
411 $tempLL = $this->readLLfile($lFileRef);
412 $this->LL_files_cache[$parts[0]][$this->lang] = $tempLL[$this->lang];
413 }
414
415 // Overriding file?
416 // @deprecated since TYPO3 4.3, remove in TYPO3 4.5, please use the generic method in
417 // t3lib_div::readLLfile and the global array $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride']
418 if (isset($GLOBALS['TYPO3_CONF_VARS']['BE']['XLLfile'][$parts[0]])) {
419 t3lib_div::deprecationLog('Usage of $TYPO3_CONF_VARS[\'BE\'][\'XLLfile\'] is deprecated since TYPO3 4.3. Use $TYPO3_CONF_VARS[\'SYS\'][\'locallangXMLOverride\'][] to include the file ' . $fileRef . ' instead.');
420 $ORarray = $this->readLLfile($GLOBALS['TYPO3_CONF_VARS']['BE']['XLLfile'][$parts[0]]);
421 $this->LL_files_cache[$parts[0]] = t3lib_div::array_merge_recursive_overrule($this->LL_files_cache[$parts[0]], $ORarray);
422 }
423 }
424 $this->LL_labels_cache[$this->lang][$input] = $this->getLLL($parts[1], $this->LL_files_cache[$parts[0]]);
425 }
426 // For the cached output charset conversion has already happened!
427 // So perform HSC right here.
428 $output = $this->LL_labels_cache[$this->lang][$input];
429 if ($hsc) {
430 $output = t3lib_div::deHSCentities(htmlspecialchars($output));
431 }
432 return $output . $this->debugLL($input);
433 }
434 }
435
436 /**
437 * Loading $TCA_DESCR[$table]['columns'] with content from locallang files
438 * as defined in $TCA_DESCR[$table]['refs']
439 * $TCA_DESCR is a global var
440 *
441 * @param string $table Table name found as key in global array $TCA_DESCR
442 * @return void
443 * @access public
444 */
445 public function loadSingleTableDescription($table) {
446
447 // First the 'table' cannot already be loaded in [columns]
448 // and secondly there must be a references to locallang files available in [refs]
449 if (is_array($GLOBALS['TCA_DESCR'][$table])
450 && !isset($GLOBALS['TCA_DESCR'][$table]['columns'])
451 && is_array($GLOBALS['TCA_DESCR'][$table]['refs'])) {
452
453 // Init $TCA_DESCR for $table-key
454 $GLOBALS['TCA_DESCR'][$table]['columns'] = array();
455
456 // Get local-lang for each file in $TCA_DESCR[$table]['refs'] as they are ordered.
457 foreach ($GLOBALS['TCA_DESCR'][$table]['refs'] as $llfile) {
458 $localLanguage = $this->includeLLFile($llfile, 0, 1);
459
460 // Traverse all keys
461 if (is_array($localLanguage['default'])) {
462 foreach ($localLanguage['default'] as $lkey => $lVal) {
463 $type = '';
464 $fieldName = '';
465
466 // Exploding by '.':
467 // 0-n => fieldname,
468 // n+1 => type from (alttitle, description, details, syntax, image_descr,image,seeAlso),
469 // n+2 => special instruction, if any
470 $keyParts = explode('.', $lkey);
471 $keyPartsCount = count($keyParts);
472 // Check if last part is special instruction
473 // Only "+" is currently supported
474 $specialInstruction = ($keyParts[$keyPartsCount - 1] == '+') ? TRUE : FALSE;
475 if ($specialInstruction) {
476 array_pop($keyParts);
477 }
478
479 // If there are more than 2 parts, get the type from the last part
480 // and merge back the other parts with a dot (.)
481 // Otherwise just get type and field name straightaway
482 if ($keyPartsCount > 2) {
483 $type = array_pop($keyParts);
484 $fieldName = implode('.', $keyParts);
485 } else {
486 $fieldName = $keyParts[0];
487 $type = $keyParts[1];
488 }
489
490 // Detecting 'hidden' labels, converting to normal fieldname
491 if ($fieldName == '_') {
492 $fieldName = '';
493 }
494 if (substr($fieldName, 0, 1) == '_') {
495 $fieldName = substr($fieldName, 1);
496 }
497
498 // Append label
499 if ($specialInstruction) {
500 $GLOBALS['TCA_DESCR'][$table]['columns'][$fieldName][$type] .= LF . $lVal[0]['source'];
501 } else {
502 // Substitute label
503 $GLOBALS['TCA_DESCR'][$table]['columns'][$fieldName][$type] = $lVal[0]['source'];
504 }
505 }
506 }
507 }
508 }
509 }
510
511 /**
512 * Includes locallang file (and possibly additional localized version if configured for)
513 * Read language labels will be merged with $LOCAL_LANG (if $setGlobal = TRUE).
514 *
515 * @param string $fileRef $fileRef is a file-reference (see t3lib_div::getFileAbsFileName)
516 * @param boolean $setGlobal Setting in global variable $LOCAL_LANG (or returning the variable)
517 * @param boolean $mergeLocalOntoDefault If $mergeLocalOntoDefault is set the local part of the $LOCAL_LANG array is merged onto the default part (if the local part exists) and the local part is unset.
518 * @return mixed If $setGlobal is TRUE the LL-files will set the $LOCAL_LANG in the global scope. Otherwise the $LOCAL_LANG array is returned from function
519 * @access public
520 */
521 public function includeLLFile($fileRef, $setGlobal = TRUE, $mergeLocalOntoDefault = FALSE) {
522
523 $globalLanguage = array();
524
525 // Get default file
526 $localLanguage = $this->readLLfile($fileRef);
527
528 if (is_array($localLanguage) && count($localLanguage)) {
529
530 // it depends on, whether we should return the result or set it in the global $LOCAL_LANG array
531 if ($setGlobal) {
532 $globalLanguage = t3lib_div::array_merge_recursive_overrule((array)$GLOBALS['LOCAL_LANG'], $localLanguage);
533 } else {
534 $globalLanguage = $localLanguage;
535 }
536
537 // Localized addition?
538 $lFileRef = $this->localizedFileRef($fileRef);
539 if ($lFileRef && (string)$globalLanguage[$this->lang] == 'EXT') {
540 $localLanguage = $this->readLLfile($lFileRef);
541 $globalLanguage = t3lib_div::array_merge_recursive_overrule($globalLanguage, $localLanguage);
542 }
543
544 // Merge local onto default
545 if ($mergeLocalOntoDefault && $this->lang !== 'default'
546 && is_array($globalLanguage[$this->lang]) && is_array($globalLanguage['default'])) {
547 // array_merge can be used so far the keys are not
548 // numeric - which we assume they are not...
549 $globalLanguage['default'] = array_merge($globalLanguage['default'], $globalLanguage[$this->lang]);
550 unset($globalLanguage[$this->lang]);
551 }
552 }
553
554 // Return value if not global is set.
555 if (!$setGlobal) {
556 return $globalLanguage;
557 } else {
558 $GLOBALS['LOCAL_LANG'] = $globalLanguage;
559 }
560 }
561
562 /**
563 * Includes a locallang file and returns the $LOCAL_LANG array found inside.
564 *
565 * @param string $fileRef Input is a file-reference (see t3lib_div::getFileAbsFileName) which, if exists, is included. That file is expected to be a 'local_lang' file containing a $LOCAL_LANG array.
566 * @return array Value of $LOCAL_LANG found in the included file. If that array is found it's returned. Otherwise an empty array
567 * @access private
568 */
569 protected function readLLfile($fileRef) {
570 return t3lib_div::readLLfile($fileRef, $this->lang, $this->charSet);
571 }
572
573 /**
574 * Returns localized fileRef (.[langkey].php)
575 *
576 * @param string $fileRef Filename/path of a 'locallang.php' file
577 * @return string Input filename with a '.[lang-key].php' ending added if $this->lang is not 'default'
578 * @access private
579 */
580 protected function localizedFileRef($fileRef) {
581 if ($this->lang != 'default' && substr($fileRef, -4) == '.php') {
582 return substr($fileRef, 0, -4) . '.' . $this->lang . '.php';
583 }
584 }
585
586 /**
587 * Overrides a label.
588 *
589 * @param string $index
590 * @param string $value
591 * @param boolean $overrideDefault Overrides default language
592 * @return void
593 */
594 public function overrideLL($index, $value, $overrideDefault = TRUE) {
595 if (isset($GLOBALS['LOCAL_LANG']) === FALSE) {
596 $GLOBALS['LOCAL_LANG'] = array();
597 }
598
599 $GLOBALS['LOCAL_LANG'][$this->lang][$index][0]['target'] = $value;
600
601 if ($overrideDefault) {
602 $GLOBALS['LOCAL_LANG']['default'][$index][0]['target'] = $value;
603 }
604 }
605 }
606
607
608 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/lang/lang.php'])) {
609 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/lang/lang.php']);
610 }
611
612 ?>