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