[BUGFIX] Locales dependency is not taken into account
[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 /**
34 * Contains the TYPO3 Backend Language class
35 *
36 * For detailed information about how localization is handled,
37 * please refer to the 'Inside TYPO3' document which descibes this.
38 *
39 * This class is normally instantiated as the global variable $LANG in typo3/template.php
40 * It's only available in the backend and under certain circumstances in the frontend
41 *
42 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
43 * @package TYPO3
44 * @subpackage core
45 * @see typo3/template.php, template
46 */
47 class language {
48
49 /**
50 * This is set to the language that is currently running for the user
51 *
52 * @var string
53 */
54 public $lang = 'default';
55
56 /**
57 * Default charset in backend
58 *
59 * @var string
60 */
61 public $charSet = 'utf-8';
62
63 /**
64 * Array with alternative charsets for other languages.
65 * Moved to t3lib_cs, set internally from csConvObj!
66 *
67 * @var array
68 */
69 public $charSetArray = array();
70
71 /**
72 * This is the url to the TYPO3 manual
73 *
74 * @var string
75 */
76 public $typo3_help_url= 'http://typo3.org/documentation/document-library/';
77
78 /**
79 * If TRUE, will show the key/location of labels in the backend.
80 * @var bool
81 */
82 public $debugKey = FALSE;
83
84 /**
85 * Can contain labels and image references from the backend modules.
86 * Relies on t3lib_loadmodules to initialize modules after a global instance of $LANG has been created.
87 *
88 * @var array
89 */
90 public $moduleLabels = array();
91
92 /**
93 * Internal cache for read LL-files
94 *
95 * @var array
96 */
97 public $LL_files_cache = array();
98
99 /**
100 * Internal cache for ll-labels (filled as labels are requested)
101 *
102 * @var array
103 */
104 public $LL_labels_cache = array();
105
106 /**
107 * instance of the "t3lib_cs" class. May be used by any application.
108 *
109 * @var t3lib_cs
110 */
111 public $csConvObj;
112
113 /**
114 * instance of the parser factory
115 * @var t3lib_l10n_Factory
116 */
117 public $parserFactory;
118
119 /**
120 * List of language dependencies for actual language. This is used for local variants of a language
121 * that depend on their "main" language, like Brazilian Portugese or Canadian French.
122 *
123 * @var array
124 */
125 protected $languageDependencies = array();
126
127 /**
128 * Initializes the backend language.
129 * This is for example done in typo3/template.php with lines like these:
130 *
131 * require (PATH_typo3 . 'sysext/lang/lang.php');
132 * $LANG = t3lib_div::makeInstance('language');
133 * $LANG->init($GLOBALS['BE_USER']->uc['lang']);
134 *
135 * @throws RuntimeException
136 * @param string $lang The language key (two character string from backend users profile)
137 * @return void
138 */
139 public function init($lang) {
140
141 // Initialize the conversion object:
142 $this->csConvObj = t3lib_div::makeInstance('t3lib_cs');
143 $this->charSetArray = $this->csConvObj->charSetArray;
144
145 // Initialize the parser factory object
146 $this->parserFactory = t3lib_div::makeInstance('t3lib_l10n_Factory');
147
148 // Finding the requested language in this list based
149 // on the $lang key being inputted to this function.
150 /** @var $locales t3lib_l10n_Locales */
151 $locales = t3lib_div::makeInstance('t3lib_l10n_Locales');
152
153 // Language is found. Configure it:
154 if (in_array($lang, $locales->getLocales())) {
155 // The current language key
156 $this->lang = $lang;
157 if ($this->charSetArray[$this->lang]) {
158 // The charset if different from the default.
159 $this->charSet = $this->charSetArray[$this->lang];
160 }
161
162 $this->languageDependencies[] = $this->lang;
163 foreach ($locales->getLocaleDependencies($this->lang) as $language) {
164 $this->languageDependencies[] = $language;
165 }
166 }
167
168 if ($GLOBALS['TYPO3_CONF_VARS']['BE']['lang']['debug']) {
169 $this->debugKey = TRUE;
170 }
171
172 // If a forced charset is used and different from the charset otherwise used:
173 if ($GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'] && $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'] != $this->charSet) {
174 // Set the forced charset:
175 $this->charSet = $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'];
176
177 if ($this->charSet != 'utf-8' && !$this->csConvObj->initCharset($this->charSet)) {
178 throw new RuntimeException('Forced charset not found: The forced character set "'. $this->charSet . '" was not found in t3lib/csconvtbl/', 1294587487);
179 }
180 }
181 }
182
183 /**
184 * Gets the parser factory.
185 *
186 * @return t3lib_l10n_Factory
187 */
188 public function getParserFactory() {
189 return $this->parserFactory;
190 }
191
192 /**
193 * Adds labels and image references from the backend modules to the internal moduleLabels array
194 *
195 * @param array $arr Array with references to module labels, keys: ['labels']['tablabel'],
196 * ['labels']['tabdescr'], ['tabs']['tab']
197 * @param string $prefix Module name prefix
198 * @return void
199 * @see t3lib_loadModules
200 */
201 public function addModuleLabels($arr, $prefix) {
202 if (is_array($arr)) {
203 foreach ($arr as $k => $larr) {
204 if (!isset($this->moduleLabels[$k])) {
205 $this->moduleLabels[$k] = array();
206 }
207 if (is_array($larr)) {
208 foreach ($larr as $l => $v) {
209 $this->moduleLabels[$k][$prefix . $l] = $v;
210 }
211 }
212 }
213 }
214 }
215
216 /**
217 * Will htmlspecialchar() the input string and before that any charset conversion
218 * will also have taken place if needed (see init())
219 * Used to pipe language labels through just before they are returned.
220 *
221 * @param string $lStr The string to process
222 * @param boolean $hsc If set, then the string is htmlspecialchars()'ed
223 * @return string The processed string
224 * @deprecated since TYPO3 4.6, will be removed in TYPO3 4.8 - Use htmlspecialchars() instead.
225 * @see init()
226 * @access public
227 */
228 public function hscAndCharConv($lStr, $hsc) {
229 t3lib_div::logDeprecatedFunction();
230 // labels returned from a locallang file used to be in the language of the charset.
231 // Since TYPO3 4.1 they are always in the charset of the BE.
232 if ($hsc) {
233 return htmlspecialchars($lStr);
234 } else {
235 return $lStr;
236 }
237 }
238
239 /**
240 * Will convert the input strings special chars (all above 127) to entities.
241 * The string is expected to be encoded in the charset, $this->charSet
242 * This function is used to create strings that can be used in the Click Menu
243 * (Context Sensitive Menus). The reason is that the values that are dynamically
244 * written into the <div> layer is decoded as iso-8859-1 no matter what charset
245 * is used in the document otherwise (only MSIE, Mozilla is OK).
246 * So by converting we by-pass this problem.
247 *
248 * @param string $str Input string
249 * @return string Output string
250 * @access public
251 */
252 public function makeEntities($str) {
253 // Convert string to UTF-8:
254 if ($this->charSet != 'utf-8') {
255 $str = $this->csConvObj->utf8_encode($str, $this->charSet);
256 }
257
258 // Convert string back again, but using the full entity conversion:
259 return $this->csConvObj->utf8_to_entities($str);
260 }
261
262 /**
263 * Converts the input string to a JavaScript function returning the same string, but charset-safe.
264 * Used for confirm and alert boxes where we must make sure that any string content
265 * does not break the script AND want to make sure the charset is preserved.
266 * Originally I used the JS function unescape() in combination with PHP function
267 * rawurlencode() in order to pass strings in a safe way. This could still be done
268 * for iso-8859-1 charsets but now I have applied the same method here for all charsets.
269 *
270 * @param string $str Input string, encoded with $this->charSet
271 * @return string Output string, a JavaScript function: "String.fromCharCode(......)"
272 * @access public
273 */
274 public function JScharCode($str) {
275
276 // Convert string to UTF-8:
277 if ($this->charSet != 'utf-8') {
278 $str = $this->csConvObj->utf8_encode($str, $this->charSet);
279 }
280
281 // Convert the UTF-8 string into a array of char numbers:
282 $nArr = $this->csConvObj->utf8_to_numberarray($str);
283
284 return 'String.fromCharCode(' . implode(',', $nArr) . ')';
285 }
286
287 /**
288 * Debugs localization key.
289 *
290 * @param $value Value to debug
291 * @return string
292 */
293 public function debugLL($value) {
294 return ($this->debugKey ? '[' . $value . ']' : '');
295 }
296
297 /**
298 * Returns the label with key $index form the globally loaded $LOCAL_LANG array.
299 * Mostly used from modules with only one LOCAL_LANG file loaded into the global space.
300 *
301 * @param string $index Label key
302 * @param boolean $hsc If set, the return value is htmlspecialchar'ed
303 * @return string
304 * @access public
305 */
306 public function getLL($index, $hsc = FALSE) {
307 // Get Local Language
308 if (isset($GLOBALS['LOCAL_LANG'][$this->lang][$index][0]['target'])) {
309 $output = $GLOBALS['LOCAL_LANG'][$this->lang][$index][0]['target'];
310 } else {
311 $output = $GLOBALS['LOCAL_LANG']['default'][$index][0]['target'];
312 }
313 if ($hsc) {
314 $output = htmlspecialchars($output);
315 }
316 return $output . $this->debugLL($index);
317 }
318
319 /**
320 * Works like ->getLL() but takes the $LOCAL_LANG array
321 * used as the second argument instead of using the global array.
322 *
323 * @param string $index Label key
324 * @param array $localLanguage $LOCAL_LANG array to get label key from
325 * @param boolean $hsc If set, the return value is htmlspecialchar'ed
326 * @return string
327 * @access public
328 */
329 public function getLLL($index, $localLanguage, $hsc = FALSE) {
330 // Get Local Language. Special handling for all extensions that
331 // read PHP LL files and pass arrays here directly.
332 $value = is_string($localLanguage[$this->lang][$index]) ? $localLanguage[$this->lang][$index] : $localLanguage[$this->lang][$index][0]['target'];
333
334 // Fallback to default language
335 if (trim($value) === '') {
336 $value = is_string($localLanguage['default'][$index]) ? $localLanguage['default'][$index] : $localLanguage['default'][$index][0]['target'];
337 }
338
339 if ($hsc) {
340 $value = htmlspecialchars($value);
341 }
342 return $value . $this->debugLL($index);
343 }
344
345 /**
346 * splitLabel function
347 * Historically labels were exploded by '|' and each part would correspond
348 * to the translation of the language found at the same 'index' in the TYPO3_languages constant.
349 * Today all translations are based on $LOCAL_LANG variables.
350 * 'language-splitted' labels can therefore refer to a local-lang file + index instead!
351 * It's highly recommended to use the 'local_lang' method
352 * (and thereby it's highly deprecated to use 'language-splitted' label strings)
353 * Refer to 'Inside TYPO3' for more details
354 *
355 * @param string $input Label key/reference
356 * @param boolean $hsc If set, the return value is htmlspecialchar'ed
357 * @return string
358 * @access public
359 */
360 public function sL($input, $hsc = FALSE) {
361 // If cached label
362 if (!isset($this->LL_labels_cache[$this->lang][$input]) && substr($input, 0, 4) === 'LLL:') {
363 $restStr = trim(substr($input, 4));
364 $extPrfx = '';
365
366 // ll-file refered to is found in an extension.
367 if (!strcmp(substr($restStr, 0, 4), 'EXT:')) {
368 $restStr = trim(substr($restStr, 4));
369 $extPrfx = 'EXT:';
370 }
371 $parts = explode(':', $restStr);
372 $parts[0] = $extPrfx . $parts[0];
373
374 // Getting data if not cached
375 if (!isset($this->LL_files_cache[$parts[0]])) {
376 $this->LL_files_cache[$parts[0]] = $this->readLLfile($parts[0]);
377
378 // If the current language is found in another file, load that as well:
379 $lFileRef = $this->localizedFileRef($parts[0]);
380 if ($lFileRef && is_string($this->LL_files_cache[$parts[0]][$this->lang])
381 && $this->LL_files_cache[$parts[0]][$this->lang] == 'EXT') {
382 $tempLL = $this->readLLfile($lFileRef);
383 $this->LL_files_cache[$parts[0]][$this->lang] = $tempLL[$this->lang];
384 }
385 }
386 $this->LL_labels_cache[$this->lang][$input] = $this->getLLL($parts[1], $this->LL_files_cache[$parts[0]]);
387 }
388 // For the cached output charset conversion has already happened!
389 // So perform HSC right here.
390 if (isset($this->LL_labels_cache[$this->lang][$input])) {
391 $output = $this->LL_labels_cache[$this->lang][$input];
392 } else {
393 // Use a constant non-localizable label
394 $output = $input;
395 }
396
397 if ($hsc) {
398 $output = t3lib_div::deHSCentities(htmlspecialchars($output));
399 }
400 return $output . $this->debugLL($input);
401 }
402
403 /**
404 * Loading $TCA_DESCR[$table]['columns'] with content from locallang files
405 * as defined in $TCA_DESCR[$table]['refs']
406 * $TCA_DESCR is a global var
407 *
408 * @param string $table Table name found as key in global array $TCA_DESCR
409 * @return void
410 * @access public
411 */
412 public function loadSingleTableDescription($table) {
413
414 // First the 'table' cannot already be loaded in [columns]
415 // and secondly there must be a references to locallang files available in [refs]
416 if (is_array($GLOBALS['TCA_DESCR'][$table])
417 && !isset($GLOBALS['TCA_DESCR'][$table]['columns'])
418 && is_array($GLOBALS['TCA_DESCR'][$table]['refs'])) {
419
420 // Init $TCA_DESCR for $table-key
421 $GLOBALS['TCA_DESCR'][$table]['columns'] = array();
422
423 // Get local-lang for each file in $TCA_DESCR[$table]['refs'] as they are ordered.
424 foreach ($GLOBALS['TCA_DESCR'][$table]['refs'] as $llfile) {
425 $localLanguage = $this->includeLLFile($llfile, 0, 1);
426
427 // Traverse all keys
428 if (is_array($localLanguage['default'])) {
429 foreach ($localLanguage['default'] as $lkey => $lVal) {
430 $type = '';
431 $fieldName = '';
432
433 // Exploding by '.':
434 // 0-n => fieldname,
435 // n+1 => type from (alttitle, description, details, syntax, image_descr,image,seeAlso),
436 // n+2 => special instruction, if any
437 $keyParts = explode('.', $lkey);
438 $keyPartsCount = count($keyParts);
439 // Check if last part is special instruction
440 // Only "+" is currently supported
441 $specialInstruction = ($keyParts[$keyPartsCount - 1] == '+') ? TRUE : FALSE;
442 if ($specialInstruction) {
443 array_pop($keyParts);
444 }
445
446 // If there are more than 2 parts, get the type from the last part
447 // and merge back the other parts with a dot (.)
448 // Otherwise just get type and field name straightaway
449 if ($keyPartsCount > 2) {
450 $type = array_pop($keyParts);
451 $fieldName = implode('.', $keyParts);
452 } else {
453 $fieldName = $keyParts[0];
454 $type = $keyParts[1];
455 }
456
457 // Detecting 'hidden' labels, converting to normal fieldname
458 if ($fieldName == '_') {
459 $fieldName = '';
460 }
461 if (substr($fieldName, 0, 1) == '_') {
462 $fieldName = substr($fieldName, 1);
463 }
464
465 // Append label
466 $label = $lVal[0]['target'] ?: $lVal[0]['source'];
467 if ($specialInstruction) {
468 $GLOBALS['TCA_DESCR'][$table]['columns'][$fieldName][$type] .= LF . $label;
469 } else {
470 // Substitute label
471 $GLOBALS['TCA_DESCR'][$table]['columns'][$fieldName][$type] = $label;
472 }
473 }
474 }
475 }
476 }
477 }
478
479 /**
480 * Includes locallang file (and possibly additional localized version if configured for)
481 * Read language labels will be merged with $LOCAL_LANG (if $setGlobal = TRUE).
482 *
483 * @param string $fileRef $fileRef is a file-reference (see t3lib_div::getFileAbsFileName)
484 * @param boolean $setGlobal Setting in global variable $LOCAL_LANG (or returning the variable)
485 * @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.
486 * @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
487 * @access public
488 */
489 public function includeLLFile($fileRef, $setGlobal = TRUE, $mergeLocalOntoDefault = FALSE) {
490
491 $globalLanguage = array();
492
493 // Get default file
494 $localLanguage = $this->readLLfile($fileRef);
495
496 if (is_array($localLanguage) && count($localLanguage)) {
497
498 // it depends on, whether we should return the result or set it in the global $LOCAL_LANG array
499 if ($setGlobal) {
500 $globalLanguage = t3lib_div::array_merge_recursive_overrule((array)$GLOBALS['LOCAL_LANG'], $localLanguage);
501 } else {
502 $globalLanguage = $localLanguage;
503 }
504
505 // Localized addition?
506 $lFileRef = $this->localizedFileRef($fileRef);
507 if ($lFileRef && (string)$globalLanguage[$this->lang] == 'EXT') {
508 $localLanguage = $this->readLLfile($lFileRef);
509 $globalLanguage = t3lib_div::array_merge_recursive_overrule($globalLanguage, $localLanguage);
510 }
511
512 // Merge local onto default
513 if ($mergeLocalOntoDefault && $this->lang !== 'default'
514 && is_array($globalLanguage[$this->lang]) && is_array($globalLanguage['default'])) {
515 // array_merge can be used so far the keys are not
516 // numeric - which we assume they are not...
517 $globalLanguage['default'] = array_merge($globalLanguage['default'], $globalLanguage[$this->lang]);
518 unset($globalLanguage[$this->lang]);
519 }
520 }
521
522 // Return value if not global is set.
523 if (!$setGlobal) {
524 return $globalLanguage;
525 } else {
526 $GLOBALS['LOCAL_LANG'] = $globalLanguage;
527 }
528 }
529
530 /**
531 * Includes a locallang file and returns the $LOCAL_LANG array found inside.
532 *
533 * @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.
534 * @return array Value of $LOCAL_LANG found in the included file. If that array is found it's returned. Otherwise an empty array
535 * @access private
536 */
537 protected function readLLfile($fileRef) {
538 if ($this->lang !== 'default') {
539 $languages = array_reverse($this->languageDependencies);
540 } else {
541 $languages = array('default');
542 }
543
544 $localLanguage = array();
545 foreach ($languages as $language) {
546 $tempLL = t3lib_div::readLLfile($fileRef, $language, $this->charSet);
547 $localLanguage['default'] = $tempLL['default'];
548 if (!isset($localLanguage[$this->lang])) {
549 $localLanguage[$this->lang] = $localLanguage['default'];
550 }
551 if ($this->lang !== 'default') {
552 // Merge current language labels onto labels from previous language
553 // This way we have a labels with fall back applied
554 $localLanguage[$this->lang] = t3lib_div::array_merge_recursive_overrule($localLanguage[$this->lang], $tempLL[$language], FALSE, FALSE);
555 }
556 }
557
558 return $localLanguage;
559 }
560
561 /**
562 * Returns localized fileRef (.[langkey].php)
563 *
564 * @param string $fileRef Filename/path of a 'locallang.php' file
565 * @return string Input filename with a '.[lang-key].php' ending added if $this->lang is not 'default'
566 * @access private
567 */
568 protected function localizedFileRef($fileRef) {
569 if ($this->lang != 'default' && substr($fileRef, -4) == '.php') {
570 return substr($fileRef, 0, -4) . '.' . $this->lang . '.php';
571 }
572 }
573
574 /**
575 * Overrides a label.
576 *
577 * @param string $index
578 * @param string $value
579 * @param boolean $overrideDefault Overrides default language
580 * @return void
581 */
582 public function overrideLL($index, $value, $overrideDefault = TRUE) {
583 if (isset($GLOBALS['LOCAL_LANG']) === FALSE) {
584 $GLOBALS['LOCAL_LANG'] = array();
585 }
586
587 $GLOBALS['LOCAL_LANG'][$this->lang][$index][0]['target'] = $value;
588
589 if ($overrideDefault) {
590 $GLOBALS['LOCAL_LANG']['default'][$index][0]['target'] = $value;
591 }
592 }
593 }
594
595
596 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/lang/lang.php'])) {
597 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/lang/lang.php']);
598 }
599
600 ?>