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