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