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