800c10a7c01cdc803805ad6af80c34312d756eae
[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
357 if ($localLanguage[$this->lang][$index][0]['target'] !== '') {
358 $output = $this->hscAndCharConv($localLanguage[$this->lang][$index][0]['target'], $hsc);
359 } else {
360 $output = $this->hscAndCharConv($localLanguage['default'][$index][0]['target'], $hsc);
361 }
362 return $output . $this->debugLL($index);
363 }
364
365 /**
366 * splitLabel function
367 * Historically labels were exploded by '|' and each part would correspond
368 * to the translation of the language found at the same 'index' in the TYPO3_languages constant.
369 * Today all translations are based on $LOCAL_LANG variables.
370 * 'language-splitted' labels can therefore refer to a local-lang file + index instead!
371 * It's highly recommended to use the 'local_lang' method
372 * (and thereby it's highly deprecated to use 'language-splitted' label strings)
373 * Refer to 'Inside TYPO3' for more details
374 *
375 * @param string $input Label key/reference
376 * @param boolean $hsc If set, the return value is htmlspecialchar'ed
377 * @return string
378 * @access public
379 */
380 public function sL($input, $hsc = FALSE) {
381 // Using obsolete 'language-splitted' labels:
382 if (strcmp(substr($input, 0, 4), 'LLL:')) {
383 $t = explode('|', $input);
384 $out = $t[$this->langSplitIndex] ? $t[$this->langSplitIndex] : $t[0];
385 return $this->hscAndCharConv($out, $hsc);
386 // LOCAL_LANG:
387 } else {
388 // If cached label
389 if (!isset($this->LL_labels_cache[$this->lang][$input])) {
390 $restStr = trim(substr($input, 4));
391 $extPrfx = '';
392
393 // ll-file refered to is found in an extension.
394 if (!strcmp(substr($restStr, 0, 4), 'EXT:')) {
395 $restStr = trim(substr($restStr, 4));
396 $extPrfx = 'EXT:';
397 }
398 $parts = explode(':', $restStr);
399 $parts[0] = $extPrfx . $parts[0];
400
401 // Getting data if not cached
402 if (!isset($this->LL_files_cache[$parts[0]])) {
403 $this->LL_files_cache[$parts[0]] = $this->readLLfile($parts[0]);
404
405 // If the current language is found in another file, load that as well:
406 $lFileRef = $this->localizedFileRef($parts[0]);
407 if ($lFileRef && is_string($this->LL_files_cache[$parts[0]][$this->lang])
408 && $this->LL_files_cache[$parts[0]][$this->lang] == 'EXT') {
409 $tempLL = $this->readLLfile($lFileRef);
410 $this->LL_files_cache[$parts[0]][$this->lang] = $tempLL[$this->lang];
411 }
412
413 // Overriding file?
414 // @deprecated since TYPO3 4.3, remove in TYPO3 4.5, please use the generic method in
415 // t3lib_div::readLLfile and the global array $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride']
416 if (isset($GLOBALS['TYPO3_CONF_VARS']['BE']['XLLfile'][$parts[0]])) {
417 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.');
418 $ORarray = $this->readLLfile($GLOBALS['TYPO3_CONF_VARS']['BE']['XLLfile'][$parts[0]]);
419 $this->LL_files_cache[$parts[0]] = t3lib_div::array_merge_recursive_overrule($this->LL_files_cache[$parts[0]], $ORarray);
420 }
421 }
422 $this->LL_labels_cache[$this->lang][$input] = $this->getLLL($parts[1], $this->LL_files_cache[$parts[0]]);
423 }
424 // For the cached output charset conversion has already happened!
425 // So perform HSC right here.
426 $output = $this->LL_labels_cache[$this->lang][$input];
427 if ($hsc) {
428 $output = t3lib_div::deHSCentities(htmlspecialchars($output));
429 }
430 return $output . $this->debugLL($input);
431 }
432 }
433
434 /**
435 * Loading $TCA_DESCR[$table]['columns'] with content from locallang files
436 * as defined in $TCA_DESCR[$table]['refs']
437 * $TCA_DESCR is a global var
438 *
439 * @param string $table Table name found as key in global array $TCA_DESCR
440 * @return void
441 * @access public
442 */
443 public function loadSingleTableDescription($table) {
444
445 // First the 'table' cannot already be loaded in [columns]
446 // and secondly there must be a references to locallang files available in [refs]
447 if (is_array($GLOBALS['TCA_DESCR'][$table])
448 && !isset($GLOBALS['TCA_DESCR'][$table]['columns'])
449 && is_array($GLOBALS['TCA_DESCR'][$table]['refs'])) {
450
451 // Init $TCA_DESCR for $table-key
452 $GLOBALS['TCA_DESCR'][$table]['columns'] = array();
453
454 // Get local-lang for each file in $TCA_DESCR[$table]['refs'] as they are ordered.
455 foreach ($GLOBALS['TCA_DESCR'][$table]['refs'] as $llfile) {
456 $localLanguage = $this->includeLLFile($llfile, 0, 1);
457
458 // Traverse all keys
459 if (is_array($localLanguage['default'])) {
460 foreach ($localLanguage['default'] as $lkey => $lVal) {
461 $type = '';
462 $fieldName = '';
463
464 // Exploding by '.':
465 // 0-n => fieldname,
466 // n+1 => type from (alttitle, description, details, syntax, image_descr,image,seeAlso),
467 // n+2 => special instruction, if any
468 $keyParts = explode('.', $lkey);
469 $keyPartsCount = count($keyParts);
470 // Check if last part is special instruction
471 // Only "+" is currently supported
472 $specialInstruction = ($keyParts[$keyPartsCount - 1] == '+') ? TRUE : FALSE;
473 if ($specialInstruction) {
474 array_pop($keyParts);
475 }
476
477 // If there are more than 2 parts, get the type from the last part
478 // and merge back the other parts with a dot (.)
479 // Otherwise just get type and field name straightaway
480 if ($keyPartsCount > 2) {
481 $type = array_pop($keyParts);
482 $fieldName = implode('.', $keyParts);
483 } else {
484 $fieldName = $keyParts[0];
485 $type = $keyParts[1];
486 }
487
488 // Detecting 'hidden' labels, converting to normal fieldname
489 if ($fieldName == '_') {
490 $fieldName = '';
491 }
492 if (substr($fieldName, 0, 1) == '_') {
493 $fieldName = substr($fieldName, 1);
494 }
495
496 // Append label
497 if ($specialInstruction) {
498 $GLOBALS['TCA_DESCR'][$table]['columns'][$fieldName][$type] .= LF . $lVal[0]['source'];
499 } else {
500 // Substitute label
501 $GLOBALS['TCA_DESCR'][$table]['columns'][$fieldName][$type] = $lVal[0]['source'];
502 }
503 }
504 }
505 }
506 }
507 }
508
509 /**
510 * Includes locallang file (and possibly additional localized version if configured for)
511 * Read language labels will be merged with $LOCAL_LANG (if $setGlobal = TRUE).
512 *
513 * @param string $fileRef $fileRef is a file-reference (see t3lib_div::getFileAbsFileName)
514 * @param boolean $setGlobal Setting in global variable $LOCAL_LANG (or returning the variable)
515 * @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.
516 * @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
517 * @access public
518 */
519 public function includeLLFile($fileRef, $setGlobal = TRUE, $mergeLocalOntoDefault = FALSE) {
520
521 $globalLanguage = array();
522
523 // Get default file
524 $localLanguage = $this->readLLfile($fileRef);
525
526 if (is_array($localLanguage) && count($localLanguage)) {
527
528 // it depends on, whether we should return the result or set it in the global $LOCAL_LANG array
529 if ($setGlobal) {
530 $globalLanguage = t3lib_div::array_merge_recursive_overrule((array)$GLOBALS['LOCAL_LANG'], $localLanguage);
531 } else {
532 $globalLanguage = $localLanguage;
533 }
534
535 // Localized addition?
536 $lFileRef = $this->localizedFileRef($fileRef);
537 if ($lFileRef && (string)$globalLanguage[$this->lang] == 'EXT') {
538 $localLanguage = $this->readLLfile($lFileRef);
539 $globalLanguage = t3lib_div::array_merge_recursive_overrule($globalLanguage, $localLanguage);
540 }
541
542 // Merge local onto default
543 if ($mergeLocalOntoDefault && $this->lang !== 'default'
544 && is_array($globalLanguage[$this->lang]) && is_array($globalLanguage['default'])) {
545 // array_merge can be used so far the keys are not
546 // numeric - which we assume they are not...
547 $globalLanguage['default'] = array_merge($globalLanguage['default'], $globalLanguage[$this->lang]);
548 unset($globalLanguage[$this->lang]);
549 }
550 }
551
552 // Return value if not global is set.
553 if (!$setGlobal) {
554 return $globalLanguage;
555 } else {
556 $GLOBALS['LOCAL_LANG'] = $globalLanguage;
557 }
558 }
559
560 /**
561 * Includes a locallang file and returns the $LOCAL_LANG array found inside.
562 *
563 * @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.
564 * @return array Value of $LOCAL_LANG found in the included file. If that array is found it's returned. Otherwise an empty array
565 * @access private
566 */
567 protected function readLLfile($fileRef) {
568 return t3lib_div::readLLfile($fileRef, $this->lang, $this->charSet);
569 }
570
571 /**
572 * Returns localized fileRef (.[langkey].php)
573 *
574 * @param string $fileRef Filename/path of a 'locallang.php' file
575 * @return string Input filename with a '.[lang-key].php' ending added if $this->lang is not 'default'
576 * @access private
577 */
578 protected function localizedFileRef($fileRef) {
579 if ($this->lang != 'default' && substr($fileRef, -4) == '.php') {
580 return substr($fileRef, 0, -4) . '.' . $this->lang . '.php';
581 }
582 }
583
584 /**
585 * Overrides a label.
586 *
587 * @param string $index
588 * @param string $value
589 * @param boolean $overrideDefault Overrides default language
590 * @return void
591 */
592 public function overrideLL($index, $value, $overrideDefault = TRUE) {
593 if (isset($GLOBALS['LOCAL_LANG']) === FALSE) {
594 $GLOBALS['LOCAL_LANG'] = array();
595 }
596
597 $GLOBALS['LOCAL_LANG'][$this->lang][$index][0]['target'] = $value;
598
599 if ($overrideDefault) {
600 $GLOBALS['LOCAL_LANG']['default'][$index][0]['target'] = $value;
601 }
602 }
603 }
604
605
606 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/lang/lang.php'])) {
607 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/lang/lang.php']);
608 }
609
610 ?>