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