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