[TASK] lang: hscAndCharConv is not needed anymore.
[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 * @deprecated since TYPO3 4.6, will be removed in TYPO3 4.8 - Use htmlspecialchars() instead.
256 * @see init()
257 * @access public
258 */
259 public function hscAndCharConv($lStr, $hsc) {
260 t3lib_div::logDeprecatedFunction();
261 // labels returned from a locallang file used to be in the language of the charset.
262 // Since TYPO3 4.1 they are always in the charset of the BE.
263 if ($hsc) {
264 return htmlspecialchars($lStr);
265 } else {
266 return $lStr;
267 }
268 }
269
270 /**
271 * Will convert the input strings special chars (all above 127) to entities.
272 * The string is expected to be encoded in the charset, $this->charSet
273 * This function is used to create strings that can be used in the Click Menu
274 * (Context Sensitive Menus). The reason is that the values that are dynamically
275 * written into the <div> layer is decoded as iso-8859-1 no matter what charset
276 * is used in the document otherwise (only MSIE, Mozilla is OK).
277 * So by converting we by-pass this problem.
278 *
279 * @param string $str Input string
280 * @return string Output string
281 * @access public
282 */
283 public function makeEntities($str) {
284 // Convert string to UTF-8:
285 if ($this->charSet != 'utf-8') {
286 $str = $this->csConvObj->utf8_encode($str, $this->charSet);
287 }
288
289 // Convert string back again, but using the full entity conversion:
290 return $this->csConvObj->utf8_to_entities($str);
291 }
292
293 /**
294 * Converts the input string to a JavaScript function returning the same string, but charset-safe.
295 * Used for confirm and alert boxes where we must make sure that any string content
296 * does not break the script AND want to make sure the charset is preserved.
297 * Originally I used the JS function unescape() in combination with PHP function
298 * rawurlencode() in order to pass strings in a safe way. This could still be done
299 * for iso-8859-1 charsets but now I have applied the same method here for all charsets.
300 *
301 * @param string $str Input string, encoded with $this->charSet
302 * @return string Output string, a JavaScript function: "String.fromCharCode(......)"
303 * @access public
304 */
305 public function JScharCode($str) {
306
307 // Convert string to UTF-8:
308 if ($this->charSet != 'utf-8') {
309 $str = $this->csConvObj->utf8_encode($str, $this->charSet);
310 }
311
312 // Convert the UTF-8 string into a array of char numbers:
313 $nArr = $this->csConvObj->utf8_to_numberarray($str);
314
315 return 'String.fromCharCode(' . implode(',', $nArr) . ')';
316 }
317
318 /**
319 * Debugs localization key.
320 *
321 * @param $value Value to debug
322 * @return string
323 */
324 public function debugLL($value) {
325 return ($this->debugKey ? '[' . $value . ']' : '');
326 }
327
328 /**
329 * Returns the label with key $index form the globally loaded $LOCAL_LANG array.
330 * Mostly used from modules with only one LOCAL_LANG file loaded into the global space.
331 *
332 * @param string $index Label key
333 * @param boolean $hsc If set, the return value is htmlspecialchar'ed
334 * @return string
335 * @access public
336 */
337 public function getLL($index, $hsc = FALSE) {
338 // Get Local Language
339 if ($GLOBALS['LOCAL_LANG'][$this->lang][$index][0]['target'] !== '') {
340 $output = $GLOBALS['LOCAL_LANG'][$this->lang][$index][0]['target'];
341 } else {
342 $output = $GLOBALS['LOCAL_LANG']['default'][$index][0]['target'];
343 }
344 if ($hsc) {
345 $output = htmlspecialchars($output);
346 }
347 return $output . $this->debugLL($index);
348 }
349
350 /**
351 * Works like ->getLL() but takes the $LOCAL_LANG array
352 * used as the second argument instead of using the global array.
353 *
354 * @param string $index Label key
355 * @param array $localLanguage $LOCAL_LANG array to get label key from
356 * @param boolean $hsc If set, the return value is htmlspecialchar'ed
357 * @return string
358 * @access public
359 */
360 public function getLLL($index, $localLanguage, $hsc = FALSE) {
361 // Get Local Language. Special handling for all extensions that
362 // read PHP LL files and pass arrays here directly.
363 $value = is_string($localLanguage[$this->lang][$index]) ? $localLanguage[$this->lang][$index] : $localLanguage[$this->lang][$index][0]['target'];
364 if ($hsc) {
365 $value = htmlspecialchars($value);
366 }
367 return $value . $this->debugLL($index);
368 }
369
370 /**
371 * splitLabel function
372 * Historically labels were exploded by '|' and each part would correspond
373 * to the translation of the language found at the same 'index' in the TYPO3_languages constant.
374 * Today all translations are based on $LOCAL_LANG variables.
375 * 'language-splitted' labels can therefore refer to a local-lang file + index instead!
376 * It's highly recommended to use the 'local_lang' method
377 * (and thereby it's highly deprecated to use 'language-splitted' label strings)
378 * Refer to 'Inside TYPO3' for more details
379 *
380 * @param string $input Label key/reference
381 * @param boolean $hsc If set, the return value is htmlspecialchar'ed
382 * @return string
383 * @access public
384 */
385 public function sL($input, $hsc = FALSE) {
386 // Using obsolete 'language-splitted' labels:
387 if (strcmp(substr($input, 0, 4), 'LLL:')) {
388 $t = explode('|', $input);
389 $out = $t[$this->langSplitIndex] ? $t[$this->langSplitIndex] : $t[0];
390 if ($hsc) {
391 $out = htmlspecialchars($out);
392 }
393 return $out;
394 // LOCAL_LANG:
395 } else {
396 // If cached label
397 if (!isset($this->LL_labels_cache[$this->lang][$input])) {
398 $restStr = trim(substr($input, 4));
399 $extPrfx = '';
400
401 // ll-file refered to is found in an extension.
402 if (!strcmp(substr($restStr, 0, 4), 'EXT:')) {
403 $restStr = trim(substr($restStr, 4));
404 $extPrfx = 'EXT:';
405 }
406 $parts = explode(':', $restStr);
407 $parts[0] = $extPrfx . $parts[0];
408
409 // Getting data if not cached
410 if (!isset($this->LL_files_cache[$parts[0]])) {
411 $this->LL_files_cache[$parts[0]] = $this->readLLfile($parts[0]);
412
413 // If the current language is found in another file, load that as well:
414 $lFileRef = $this->localizedFileRef($parts[0]);
415 if ($lFileRef && is_string($this->LL_files_cache[$parts[0]][$this->lang])
416 && $this->LL_files_cache[$parts[0]][$this->lang] == 'EXT') {
417 $tempLL = $this->readLLfile($lFileRef);
418 $this->LL_files_cache[$parts[0]][$this->lang] = $tempLL[$this->lang];
419 }
420
421 // Overriding file?
422 // @deprecated since TYPO3 4.3, remove in TYPO3 4.5, please use the generic method in
423 // t3lib_div::readLLfile and the global array $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride']
424 if (isset($GLOBALS['TYPO3_CONF_VARS']['BE']['XLLfile'][$parts[0]])) {
425 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.');
426 $ORarray = $this->readLLfile($GLOBALS['TYPO3_CONF_VARS']['BE']['XLLfile'][$parts[0]]);
427 $this->LL_files_cache[$parts[0]] = t3lib_div::array_merge_recursive_overrule($this->LL_files_cache[$parts[0]], $ORarray);
428 }
429 }
430 $this->LL_labels_cache[$this->lang][$input] = $this->getLLL($parts[1], $this->LL_files_cache[$parts[0]]);
431 }
432 // For the cached output charset conversion has already happened!
433 // So perform HSC right here.
434 $output = $this->LL_labels_cache[$this->lang][$input];
435 if ($hsc) {
436 $output = t3lib_div::deHSCentities(htmlspecialchars($output));
437 }
438 return $output . $this->debugLL($input);
439 }
440 }
441
442 /**
443 * Loading $TCA_DESCR[$table]['columns'] with content from locallang files
444 * as defined in $TCA_DESCR[$table]['refs']
445 * $TCA_DESCR is a global var
446 *
447 * @param string $table Table name found as key in global array $TCA_DESCR
448 * @return void
449 * @access public
450 */
451 public function loadSingleTableDescription($table) {
452
453 // First the 'table' cannot already be loaded in [columns]
454 // and secondly there must be a references to locallang files available in [refs]
455 if (is_array($GLOBALS['TCA_DESCR'][$table])
456 && !isset($GLOBALS['TCA_DESCR'][$table]['columns'])
457 && is_array($GLOBALS['TCA_DESCR'][$table]['refs'])) {
458
459 // Init $TCA_DESCR for $table-key
460 $GLOBALS['TCA_DESCR'][$table]['columns'] = array();
461
462 // Get local-lang for each file in $TCA_DESCR[$table]['refs'] as they are ordered.
463 foreach ($GLOBALS['TCA_DESCR'][$table]['refs'] as $llfile) {
464 $localLanguage = $this->includeLLFile($llfile, 0, 1);
465
466 // Traverse all keys
467 if (is_array($localLanguage['default'])) {
468 foreach ($localLanguage['default'] as $lkey => $lVal) {
469 $type = '';
470 $fieldName = '';
471
472 // Exploding by '.':
473 // 0-n => fieldname,
474 // n+1 => type from (alttitle, description, details, syntax, image_descr,image,seeAlso),
475 // n+2 => special instruction, if any
476 $keyParts = explode('.', $lkey);
477 $keyPartsCount = count($keyParts);
478 // Check if last part is special instruction
479 // Only "+" is currently supported
480 $specialInstruction = ($keyParts[$keyPartsCount - 1] == '+') ? TRUE : FALSE;
481 if ($specialInstruction) {
482 array_pop($keyParts);
483 }
484
485 // If there are more than 2 parts, get the type from the last part
486 // and merge back the other parts with a dot (.)
487 // Otherwise just get type and field name straightaway
488 if ($keyPartsCount > 2) {
489 $type = array_pop($keyParts);
490 $fieldName = implode('.', $keyParts);
491 } else {
492 $fieldName = $keyParts[0];
493 $type = $keyParts[1];
494 }
495
496 // Detecting 'hidden' labels, converting to normal fieldname
497 if ($fieldName == '_') {
498 $fieldName = '';
499 }
500 if (substr($fieldName, 0, 1) == '_') {
501 $fieldName = substr($fieldName, 1);
502 }
503
504 // Append label
505 if ($specialInstruction) {
506 $GLOBALS['TCA_DESCR'][$table]['columns'][$fieldName][$type] .= LF . $lVal[0]['source'];
507 } else {
508 // Substitute label
509 $GLOBALS['TCA_DESCR'][$table]['columns'][$fieldName][$type] = $lVal[0]['source'];
510 }
511 }
512 }
513 }
514 }
515 }
516
517 /**
518 * Includes locallang file (and possibly additional localized version if configured for)
519 * Read language labels will be merged with $LOCAL_LANG (if $setGlobal = TRUE).
520 *
521 * @param string $fileRef $fileRef is a file-reference (see t3lib_div::getFileAbsFileName)
522 * @param boolean $setGlobal Setting in global variable $LOCAL_LANG (or returning the variable)
523 * @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.
524 * @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
525 * @access public
526 */
527 public function includeLLFile($fileRef, $setGlobal = TRUE, $mergeLocalOntoDefault = FALSE) {
528
529 $globalLanguage = array();
530
531 // Get default file
532 $localLanguage = $this->readLLfile($fileRef);
533
534 if (is_array($localLanguage) && count($localLanguage)) {
535
536 // it depends on, whether we should return the result or set it in the global $LOCAL_LANG array
537 if ($setGlobal) {
538 $globalLanguage = t3lib_div::array_merge_recursive_overrule((array)$GLOBALS['LOCAL_LANG'], $localLanguage);
539 } else {
540 $globalLanguage = $localLanguage;
541 }
542
543 // Localized addition?
544 $lFileRef = $this->localizedFileRef($fileRef);
545 if ($lFileRef && (string)$globalLanguage[$this->lang] == 'EXT') {
546 $localLanguage = $this->readLLfile($lFileRef);
547 $globalLanguage = t3lib_div::array_merge_recursive_overrule($globalLanguage, $localLanguage);
548 }
549
550 // Merge local onto default
551 if ($mergeLocalOntoDefault && $this->lang !== 'default'
552 && is_array($globalLanguage[$this->lang]) && is_array($globalLanguage['default'])) {
553 // array_merge can be used so far the keys are not
554 // numeric - which we assume they are not...
555 $globalLanguage['default'] = array_merge($globalLanguage['default'], $globalLanguage[$this->lang]);
556 unset($globalLanguage[$this->lang]);
557 }
558 }
559
560 // Return value if not global is set.
561 if (!$setGlobal) {
562 return $globalLanguage;
563 } else {
564 $GLOBALS['LOCAL_LANG'] = $globalLanguage;
565 }
566 }
567
568 /**
569 * Includes a locallang file and returns the $LOCAL_LANG array found inside.
570 *
571 * @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.
572 * @return array Value of $LOCAL_LANG found in the included file. If that array is found it's returned. Otherwise an empty array
573 * @access private
574 */
575 protected function readLLfile($fileRef) {
576 return t3lib_div::readLLfile($fileRef, $this->lang, $this->charSet);
577 }
578
579 /**
580 * Returns localized fileRef (.[langkey].php)
581 *
582 * @param string $fileRef Filename/path of a 'locallang.php' file
583 * @return string Input filename with a '.[lang-key].php' ending added if $this->lang is not 'default'
584 * @access private
585 */
586 protected function localizedFileRef($fileRef) {
587 if ($this->lang != 'default' && substr($fileRef, -4) == '.php') {
588 return substr($fileRef, 0, -4) . '.' . $this->lang . '.php';
589 }
590 }
591
592 /**
593 * Overrides a label.
594 *
595 * @param string $index
596 * @param string $value
597 * @param boolean $overrideDefault Overrides default language
598 * @return void
599 */
600 public function overrideLL($index, $value, $overrideDefault = TRUE) {
601 if (isset($GLOBALS['LOCAL_LANG']) === FALSE) {
602 $GLOBALS['LOCAL_LANG'] = array();
603 }
604
605 $GLOBALS['LOCAL_LANG'][$this->lang][$index][0]['target'] = $value;
606
607 if ($overrideDefault) {
608 $GLOBALS['LOCAL_LANG']['default'][$index][0]['target'] = $value;
609 }
610 }
611 }
612
613
614 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/lang/lang.php'])) {
615 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/lang/lang.php']);
616 }
617
618 ?>