Added $Id$ keywords, cleaned up comment tags
[Packages/TYPO3.CMS.git] / typo3 / sysext / lang / lang.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2003 Kasper Skaarhoj (kasper@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. TYPO3 is free software;
9 * You can redistribute it and/or modify it under the terms of the
10 * TYPO3 License as published from the www.typo3.com website.
11 *
12 * This script is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 *
16 * This copyright notice MUST APPEAR in all copies of this script
17 ***************************************************************/
18 /**
19 * Contains the TYPO3 Backend Language class
20 *
21 * $Id$
22 * Revised for TYPO3 3.6.0
23 *
24 * @author Kasper Skaarhoj <kasper@typo3.com>
25 */
26 /**
27 * [CLASS/FUNCTION INDEX of SCRIPT]
28 *
29 *
30 *
31 * 79: class language
32 * 158: function init($lang,$altPath='')
33 * 201: function addModuleLabels($arr,$prefix)
34 * 227: function hscAndCharConv($lStr,$hsc)
35 * 242: function makeEntities($str)
36 * 259: function JScharCode($str)
37 * 278: function getLL($index,$hsc=0)
38 * 295: function getLLL($index,$LOCAL_LANG,$hsc=0)
39 * 315: function sL($input,$hsc=0)
40 * 359: function loadSingleTableDescription($table)
41 * 410: function includeLLFile($fileRef,$setGlobal=1,$mergeLocalOntoDefault=0)
42 * 457: function readLLfile($fileRef)
43 * 471: function localizedFileRef($fileRef)
44 *
45 * TOTAL FUNCTIONS: 12
46 * (This index is automatically created/updated by the extension "extdeveval")
47 *
48 */
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 /**
66 * Contains the TYPO3 Backend Language class
67 *
68 * For detailed information about how localization is handled,
69 * please refer to the 'Inside TYPO3' document which descibes this.
70 *
71 * This class is normally instantiated as the global variable $LANG in typo3/template.php
72 * It's only available in the backend and under certain circumstances in the frontend
73 *
74 * @author Kasper Skaarhoj <kasper@typo3.com>
75 * @package TYPO3
76 * @subpackage core
77 * @see typo3/template.php, template
78 */
79 class language {
80 var $lang='default'; // This is set to the language that is currently running for the user
81 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!)
82
83 // Default charset in backend
84 var $charSet = 'iso-8859-1';
85
86 // Array with alternative charsets for other languages.
87 var $charSetArray = array(
88 'dk' => '',
89 'de' => '',
90 'no' => '',
91 'it' => '',
92 'fr' => '',
93 'es' => '',
94 'nl' => '',
95 'cz' => 'windows-1250',
96 'pl' => 'iso-8859-2',
97 'si' => 'windows-1250',
98 'fi' => '',
99 'tr' => 'iso-8859-9',
100 'se' => '',
101 'pt' => '',
102 'ru' => 'windows-1251',
103 'ro' => 'iso-8859-2',
104 'ch' => 'gb2312',
105 'sk' => 'windows-1250',
106 'lt' => 'windows-1257',
107 'is' => 'utf-8',
108 'hr' => 'windows-1250',
109 'hu' => 'iso-8859-2',
110 'gl' => '',
111 'th' => 'iso-8859-11',
112 'gr' => 'iso-8859-7',
113 'hk' => 'big5',
114 'eu' => '',
115 'bg' => 'windows-1251',
116 'br' => '',
117 'et' => 'iso-8859-4'
118 );
119
120 // This is the url to the TYPO3 manual
121 var $typo3_help_url= 'http://www.typo3.com/man_uk/';
122 // Array with alternative URLs based on language.
123 var $helpUrlArray = array(
124 'dk' => 'http://www.typo3.com/man_dk/',
125 );
126
127
128 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.
129
130 // Internal
131 var $langSplitIndex=0; // Points to the position of the current language key as found in constant TYPO3_languages
132 var $LL_files_cache=array(); // Internal cache for read LL-files
133 var $LL_labels_cache=array(); // Internal cache for ll-labels (filled as labels are requested)
134
135 // Internal charset conversion:
136 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.
137 var $csConvObj; // An instance of the "t3lib_cs" class. May be used by any application.
138
139
140
141
142
143
144
145
146 /**
147 * Initializes the backend language.
148 * This is for example done in typo3/template.php with lines like these:
149 *
150 * require (PATH_typo3.'sysext/lang/lang.php');
151 * $LANG = t3lib_div::makeInstance('language');
152 * $LANG->init($BE_USER->uc['lang']);
153 *
154 * @param string The language key (two character string from backend users profile)
155 * @param string IGNORE. Not used.
156 * @return void
157 */
158 function init($lang,$altPath='') {
159 // Internally setting the list of TYPO3 backend languages.
160 $this->langSplit=TYPO3_languages;
161
162 // Finding the requested language in this list based on the $lang key being inputted to this function.
163 $ls = explode('|',$this->langSplit);
164 while(list($i,$v)=each($ls)) {
165 if ($v==$lang) { // Language is found. Configure it:
166 $this->langSplitIndex=$i; // The index of the language as found in the TYPO3_languages list
167 $this->lang = $lang; // The current language key
168 if ($this->helpUrlArray[$this->lang]) $this->typo3_help_url=$this->helpUrlArray[$this->lang]; // The help URL if different from the default.
169 if ($this->charSetArray[$this->lang]) $this->charSet=$this->charSetArray[$this->lang]; // The charset if different from the default.
170 }
171 }
172
173 // Initialize the conversion object:
174 $this->csConvObj = t3lib_div::makeInstance('t3lib_cs');
175
176 // If a forced charset is used and different from the charset otherwise used:
177 if ($GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'] && $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset']!=$this->charSet) {
178 // Set the forced charset:
179 $this->origCharSet = $this->charSet;
180 $this->charSet = $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'];
181
182 if ($this->charSet!='utf-8' && !$this->csConvObj->initCharset($this->charSet)) {
183 t3lib_BEfunc::typo3PrintError ('The forced character set "'.$this->charSet.'" was not found in t3lib/csconvtbl/','Forced charset not found');
184 exit;
185 }
186 if ($this->origCharSet!='utf-8' && !$this->csConvObj->initCharset($this->origCharSet)) {
187 t3lib_BEfunc::typo3PrintError ('The original character set "'.$this->origCharSet.'" was not found in t3lib/csconvtbl/','Forced charset not found');
188 exit;
189 }
190 }
191 }
192
193 /**
194 * Adds labels and image references from the backend modules to the internal moduleLabels array
195 *
196 * @param array Array with references to module labels, keys: ['labels']['tablabel'], ['labels']['tabdescr'], ['tabs']['tab']
197 * @param string Module name prefix
198 * @return void
199 * @see t3lib_loadModules
200 */
201 function addModuleLabels($arr,$prefix) {
202 if (is_array($arr)) {
203 reset($arr);
204 while(list($k,$larr)=each($arr)) {
205 if (!isset($this->moduleLabels[$k])) {
206 $this->moduleLabels[$k]=array();
207 }
208 if (is_array($larr)) {
209 reset($larr);
210 while(list($l,$v)=each($larr)) {
211 $this->moduleLabels[$k][$prefix.$l]=$v;
212 }
213 }
214 }
215 }
216 }
217
218 /**
219 * Will htmlspecialchar() the input string and before that any charset conversion will also have taken place if needed (see init())
220 * Used to pipe language labels through just before they are returned.
221 *
222 * @param string The string to process
223 * @param boolean If set, then the string is htmlspecialchars()'ed
224 * @return string The processed string
225 * @see init()
226 */
227 function hscAndCharConv($lStr,$hsc) {
228 $lStr = $hsc ? htmlspecialchars($lStr) : $lStr;
229 if ($this->origCharSet) {
230 $lStr = $this->csConvObj->conv($lStr,$this->origCharSet,$this->charSet,1);
231 }
232 return $lStr;
233 }
234
235 /**
236 * Will convert the input strings special chars (all above 127) to entities. The string is expected to be encoded in the charset, $this->charSet
237 * 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.
238 *
239 * @param string Input string
240 * @return string Output string
241 */
242 function makeEntities($str) {
243 // Convert string to UTF-8:
244 if ($this->charSet!='utf-8') $str = $this->csConvObj->utf8_encode($str,$this->charSet);
245
246 // Convert string back again, but using the full entity conversion:
247 $str = $this->csConvObj->utf8_to_entities($str);
248 return $str;
249 }
250
251 /**
252 * Converts the input string to a JavaScript function returning the same string, but charset-safe.
253 * 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.
254 * 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.
255 *
256 * @param string Input string, encoded with $this->charSet
257 * @return string Output string, a JavaScript function: "String.fromCharCode(......)"
258 */
259 function JScharCode($str) {
260
261 // Convert string to UTF-8:
262 if ($this->charSet!='utf-8') $str = $this->csConvObj->utf8_encode($str,$this->charSet);
263
264 // Convert the UTF-8 string into a array of char numbers:
265 $nArr = $this->csConvObj->utf8_to_numberarray($str);
266
267 return 'String.fromCharCode('.implode(',',$nArr).')';
268 }
269
270 /**
271 * Returns the label with key $index form the globally loaded $LOCAL_LANG array.
272 * Mostly used from modules with only one LOCAL_LANG file loaded into the global space.
273 *
274 * @param string Label key
275 * @param boolean If set, the return value is htmlspecialchar'ed
276 * @return string
277 */
278 function getLL($index,$hsc=0) {
279 // Get Local Language
280 if (strcmp($GLOBALS['LOCAL_LANG'][$this->lang][$index],'')) {
281 return $this->hscAndCharConv($GLOBALS['LOCAL_LANG'][$this->lang][$index], $hsc); // Returns local label if not blank.
282 } else {
283 return $this->hscAndCharConv($GLOBALS['LOCAL_LANG']['default'][$index], $hsc); // Returns default label
284 }
285 }
286
287 /**
288 * Works like ->getLL() but takes the $LOCAL_LANG array used as the second argument instead of using the global array.
289 *
290 * @param string Label key
291 * @param array $LOCAL_LANG array to get label key from
292 * @param boolean If set, the return value is htmlspecialchar'ed
293 * @return string
294 */
295 function getLLL($index,$LOCAL_LANG,$hsc=0) {
296 // Get Local Language
297 if (strcmp($LOCAL_LANG[$this->lang][$index],'')) {
298 return $this->hscAndCharConv($LOCAL_LANG[$this->lang][$index], $hsc); // Returns local label if not blank.
299 } else {
300 return $this->hscAndCharConv($LOCAL_LANG['default'][$index], $hsc); // Returns default label
301 }
302 }
303
304 /**
305 * splitLabel function
306 * 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.
307 * Today all translations are based on $LOCAL_LANG variables. 'language-splitted' labels can therefore refer to a local-lang file + index instead!
308 * It's highly recommended to use the 'local_lang' method (and thereby it's highly depreciated to use 'language-splitted' label strings)
309 * Refer to 'Inside TYPO3' for more details
310 *
311 * @param string Label key/reference
312 * @param boolean If set, the return value is htmlspecialchar'ed
313 * @return string
314 */
315 function sL($input,$hsc=0) {
316 if (strcmp(substr($input,0,4),'LLL:')) { // Using obsolete 'language-splitted' labels:
317 $t = explode('|',$input);
318 $out = $t[$this->langSplitIndex] ? $t[$this->langSplitIndex] : $t[0];
319 return $this->hscAndCharConv($out, $hsc);
320 } else { // LOCAL_LANG:
321 if (!isset($this->LL_labels_cache[$this->lang][$input])) { // If cached label
322 $restStr = trim(substr($input,4));
323 $extPrfx='';
324 if (!strcmp(substr($restStr,0,4),'EXT:')) { // ll-file refered to is found in an extension.
325 $restStr = trim(substr($restStr,4));
326 $extPrfx='EXT:';
327 }
328 $parts = explode(':',$restStr);
329 $parts[0]=$extPrfx.$parts[0];
330 if (!isset($this->LL_files_cache[$parts[0]])) { // Getting data if not cached
331 $this->LL_files_cache[$parts[0]] = $this->readLLfile($parts[0]);
332
333 // If the current language is found in another file, load that as well:
334 $lFileRef = $this->localizedFileRef($parts[0]);
335 if ($lFileRef && is_string($this->LL_files_cache[$parts[0]][$this->lang]) && $this->LL_files_cache[$parts[0]][$this->lang]=='EXT') {
336 $tempLL = $this->readLLfile($lFileRef);
337 $this->LL_files_cache[$parts[0]][$this->lang] = $tempLL[$this->lang];
338 }
339
340 // Overriding file?
341 if (isset($GLOBALS['TYPO3_CONF_VARS']['BE']['XLLfile'][$parts[0]])) {
342 $ORarray = $this->readLLfile($GLOBALS['TYPO3_CONF_VARS']['BE']['XLLfile'][$parts[0]]);
343 $this->LL_files_cache[$parts[0]] = t3lib_div::array_merge_recursive_overrule($this->LL_files_cache[$parts[0]],$ORarray);
344 }
345 }
346 $this->LL_labels_cache[$this->lang][$input] = $this->getLLL($parts[1],$this->LL_files_cache[$parts[0]]);
347 }
348 return $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.
349 }
350 }
351
352 /**
353 * Loading $TCA_DESCR[$table]['columns'] with content from locallang files as defined in $TCA_DESCR[$table]['refs']
354 * $TCA_DESCR is a global var
355 *
356 * @param string Table name found as key in global array $TCA_DESCR
357 * @return void
358 */
359 function loadSingleTableDescription($table) {
360 global $TCA_DESCR;
361
362 if (is_array($TCA_DESCR[$table])
363 && !isset($TCA_DESCR[$table]['columns'])
364 && 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]
365
366 // Init $TCA_DESCR for $table-key
367 $TCA_DESCR[$table]['columns']=array();
368
369 // Get local-lang for each file in $TCA_DESCR[$table]['refs'] as they are ordered.
370 foreach ($TCA_DESCR[$table]['refs'] as $llfile) {
371 $LOCAL_LANG = $this->includeLLFile($llfile,0,1);
372
373 // Traverse all keys
374 if (is_array($LOCAL_LANG['default'])) {
375 foreach($LOCAL_LANG['default'] as $lkey => $lVal) {
376
377 // exploding by '.':
378 // 0 => fieldname,
379 // 1 => type from (alttitle,description,details,syntax,image_descr,image,seeAlso),
380 // 2 => special instruction, see switch construct
381 $kParts = explode('.',$lkey);
382
383 // Detecting 'hidden' labels, converting to normal fieldname
384 if ($kParts[0]=='_') $kParts[0]='';
385 if (substr($kParts[0],0,1)=='_') {$kParts[0]=substr($kParts[0],1);}
386
387 // Add label:
388 switch((string)$kParts[2]) {
389 case '+': // adding
390 $TCA_DESCR[$table]['columns'][$kParts[0]][$kParts[1]].= chr(10).$lVal;
391 break;
392 default: // Substituting:
393 $TCA_DESCR[$table]['columns'][$kParts[0]][$kParts[1]] = $lVal;
394 break;
395 }
396 }
397 }
398 }
399 }
400 }
401
402 /**
403 * Includes locallang file (and possibly additional localized version if configured for)
404 *
405 * @param string $fileRef is a file-reference (see t3lib_div::getFileAbsFileName)
406 * @param boolean Setting in global variable $LOCAL_LANG (or returning the variable)
407 * @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.
408 * @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
409 */
410 function includeLLFile($fileRef,$setGlobal=1,$mergeLocalOntoDefault=0) {
411 // Configure for global flag:
412 if ($setGlobal) {
413 global $LOCAL_LANG;
414 }
415
416 // Get default file:
417 $file = t3lib_div::getFileAbsFileName($fileRef);
418 if (@is_file($file)) {
419 // Include main locallang file:
420 include($file);
421
422 // Localized addition?
423 $lFileRef = $this->localizedFileRef($fileRef);
424 if ($lFileRef && (string)$LOCAL_LANG[$this->lang]=='EXT') {
425 $lfile = t3lib_div::getFileAbsFileName($lFileRef);
426 if (@is_file($lfile)) {
427 // Include subfile:
428 include($lfile);
429 }
430 }
431
432 // Overriding file?
433 if (isset($GLOBALS['TYPO3_CONF_VARS']['BE']['XLLfile'][$fileRef])) {
434 $ORarray = $this->readLLfile($GLOBALS['TYPO3_CONF_VARS']['BE']['XLLfile'][$fileRef]);
435 $LOCAL_LANG = t3lib_div::array_merge_recursive_overrule($LOCAL_LANG,$ORarray);
436 }
437
438 // Merge local onto default:
439 if ($mergeLocalOntoDefault && strcmp($this->lang,'default') && is_array($LOCAL_LANG[$this->lang]) && is_array($LOCAL_LANG['default'])) {
440 $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...
441 unset($LOCAL_LANG[$this->lang]);
442 }
443 }
444
445 // Return value if not global is set.
446 if (!$setGlobal) {
447 return $LOCAL_LANG;
448 }
449 }
450
451 /**
452 * Includes a locallang file and returns the $LOCAL_LANG array found inside.
453 *
454 * @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.
455 * @return array Value of $LOCAL_LANG found in the included file. If that array is found it's returned. Otherwise an empty array
456 */
457 function readLLfile($fileRef) {
458 $file = t3lib_div::getFileAbsFileName($fileRef);
459 if (@is_file($file)) {
460 include($file);
461 }
462 return is_array($LOCAL_LANG)?$LOCAL_LANG:array();
463 }
464
465 /**
466 * Returns localized fileRef (.[langkey].php)
467 *
468 * @param string Filename/path of a 'locallang.php' file
469 * @return string Input filename with a '.[lang-key].php' ending added if $this->lang is not 'default'
470 */
471 function localizedFileRef($fileRef) {
472 if ($this->lang!='default' && substr($fileRef,-4)=='.php') {
473 return substr($fileRef,0,-4).'.'.$this->lang.'.php';
474 }
475 }
476 }
477
478 // Include extension to the template class?
479 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/lang/lang.php']) {
480 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/lang/lang.php']);
481 }
482 ?>