TYPO3  7.6
LanguageService.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Lang;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
20 
30 {
36  public $lang = 'default';
37 
43  public $charSet = 'utf-8';
44 
51  public $charSetArray = array();
52 
58  public $typo3_help_url = 'http://typo3.org/documentation/document-library/';
59 
65  public $debugKey = false;
66 
73  public $moduleLabels = array();
74 
80  public $LL_files_cache = array();
81 
87  public $LL_labels_cache = array();
88 
94  public $csConvObj;
95 
102 
109  protected $languageDependencies = array();
110 
121  public function init($lang)
122  {
123  // Initialize the conversion object:
124  $this->csConvObj = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Charset\CharsetConverter::class);
125  $this->charSetArray = $this->csConvObj->charSetArray;
126  // Initialize the parser factory object
127  $this->parserFactory = GeneralUtility::makeInstance(LocalizationFactory::class);
128  // Find the requested language in this list based
129  // on the $lang key being inputted to this function.
131  $locales = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Localization\Locales::class);
132  // Language is found. Configure it:
133  if (in_array($lang, $locales->getLocales())) {
134  // The current language key
135  $this->lang = $lang;
136  $this->languageDependencies[] = $this->lang;
137  foreach ($locales->getLocaleDependencies($this->lang) as $language) {
138  $this->languageDependencies[] = $language;
139  }
140  }
141  if ($GLOBALS['TYPO3_CONF_VARS']['BE']['lang']['debug']) {
142  $this->debugKey = true;
143  }
144  }
145 
151  public function getParserFactory()
152  {
153  return $this->parserFactory;
154  }
155 
164  public function addModuleLabels($arr, $prefix)
165  {
166  if (is_array($arr)) {
167  foreach ($arr as $k => $larr) {
168  if (!isset($this->moduleLabels[$k])) {
169  $this->moduleLabels[$k] = array();
170  }
171  if (is_array($larr)) {
172  foreach ($larr as $l => $v) {
173  $this->moduleLabels[$k][$prefix . $l] = $v;
174  }
175  }
176  }
177  }
178  }
179 
192  public function makeEntities($str)
193  {
194  // Convert string back again, but using the full entity conversion:
195  return $this->csConvObj->utf8_to_entities($str);
196  }
197 
204  public function debugLL($value)
205  {
206  return $this->debugKey ? '[' . $value . ']' : '';
207  }
208 
217  public function getLL($index, $hsc = false)
218  {
219  return $this->getLLL($index, $GLOBALS['LOCAL_LANG'], $hsc);
220  }
221 
230  public function getLLL($index, $localLanguage, $hsc = false)
231  {
232  // Get Local Language. Special handling for all extensions that
233  // read PHP LL files and pass arrays here directly.
234  if (isset($localLanguage[$this->lang][$index])) {
235  $value = is_string($localLanguage[$this->lang][$index])
236  ? $localLanguage[$this->lang][$index]
237  : $localLanguage[$this->lang][$index][0]['target'];
238  } elseif (isset($localLanguage['default'][$index])) {
239  $value = is_string($localLanguage['default'][$index])
240  ? $localLanguage['default'][$index]
241  : $localLanguage['default'][$index][0]['target'];
242  } else {
243  $value = '';
244  }
245  if ($hsc) {
246  $value = htmlspecialchars($value);
247  }
248  return $value . $this->debugLL($index);
249  }
250 
262  public function sL($input, $hsc = false)
263  {
264  $identifier = $input . '_' . (int)$hsc . '_' . (int)$this->debugKey;
265  if (isset($this->LL_labels_cache[$this->lang][$identifier])) {
266  return $this->LL_labels_cache[$this->lang][$identifier];
267  }
268  if (strpos($input, 'LLL:') === 0) {
269  $restStr = trim(substr($input, 4));
270  $extPrfx = '';
271  // ll-file referred to is found in an extension.
272  if (strpos($restStr, 'EXT:') === 0) {
273  $restStr = trim(substr($restStr, 4));
274  $extPrfx = 'EXT:';
275  }
276  $parts = explode(':', $restStr);
277  $parts[0] = $extPrfx . $parts[0];
278  // Getting data if not cached
279  if (!isset($this->LL_files_cache[$parts[0]])) {
280  $this->LL_files_cache[$parts[0]] = $this->readLLfile($parts[0]);
281  // If the current language is found in another file, load that as well:
282  $lFileRef = $this->localizedFileRef($parts[0]);
283  if ($lFileRef && $this->LL_files_cache[$parts[0]][$this->lang] === 'EXT') {
284  $tempLL = $this->readLLfile($lFileRef);
285  $this->LL_files_cache[$parts[0]][$this->lang] = $tempLL[$this->lang];
286  }
287  }
288  $output = $this->getLLL($parts[1], $this->LL_files_cache[$parts[0]]);
289  } else {
290  // Use a constant non-localizable label
291  $output = $input;
292  }
293  if ($hsc) {
294  $output = htmlspecialchars($output, ENT_COMPAT, 'UTF-8', false);
295  }
296  $output .= $this->debugLL($input);
297  $this->LL_labels_cache[$this->lang][$identifier] = $output;
298  return $output;
299  }
300 
309  public function loadSingleTableDescription($table)
310  {
311  // First the 'table' cannot already be loaded in [columns]
312  // and secondly there must be a references to locallang files available in [refs]
313  if (is_array($GLOBALS['TCA_DESCR'][$table]) && !isset($GLOBALS['TCA_DESCR'][$table]['columns']) && is_array($GLOBALS['TCA_DESCR'][$table]['refs'])) {
314  // Init $TCA_DESCR for $table-key
315  $GLOBALS['TCA_DESCR'][$table]['columns'] = array();
316  // Get local-lang for each file in $TCA_DESCR[$table]['refs'] as they are ordered.
317  foreach ($GLOBALS['TCA_DESCR'][$table]['refs'] as $llfile) {
318  $localLanguage = $this->includeLLFile($llfile, 0, 1);
319  // Traverse all keys
320  if (is_array($localLanguage['default'])) {
321  foreach ($localLanguage['default'] as $lkey => $lVal) {
322  // Exploding by '.':
323  // 0-n => fieldname,
324  // n+1 => type from (alttitle, description, details, syntax, image_descr,image,seeAlso),
325  // n+2 => special instruction, if any
326  $keyParts = explode('.', $lkey);
327  $keyPartsCount = count($keyParts);
328  // Check if last part is special instruction
329  // Only "+" is currently supported
330  $specialInstruction = $keyParts[$keyPartsCount - 1] === '+';
331  if ($specialInstruction) {
332  array_pop($keyParts);
333  }
334  // If there are more than 2 parts, get the type from the last part
335  // and merge back the other parts with a dot (.)
336  // Otherwise just get type and field name straightaway
337  if ($keyPartsCount > 2) {
338  $type = array_pop($keyParts);
339  $fieldName = implode('.', $keyParts);
340  } else {
341  $fieldName = $keyParts[0];
342  $type = $keyParts[1];
343  }
344  // Detecting 'hidden' labels, converting to normal fieldname
345  if ($fieldName === '_') {
346  $fieldName = '';
347  }
348  if ($fieldName !== '' && $fieldName[0] === '_') {
349  $fieldName = substr($fieldName, 1);
350  }
351  // Append label
352  $label = $lVal[0]['target'] ? :
353  $lVal[0]['source'];
354  if ($specialInstruction) {
355  $GLOBALS['TCA_DESCR'][$table]['columns'][$fieldName][$type] .= LF . $label;
356  } else {
357  // Substitute label
358  $GLOBALS['TCA_DESCR'][$table]['columns'][$fieldName][$type] = $label;
359  }
360  }
361  }
362  }
363  }
364  }
365 
375  public function includeLLFile($fileRef, $setGlobal = true, $mergeLocalOntoDefault = false)
376  {
377  $globalLanguage = array();
378  // Get default file
379  $localLanguage = $this->readLLfile($fileRef);
380  if (is_array($localLanguage) && !empty($localLanguage)) {
381  // it depends on, whether we should return the result or set it in the global $LOCAL_LANG array
382  if ($setGlobal) {
383  $globalLanguage = (array)$GLOBALS['LOCAL_LANG'];
384  ArrayUtility::mergeRecursiveWithOverrule($globalLanguage, $localLanguage);
385  } else {
386  $globalLanguage = $localLanguage;
387  }
388  // Localized addition?
389  $lFileRef = $this->localizedFileRef($fileRef);
390  if ($lFileRef && (string)$globalLanguage[$this->lang] === 'EXT') {
391  $localLanguage = $this->readLLfile($lFileRef);
392  ArrayUtility::mergeRecursiveWithOverrule($globalLanguage, $localLanguage);
393  }
394  // Merge local onto default
395  if ($mergeLocalOntoDefault && $this->lang !== 'default' && is_array($globalLanguage[$this->lang]) && is_array($globalLanguage['default'])) {
396  // array_merge can be used so far the keys are not
397  // numeric - which we assume they are not...
398  $globalLanguage['default'] = array_merge($globalLanguage['default'], $globalLanguage[$this->lang]);
399  unset($globalLanguage[$this->lang]);
400  }
401  }
402  // Return value if not global is set.
403  if (!$setGlobal) {
404  return $globalLanguage;
405  } else {
406  $GLOBALS['LOCAL_LANG'] = $globalLanguage;
407  return null;
408  }
409  }
410 
417  protected function readLLfile($fileRef)
418  {
419  // @todo: Usually, an instance of the LocalizationFactory is found in $this->parserFactory.
420  // @todo: This is not the case if $GLOBALS['LANG'] is not used to get hold of this object,
421  // @todo: but the objectManager instead. If then init() is not called, this will fatal ...
422  // @todo: To be sure, we always create an instance here for now.
424  $languageFactory = GeneralUtility::makeInstance(LocalizationFactory::class);
425 
426  if ($this->lang !== 'default') {
427  $languages = array_reverse($this->languageDependencies);
428  } else {
429  $languages = array('default');
430  }
431  $localLanguage = array();
432  foreach ($languages as $language) {
433  $tempLL = $languageFactory->getParsedData($fileRef, $language, $this->charSet);
434  $localLanguage['default'] = $tempLL['default'];
435  if (!isset($localLanguage[$this->lang])) {
436  $localLanguage[$this->lang] = $localLanguage['default'];
437  }
438  if ($this->lang !== 'default' && isset($tempLL[$language])) {
439  // Merge current language labels onto labels from previous language
440  // This way we have a labels with fall back applied
441  ArrayUtility::mergeRecursiveWithOverrule($localLanguage[$this->lang], $tempLL[$language], true, false);
442  }
443  }
444  return $localLanguage;
445  }
446 
454  protected function localizedFileRef($fileRef)
455  {
456  if ($this->lang !== 'default' && substr($fileRef, -4) === '.php') {
458  return substr($fileRef, 0, -4) . '.' . $this->lang . '.php';
459  } else {
460  return null;
461  }
462  }
463 
472  public function overrideLL($index, $value, $overrideDefault = true)
473  {
474  if (!isset($GLOBALS['LOCAL_LANG'])) {
475  $GLOBALS['LOCAL_LANG'] = array();
476  }
477  $GLOBALS['LOCAL_LANG'][$this->lang][$index][0]['target'] = $value;
478  if ($overrideDefault) {
479  $GLOBALS['LOCAL_LANG']['default'][$index][0]['target'] = $value;
480  }
481  }
482 
491  public function getLabelsWithPrefix($prefix, $strip = '')
492  {
493  $extraction = array();
494  $labels = array_merge((array)$GLOBALS['LOCAL_LANG']['default'], (array)$GLOBALS['LOCAL_LANG'][$GLOBALS['LANG']->lang]);
495  // Regular expression to strip the selection prefix and possibly something from the label name:
496  $labelPattern = '#^' . preg_quote($prefix, '#') . '(' . preg_quote($strip, '#') . ')?#';
497  // Iterate through all locallang labels:
498  foreach ($labels as $label => $value) {
499  if (strpos($label, $prefix) === 0) {
500  $key = preg_replace($labelPattern, '', $label);
501  $extraction[$key] = $value;
502  }
503  }
504  return $extraction;
505  }
506 }