2 /***************************************************************
5 * (c) 1999-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
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.
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.
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.
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
28 * Contains the TYPO3 Backend Language class
30 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
34 * Contains the TYPO3 Backend Language class
36 * For detailed information about how localization is handled,
37 * please refer to the 'Inside TYPO3' document which descibes this.
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
42 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
45 * @see typo3/template.php, template
50 * This is set to the language that is currently running for the user
54 public $lang = 'default';
57 * Default charset in backend
61 public $charSet = 'utf-8';
64 * Array with alternative charsets for other languages.
65 * Moved to t3lib_cs, set internally from csConvObj!
69 public $charSetArray = array();
72 * This is the url to the TYPO3 manual
76 public $typo3_help_url= 'http://typo3.org/documentation/document-library/';
79 * If TRUE, will show the key/location of labels in the backend.
82 public $debugKey = FALSE;
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.
90 public $moduleLabels = array();
93 * Internal cache for read LL-files
97 public $LL_files_cache = array();
100 * Internal cache for ll-labels (filled as labels are requested)
104 public $LL_labels_cache = array();
107 * instance of the "t3lib_cs" class. May be used by any application.
114 * instance of the parser factory
115 * @var t3lib_l10n_Factory
117 public $parserFactory;
120 * Initializes the backend language.
121 * This is for example done in typo3/template.php with lines like these:
123 * require (PATH_typo3 . 'sysext/lang/lang.php');
124 * $LANG = t3lib_div::makeInstance('language');
125 * $LANG->init($GLOBALS['BE_USER']->uc['lang']);
127 * @throws RuntimeException
128 * @param string $lang The language key (two character string from backend users profile)
131 public function init($lang) {
133 // Initialize the conversion object:
134 $this->csConvObj
= t3lib_div
::makeInstance('t3lib_cs');
135 $this->charSetArray
= $this->csConvObj
->charSetArray
;
137 // Initialize the parser factory object
138 $this->parserFactory
= t3lib_div
::makeInstance('t3lib_l10n_Factory');
140 // Finding the requested language in this list based
141 // on the $lang key being inputted to this function.
142 $ls = explode('|', TYPO3_languages
);
144 // Language is found. Configure it:
145 if (in_array($lang, $ls)) {
146 // The current language key
148 if ($this->charSetArray
[$this->lang
]) {
149 // The charset if different from the default.
150 $this->charSet
= $this->charSetArray
[$this->lang
];
154 if ($GLOBALS['TYPO3_CONF_VARS']['BE']['lang']['debug']) {
155 $this->debugKey
= TRUE;
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->charSet
= $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'];
163 if ($this->charSet
!= 'utf-8' && !$this->csConvObj
->initCharset($this->charSet
)) {
164 throw new RuntimeException('Forced charset not found: The forced character set "'. $this->charSet
. '" was not found in t3lib/csconvtbl/', 1294587487);
170 * Gets the parser factory.
172 * @return t3lib_l10n_Factory
174 public function getParserFactory() {
175 return $this->parserFactory
;
179 * Adds labels and image references from the backend modules to the internal moduleLabels array
181 * @param array $arr Array with references to module labels, keys: ['labels']['tablabel'],
182 * ['labels']['tabdescr'], ['tabs']['tab']
183 * @param string $prefix Module name prefix
185 * @see t3lib_loadModules
187 public function addModuleLabels($arr, $prefix) {
188 if (is_array($arr)) {
189 foreach ($arr as $k => $larr) {
190 if (!isset($this->moduleLabels
[$k])) {
191 $this->moduleLabels
[$k] = array();
193 if (is_array($larr)) {
194 foreach ($larr as $l => $v) {
195 $this->moduleLabels
[$k][$prefix . $l] = $v;
203 * Will htmlspecialchar() the input string and before that any charset conversion
204 * will also have taken place if needed (see init())
205 * Used to pipe language labels through just before they are returned.
207 * @param string $lStr The string to process
208 * @param boolean $hsc If set, then the string is htmlspecialchars()'ed
209 * @return string The processed string
210 * @deprecated since TYPO3 4.6, will be removed in TYPO3 4.8 - Use htmlspecialchars() instead.
214 public function hscAndCharConv($lStr, $hsc) {
215 t3lib_div
::logDeprecatedFunction();
216 // labels returned from a locallang file used to be in the language of the charset.
217 // Since TYPO3 4.1 they are always in the charset of the BE.
219 return htmlspecialchars($lStr);
226 * Will convert the input strings special chars (all above 127) to entities.
227 * The string is expected to be encoded in the charset, $this->charSet
228 * This function is used to create strings that can be used in the Click Menu
229 * (Context Sensitive Menus). The reason is that the values that are dynamically
230 * written into the <div> layer is decoded as iso-8859-1 no matter what charset
231 * is used in the document otherwise (only MSIE, Mozilla is OK).
232 * So by converting we by-pass this problem.
234 * @param string $str Input string
235 * @return string Output string
238 public function makeEntities($str) {
239 // Convert string to UTF-8:
240 if ($this->charSet
!= 'utf-8') {
241 $str = $this->csConvObj
->utf8_encode($str, $this->charSet
);
244 // Convert string back again, but using the full entity conversion:
245 return $this->csConvObj
->utf8_to_entities($str);
249 * Converts the input string to a JavaScript function returning the same string, but charset-safe.
250 * Used for confirm and alert boxes where we must make sure that any string content
251 * does not break the script AND want to make sure the charset is preserved.
252 * Originally I used the JS function unescape() in combination with PHP function
253 * rawurlencode() in order to pass strings in a safe way. This could still be done
254 * for iso-8859-1 charsets but now I have applied the same method here for all charsets.
256 * @param string $str Input string, encoded with $this->charSet
257 * @return string Output string, a JavaScript function: "String.fromCharCode(......)"
260 public function JScharCode($str) {
262 // Convert string to UTF-8:
263 if ($this->charSet
!= 'utf-8') {
264 $str = $this->csConvObj
->utf8_encode($str, $this->charSet
);
267 // Convert the UTF-8 string into a array of char numbers:
268 $nArr = $this->csConvObj
->utf8_to_numberarray($str);
270 return 'String.fromCharCode(' . implode(',', $nArr) . ')';
274 * Debugs localization key.
276 * @param $value Value to debug
279 public function debugLL($value) {
280 return ($this->debugKey ?
'[' . $value . ']' : '');
284 * Returns the label with key $index form the globally loaded $LOCAL_LANG array.
285 * Mostly used from modules with only one LOCAL_LANG file loaded into the global space.
287 * @param string $index Label key
288 * @param boolean $hsc If set, the return value is htmlspecialchar'ed
292 public function getLL($index, $hsc = FALSE) {
293 // Get Local Language
294 if (isset($GLOBALS['LOCAL_LANG'][$this->lang
][$index][0]['target'])) {
295 $output = $GLOBALS['LOCAL_LANG'][$this->lang
][$index][0]['target'];
297 $output = $GLOBALS['LOCAL_LANG']['default'][$index][0]['target'];
300 $output = htmlspecialchars($output);
302 return $output . $this->debugLL($index);
306 * Works like ->getLL() but takes the $LOCAL_LANG array
307 * used as the second argument instead of using the global array.
309 * @param string $index Label key
310 * @param array $localLanguage $LOCAL_LANG array to get label key from
311 * @param boolean $hsc If set, the return value is htmlspecialchar'ed
315 public function getLLL($index, $localLanguage, $hsc = FALSE) {
316 // Get Local Language. Special handling for all extensions that
317 // read PHP LL files and pass arrays here directly.
318 $value = is_string($localLanguage[$this->lang
][$index]) ?
$localLanguage[$this->lang
][$index] : $localLanguage[$this->lang
][$index][0]['target'];
320 // Fallback to default language
321 if (trim($value) === '') {
322 $value = is_string($localLanguage['default'][$index]) ?
$localLanguage['default'][$index] : $localLanguage['default'][$index][0]['target'];
326 $value = htmlspecialchars($value);
328 return $value . $this->debugLL($index);
332 * splitLabel function
333 * Historically labels were exploded by '|' and each part would correspond
334 * to the translation of the language found at the same 'index' in the TYPO3_languages constant.
335 * Today all translations are based on $LOCAL_LANG variables.
336 * 'language-splitted' labels can therefore refer to a local-lang file + index instead!
337 * It's highly recommended to use the 'local_lang' method
338 * (and thereby it's highly deprecated to use 'language-splitted' label strings)
339 * Refer to 'Inside TYPO3' for more details
341 * @param string $input Label key/reference
342 * @param boolean $hsc If set, the return value is htmlspecialchar'ed
346 public function sL($input, $hsc = FALSE) {
348 if (!isset($this->LL_labels_cache
[$this->lang
][$input]) && substr($input, 0, 4) === 'LLL:') {
349 $restStr = trim(substr($input, 4));
352 // ll-file refered to is found in an extension.
353 if (!strcmp(substr($restStr, 0, 4), 'EXT:')) {
354 $restStr = trim(substr($restStr, 4));
357 $parts = explode(':', $restStr);
358 $parts[0] = $extPrfx . $parts[0];
360 // Getting data if not cached
361 if (!isset($this->LL_files_cache
[$parts[0]])) {
362 $this->LL_files_cache
[$parts[0]] = $this->readLLfile($parts[0]);
364 // If the current language is found in another file, load that as well:
365 $lFileRef = $this->localizedFileRef($parts[0]);
366 if ($lFileRef && is_string($this->LL_files_cache
[$parts[0]][$this->lang
])
367 && $this->LL_files_cache
[$parts[0]][$this->lang
] == 'EXT') {
368 $tempLL = $this->readLLfile($lFileRef);
369 $this->LL_files_cache
[$parts[0]][$this->lang
] = $tempLL[$this->lang
];
372 $this->LL_labels_cache
[$this->lang
][$input] = $this->getLLL($parts[1], $this->LL_files_cache
[$parts[0]]);
374 // For the cached output charset conversion has already happened!
375 // So perform HSC right here.
376 if (isset($this->LL_labels_cache
[$this->lang
][$input])) {
377 $output = $this->LL_labels_cache
[$this->lang
][$input];
379 // Use a constant non-localizable label
384 $output = t3lib_div
::deHSCentities(htmlspecialchars($output));
386 return $output . $this->debugLL($input);
390 * Loading $TCA_DESCR[$table]['columns'] with content from locallang files
391 * as defined in $TCA_DESCR[$table]['refs']
392 * $TCA_DESCR is a global var
394 * @param string $table Table name found as key in global array $TCA_DESCR
398 public function loadSingleTableDescription($table) {
400 // First the 'table' cannot already be loaded in [columns]
401 // and secondly there must be a references to locallang files available in [refs]
402 if (is_array($GLOBALS['TCA_DESCR'][$table])
403 && !isset($GLOBALS['TCA_DESCR'][$table]['columns'])
404 && is_array($GLOBALS['TCA_DESCR'][$table]['refs'])) {
406 // Init $TCA_DESCR for $table-key
407 $GLOBALS['TCA_DESCR'][$table]['columns'] = array();
409 // Get local-lang for each file in $TCA_DESCR[$table]['refs'] as they are ordered.
410 foreach ($GLOBALS['TCA_DESCR'][$table]['refs'] as $llfile) {
411 $localLanguage = $this->includeLLFile($llfile, 0, 1);
414 if (is_array($localLanguage['default'])) {
415 foreach ($localLanguage['default'] as $lkey => $lVal) {
421 // n+1 => type from (alttitle, description, details, syntax, image_descr,image,seeAlso),
422 // n+2 => special instruction, if any
423 $keyParts = explode('.', $lkey);
424 $keyPartsCount = count($keyParts);
425 // Check if last part is special instruction
426 // Only "+" is currently supported
427 $specialInstruction = ($keyParts[$keyPartsCount - 1] == '+') ?
TRUE : FALSE;
428 if ($specialInstruction) {
429 array_pop($keyParts);
432 // If there are more than 2 parts, get the type from the last part
433 // and merge back the other parts with a dot (.)
434 // Otherwise just get type and field name straightaway
435 if ($keyPartsCount > 2) {
436 $type = array_pop($keyParts);
437 $fieldName = implode('.', $keyParts);
439 $fieldName = $keyParts[0];
440 $type = $keyParts[1];
443 // Detecting 'hidden' labels, converting to normal fieldname
444 if ($fieldName == '_') {
447 if (substr($fieldName, 0, 1) == '_') {
448 $fieldName = substr($fieldName, 1);
452 if ($specialInstruction) {
453 $GLOBALS['TCA_DESCR'][$table]['columns'][$fieldName][$type] .= LF
. $lVal[0]['source'];
456 $GLOBALS['TCA_DESCR'][$table]['columns'][$fieldName][$type] = $lVal[0]['source'];
465 * Includes locallang file (and possibly additional localized version if configured for)
466 * Read language labels will be merged with $LOCAL_LANG (if $setGlobal = TRUE).
468 * @param string $fileRef $fileRef is a file-reference (see t3lib_div::getFileAbsFileName)
469 * @param boolean $setGlobal Setting in global variable $LOCAL_LANG (or returning the variable)
470 * @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.
471 * @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
474 public function includeLLFile($fileRef, $setGlobal = TRUE, $mergeLocalOntoDefault = FALSE) {
476 $globalLanguage = array();
479 $localLanguage = $this->readLLfile($fileRef);
481 if (is_array($localLanguage) && count($localLanguage)) {
483 // it depends on, whether we should return the result or set it in the global $LOCAL_LANG array
485 $globalLanguage = t3lib_div
::array_merge_recursive_overrule((array)$GLOBALS['LOCAL_LANG'], $localLanguage);
487 $globalLanguage = $localLanguage;
490 // Localized addition?
491 $lFileRef = $this->localizedFileRef($fileRef);
492 if ($lFileRef && (string)$globalLanguage[$this->lang
] == 'EXT') {
493 $localLanguage = $this->readLLfile($lFileRef);
494 $globalLanguage = t3lib_div
::array_merge_recursive_overrule($globalLanguage, $localLanguage);
497 // Merge local onto default
498 if ($mergeLocalOntoDefault && $this->lang
!== 'default'
499 && is_array($globalLanguage[$this->lang
]) && is_array($globalLanguage['default'])) {
500 // array_merge can be used so far the keys are not
501 // numeric - which we assume they are not...
502 $globalLanguage['default'] = array_merge($globalLanguage['default'], $globalLanguage[$this->lang
]);
503 unset($globalLanguage[$this->lang
]);
507 // Return value if not global is set.
509 return $globalLanguage;
511 $GLOBALS['LOCAL_LANG'] = $globalLanguage;
516 * Includes a locallang file and returns the $LOCAL_LANG array found inside.
518 * @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.
519 * @return array Value of $LOCAL_LANG found in the included file. If that array is found it's returned. Otherwise an empty array
522 protected function readLLfile($fileRef) {
523 return t3lib_div
::readLLfile($fileRef, $this->lang
, $this->charSet
);
527 * Returns localized fileRef (.[langkey].php)
529 * @param string $fileRef Filename/path of a 'locallang.php' file
530 * @return string Input filename with a '.[lang-key].php' ending added if $this->lang is not 'default'
533 protected function localizedFileRef($fileRef) {
534 if ($this->lang
!= 'default' && substr($fileRef, -4) == '.php') {
535 return substr($fileRef, 0, -4) . '.' . $this->lang
. '.php';
542 * @param string $index
543 * @param string $value
544 * @param boolean $overrideDefault Overrides default language
547 public function overrideLL($index, $value, $overrideDefault = TRUE) {
548 if (isset($GLOBALS['LOCAL_LANG']) === FALSE) {
549 $GLOBALS['LOCAL_LANG'] = array();
552 $GLOBALS['LOCAL_LANG'][$this->lang
][$index][0]['target'] = $value;
554 if ($overrideDefault) {
555 $GLOBALS['LOCAL_LANG']['default'][$index][0]['target'] = $value;
561 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE
]['XCLASS']['ext/lang/lang.php'])) {
562 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE
]['XCLASS']['ext/lang/lang.php']);