Cleanup: Updated copyright comments
[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 * $Id$
31 * Revised for TYPO3 3.6.0
32 *
33 * @author Kasper Skårhøj <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 Skårhøj <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 throw new RuntimeException('Forced charset not found: The forced character set "'. $this->charSet . '" was not found in t3lib/csconvtbl/');
168 }
169 }
170 }
171
172 /**
173 * Adds labels and image references from the backend modules to the internal moduleLabels array
174 *
175 * @param array Array with references to module labels, keys: ['labels']['tablabel'],
176 * ['labels']['tabdescr'], ['tabs']['tab']
177 * @param string Module name prefix
178 * @return void
179 * @access public
180 * @see t3lib_loadModules
181 */
182 public function addModuleLabels($arr, $prefix) {
183 if (is_array($arr)) {
184 foreach ($arr as $k => $larr) {
185 if (!isset($this->moduleLabels[$k])) {
186 $this->moduleLabels[$k] = array();
187 }
188 if (is_array($larr)) {
189 foreach ($larr as $l => $v) {
190 $this->moduleLabels[$k][$prefix . $l] = $v;
191 }
192 }
193 }
194 }
195 }
196
197 /**
198 * Will htmlspecialchar() the input string and before that any charset conversion
199 * will also have taken place if needed (see init())
200 * Used to pipe language labels through just before they are returned.
201 *
202 * @param string The string to process
203 * @param boolean If set, then the string is htmlspecialchars()'ed
204 * @return string The processed string
205 * @see init()
206 * @access public
207 */
208 public function hscAndCharConv($lStr, $hsc) {
209 // labels returned from a locallang file used to be in the language of the charset.
210 // Since TYPO3 4.1 they are always in the charset of the BE.
211 if ($hsc) {
212 return htmlspecialchars($lStr);
213 } else {
214 return $lStr;
215 }
216 }
217
218 /**
219 * Will convert the input strings special chars (all above 127) to entities.
220 * The string is expected to be encoded in the charset, $this->charSet
221 * This function is used to create strings that can be used in the Click Menu
222 * (Context Sensitive Menus). The reason is that the values that are dynamically
223 * written into the <div> layer is decoded as iso-8859-1 no matter what charset
224 * is used in the document otherwise (only MSIE, Mozilla is OK).
225 * So by converting we by-pass this problem.
226 *
227 * @param string Input string
228 * @return string Output string
229 * @access public
230 */
231 public function makeEntities($str) {
232 // Convert string to UTF-8:
233 if ($this->charSet != 'utf-8') {
234 $str = $this->csConvObj->utf8_encode($str, $this->charSet);
235 }
236
237 // Convert string back again, but using the full entity conversion:
238 return $this->csConvObj->utf8_to_entities($str);
239 }
240
241 /**
242 * Converts the input string to a JavaScript function returning the same string, but charset-safe.
243 * Used for confirm and alert boxes where we must make sure that any string content
244 * does not break the script AND want to make sure the charset is preserved.
245 * Originally I used the JS function unescape() in combination with PHP function
246 * rawurlencode() in order to pass strings in a safe way. This could still be done
247 * for iso-8859-1 charsets but now I have applied the same method here for all charsets.
248 *
249 * @param string Input string, encoded with $this->charSet
250 * @return string Output string, a JavaScript function: "String.fromCharCode(......)"
251 * @access public
252 */
253 public function JScharCode($str) {
254
255 // Convert string to UTF-8:
256 if ($this->charSet != 'utf-8') {
257 $str = $this->csConvObj->utf8_encode($str, $this->charSet);
258 }
259
260 // Convert the UTF-8 string into a array of char numbers:
261 $nArr = $this->csConvObj->utf8_to_numberarray($str);
262
263 return 'String.fromCharCode(' . implode(',', $nArr) . ')';
264 }
265
266 /**
267 * Returns the label with key $index form the globally loaded $LOCAL_LANG array.
268 * Mostly used from modules with only one LOCAL_LANG file loaded into the global space.
269 *
270 * @param string Label key
271 * @param boolean If set, the return value is htmlspecialchar'ed
272 * @return string
273 * @access public
274 */
275 public function getLL($index, $hsc = 0) {
276 // Get Local Language
277 if (strcmp($GLOBALS['LOCAL_LANG'][$this->lang][$index], '')) {
278 // Returns local label if not blank.
279 $output = $this->hscAndCharConv($GLOBALS['LOCAL_LANG'][$this->lang][$index], $hsc);
280 } else {
281 // Returns default label
282 $output = $this->hscAndCharConv($GLOBALS['LOCAL_LANG']['default'][$index], $hsc);
283 }
284 return $output . ($this->debugKey ? ' [' . $index . ']' : '');
285 }
286
287 /**
288 * Works like ->getLL() but takes the $LOCAL_LANG array
289 * used as the second argument instead of using the global array.
290 *
291 * @param string Label key
292 * @param array $LOCAL_LANG array to get label key from
293 * @param boolean If set, the return value is htmlspecialchar'ed
294 * @return string
295 * @access public
296 */
297 public function getLLL($index, $LOCAL_LANG, $hsc = 0) {
298 // Get Local Language
299 if (strcmp($LOCAL_LANG[$this->lang][$index], '')) {
300 // Returns local label if not blank.
301 $output = $this->hscAndCharConv($LOCAL_LANG[$this->lang][$index], $hsc);
302 } else {
303 // Returns default label
304 $output = $this->hscAndCharConv($LOCAL_LANG['default'][$index], $hsc);
305 }
306 return $output . ($this->debugKey ? ' [' . $index . ']' : '');
307 }
308
309 /**
310 * splitLabel function
311 * Historically labels were exploded by '|' and each part would correspond
312 * to the translation of the language found at the same 'index' in the TYPO3_languages constant.
313 * Today all translations are based on $LOCAL_LANG variables.
314 * 'language-splitted' labels can therefore refer to a local-lang file + index instead!
315 * It's highly recommended to use the 'local_lang' method
316 * (and thereby it's highly deprecated to use 'language-splitted' label strings)
317 * Refer to 'Inside TYPO3' for more details
318 *
319 * @param string Label key/reference
320 * @param boolean If set, the return value is htmlspecialchar'ed
321 * @return string
322 * @access public
323 */
324 public function sL($input, $hsc = 0) {
325 // Using obsolete 'language-splitted' labels:
326 if (strcmp(substr($input, 0, 4), 'LLL:')) {
327 $t = explode('|', $input);
328 $out = $t[$this->langSplitIndex] ? $t[$this->langSplitIndex] : $t[0];
329 return $this->hscAndCharConv($out, $hsc);
330 // LOCAL_LANG:
331 } else {
332 // If cached label
333 if (!isset($this->LL_labels_cache[$this->lang][$input])) {
334 $restStr = trim(substr($input, 4));
335 $extPrfx = '';
336
337 // ll-file refered to is found in an extension.
338 if (!strcmp(substr($restStr, 0, 4), 'EXT:')) {
339 $restStr = trim(substr($restStr, 4));
340 $extPrfx = 'EXT:';
341 }
342 $parts = explode(':', $restStr);
343 $parts[0] = $extPrfx . $parts[0];
344
345 // Getting data if not cached
346 if (!isset($this->LL_files_cache[$parts[0]])) {
347 $this->LL_files_cache[$parts[0]] = $this->readLLfile($parts[0]);
348
349 // If the current language is found in another file, load that as well:
350 $lFileRef = $this->localizedFileRef($parts[0]);
351 if ($lFileRef && is_string($this->LL_files_cache[$parts[0]][$this->lang])
352 && $this->LL_files_cache[$parts[0]][$this->lang] == 'EXT') {
353 $tempLL = $this->readLLfile($lFileRef);
354 $this->LL_files_cache[$parts[0]][$this->lang] = $tempLL[$this->lang];
355 }
356
357 // Overriding file?
358 // @deprecated since TYPO3 4.3, remove in TYPO3 4.5, please use the generic method in
359 // t3lib_div::readLLfile and the global array $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride']
360 if (isset($GLOBALS['TYPO3_CONF_VARS']['BE']['XLLfile'][$parts[0]])) {
361 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.');
362 $ORarray = $this->readLLfile($GLOBALS['TYPO3_CONF_VARS']['BE']['XLLfile'][$parts[0]]);
363 $this->LL_files_cache[$parts[0]] = t3lib_div::array_merge_recursive_overrule($this->LL_files_cache[$parts[0]], $ORarray);
364 }
365 }
366 $this->LL_labels_cache[$this->lang][$input] = $this->getLLL($parts[1], $this->LL_files_cache[$parts[0]]);
367 }
368 // For the cached output charset conversion has already happened!
369 // So perform HSC right here.
370 $output = $this->LL_labels_cache[$this->lang][$input];
371 if ($hsc) {
372 $output = t3lib_div::deHSCentities(htmlspecialchars($output));
373 }
374 return $output . ($this->debugKey ? ' [' . $input . ']' : '');
375 }
376 }
377
378 /**
379 * Loading $TCA_DESCR[$table]['columns'] with content from locallang files
380 * as defined in $TCA_DESCR[$table]['refs']
381 * $TCA_DESCR is a global var
382 *
383 * @param string Table name found as key in global array $TCA_DESCR
384 * @return void
385 * @access public
386 */
387 public function loadSingleTableDescription($table) {
388 global $TCA_DESCR;
389
390 // First the 'table' cannot already be loaded in [columns]
391 // and secondly there must be a references to locallang files available in [refs]
392 if (is_array($TCA_DESCR[$table])
393 && !isset($TCA_DESCR[$table]['columns'])
394 && is_array($TCA_DESCR[$table]['refs'])) {
395
396 // Init $TCA_DESCR for $table-key
397 $TCA_DESCR[$table]['columns'] = array();
398
399 // Get local-lang for each file in $TCA_DESCR[$table]['refs'] as they are ordered.
400 foreach ($TCA_DESCR[$table]['refs'] as $llfile) {
401 $LOCAL_LANG = $this->includeLLFile($llfile, 0, 1);
402
403 // Traverse all keys
404 if (is_array($LOCAL_LANG['default'])) {
405 foreach ($LOCAL_LANG['default'] as $lkey => $lVal) {
406
407 // exploding by '.':
408 // 0 => fieldname,
409 // 1 => type from (alttitle, description, details, syntax, image_descr,image,seeAlso),
410 // 2 => special instruction, see switch construct
411 $kParts = explode('.', $lkey);
412
413 // Detecting 'hidden' labels, converting to normal fieldname
414 if ($kParts[0] == '_') {
415 $kParts[0] = '';
416 }
417 if (substr($kParts[0], 0, 1) == '_') {
418 $kParts[0] = substr($kParts[0], 1);
419 }
420
421 // Add label:
422 switch ((string)$kParts[2]) {
423 // adding
424 case '+':
425 $TCA_DESCR[$table]['columns'][$kParts[0]][$kParts[1]] .= LF . $lVal;
426 break;
427 // Substituting:
428 default:
429 $TCA_DESCR[$table]['columns'][$kParts[0]][$kParts[1]] = $lVal;
430 }
431 }
432 }
433 }
434 }
435 }
436
437 /**
438 * Includes locallang file (and possibly additional localized version if configured for)
439 * Read language labels will be merged with $LOCAL_LANG (if $setGlobal = true).
440 *
441 * @param string $fileRef is a file-reference (see t3lib_div::getFileAbsFileName)
442 * @param boolean Setting in global variable $LOCAL_LANG (or returning the variable)
443 * @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.
444 * @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
445 * @access public
446 */
447 public function includeLLFile($fileRef, $setGlobal = 1, $mergeLocalOntoDefault = 0) {
448 // Configure for global flag:
449 if ($setGlobal) {
450 global $LOCAL_LANG;
451 }
452
453 // Get default file
454 $llang = $this->readLLfile($fileRef);
455
456 if (is_array($llang) && count($llang)) {
457
458 $LOCAL_LANG = t3lib_div::array_merge_recursive_overrule((array)$LOCAL_LANG, $llang);
459
460 // Localized addition?
461 $lFileRef = $this->localizedFileRef($fileRef);
462 if ($lFileRef && (string)$LOCAL_LANG[$this->lang] == 'EXT') {
463 $llang = $this->readLLfile($lFileRef);
464 $LOCAL_LANG = t3lib_div::array_merge_recursive_overrule($LOCAL_LANG, $llang);
465 }
466
467 // Overriding file?
468 // @deprecated since TYPO3 4.3, remove in TYPO3 4.5, please use the generic method in
469 // t3lib_div::readLLfile and the global array $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride']
470 if (isset($GLOBALS['TYPO3_CONF_VARS']['BE']['XLLfile'][$fileRef])) {
471 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.');
472 $ORarray = $this->readLLfile($GLOBALS['TYPO3_CONF_VARS']['BE']['XLLfile'][$fileRef]);
473 $LOCAL_LANG = t3lib_div::array_merge_recursive_overrule($LOCAL_LANG, $ORarray);
474 }
475
476 // Merge local onto default
477 if ($mergeLocalOntoDefault && strcmp($this->lang, 'default') && is_array($LOCAL_LANG[$this->lang]) && is_array($LOCAL_LANG['default'])) {
478 // array_merge can be used so far the keys are not
479 // numeric - which we assume they are not...
480 $LOCAL_LANG['default'] = array_merge($LOCAL_LANG['default'], $LOCAL_LANG[$this->lang]);
481 unset($LOCAL_LANG[$this->lang]);
482 }
483 }
484
485 // Return value if not global is set.
486 if (!$setGlobal) {
487 return $LOCAL_LANG;
488 }
489 }
490
491 /**
492 * Includes a locallang file and returns the $LOCAL_LANG array found inside.
493 *
494 * @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.
495 * @return array Value of $LOCAL_LANG found in the included file. If that array is found it's returned. Otherwise an empty array
496 * @access private
497 */
498 protected function readLLfile($fileRef) {
499 return t3lib_div::readLLfile($fileRef, $this->lang, $this->charSet);
500 }
501
502 /**
503 * Returns localized fileRef (.[langkey].php)
504 *
505 * @param string Filename/path of a 'locallang.php' file
506 * @return string Input filename with a '.[lang-key].php' ending added if $this->lang is not 'default'
507 * @access private
508 */
509 protected function localizedFileRef($fileRef) {
510 if ($this->lang != 'default' && substr($fileRef, -4) == '.php') {
511 return substr($fileRef, 0, -4) . '.' . $this->lang . '.php';
512 }
513 }
514 }
515
516
517 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/lang/lang.php'])) {
518 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/lang/lang.php']);
519 }
520
521 ?>