TYPO3 CMS  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 
50  public $debugKey = false;
51 
58  public $moduleLabels = [];
59 
65  public $LL_files_cache = [];
66 
72  public $LL_labels_cache = [];
73 
79  public $csConvObj;
80 
87 
94  protected $languageDependencies = [];
95 
106  public function init($lang)
107  {
108  // Initialize the conversion object:
109  $this->csConvObj = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Charset\CharsetConverter::class);
110  // Initialize the parser factory object
111  $this->parserFactory = GeneralUtility::makeInstance(LocalizationFactory::class);
112  // Find the requested language in this list based
113  // on the $lang key being inputted to this function.
115  $locales = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Localization\Locales::class);
116  // Language is found. Configure it:
117  if (in_array($lang, $locales->getLocales())) {
118  // The current language key
119  $this->lang = $lang;
120  $this->languageDependencies[] = $this->lang;
121  foreach ($locales->getLocaleDependencies($this->lang) as $language) {
122  $this->languageDependencies[] = $language;
123  }
124  }
125  if ($GLOBALS['TYPO3_CONF_VARS']['BE']['lang']['debug']) {
126  $this->debugKey = true;
127  }
128  }
129 
135  public function getParserFactory()
136  {
137  return $this->parserFactory;
138  }
139 
148  public function addModuleLabels($arr, $prefix)
149  {
150  if (is_array($arr)) {
151  foreach ($arr as $k => $larr) {
152  if (!isset($this->moduleLabels[$k])) {
153  $this->moduleLabels[$k] = [];
154  }
155  if (is_array($larr)) {
156  foreach ($larr as $l => $v) {
157  $this->moduleLabels[$k][$prefix . $l] = $v;
158  }
159  }
160  }
161  }
162  }
163 
176  public function makeEntities($str)
177  {
178  // Convert string back again, but using the full entity conversion:
179  return $this->csConvObj->utf8_to_entities($str);
180  }
181 
188  public function debugLL($value)
189  {
190  return $this->debugKey ? '[' . $value . ']' : '';
191  }
192 
201  public function getLL($index, $hsc = false)
202  {
203  return $this->getLLL($index, $GLOBALS['LOCAL_LANG'], $hsc);
204  }
205 
214  public function getLLL($index, $localLanguage, $hsc = false)
215  {
216  // Get Local Language. Special handling for all extensions that
217  // read PHP LL files and pass arrays here directly.
218  if (isset($localLanguage[$this->lang][$index])) {
219  $value = is_string($localLanguage[$this->lang][$index])
220  ? $localLanguage[$this->lang][$index]
221  : $localLanguage[$this->lang][$index][0]['target'];
222  } elseif (isset($localLanguage['default'][$index])) {
223  $value = is_string($localLanguage['default'][$index])
224  ? $localLanguage['default'][$index]
225  : $localLanguage['default'][$index][0]['target'];
226  } else {
227  $value = '';
228  }
229  if ($hsc) {
230  $value = htmlspecialchars($value);
231  }
232  return $value . $this->debugLL($index);
233  }
234 
246  public function sL($input, $hsc = false)
247  {
248  $identifier = $input . '_' . (int)$hsc . '_' . (int)$this->debugKey;
249  if (isset($this->LL_labels_cache[$this->lang][$identifier])) {
250  return $this->LL_labels_cache[$this->lang][$identifier];
251  }
252  if (strpos($input, 'LLL:') === 0) {
253  $restStr = trim(substr($input, 4));
254  $extPrfx = '';
255  // ll-file referred to is found in an extension.
256  if (strpos($restStr, 'EXT:') === 0) {
257  $restStr = trim(substr($restStr, 4));
258  $extPrfx = 'EXT:';
259  }
260  $parts = explode(':', $restStr);
261  $parts[0] = $extPrfx . $parts[0];
262  // Getting data if not cached
263  if (!isset($this->LL_files_cache[$parts[0]])) {
264  $this->LL_files_cache[$parts[0]] = $this->readLLfile($parts[0]);
265  // If the current language is found in another file, load that as well:
266  $lFileRef = $this->localizedFileRef($parts[0]);
267  if ($lFileRef && $this->LL_files_cache[$parts[0]][$this->lang] === 'EXT') {
268  $tempLL = $this->readLLfile($lFileRef);
269  $this->LL_files_cache[$parts[0]][$this->lang] = $tempLL[$this->lang];
270  }
271  }
272  $output = $this->getLLL($parts[1], $this->LL_files_cache[$parts[0]]);
273  } else {
274  // Use a constant non-localizable label
275  $output = $input;
276  }
277  if ($hsc) {
278  $output = htmlspecialchars($output, ENT_COMPAT, 'UTF-8', false);
279  }
280  $output .= $this->debugLL($input);
281  $this->LL_labels_cache[$this->lang][$identifier] = $output;
282  return $output;
283  }
284 
293  public function loadSingleTableDescription($table)
294  {
295  // First the 'table' cannot already be loaded in [columns]
296  // and secondly there must be a references to locallang files available in [refs]
297  if (is_array($GLOBALS['TCA_DESCR'][$table]) && !isset($GLOBALS['TCA_DESCR'][$table]['columns']) && is_array($GLOBALS['TCA_DESCR'][$table]['refs'])) {
298  // Init $TCA_DESCR for $table-key
299  $GLOBALS['TCA_DESCR'][$table]['columns'] = [];
300  // Get local-lang for each file in $TCA_DESCR[$table]['refs'] as they are ordered.
301  foreach ($GLOBALS['TCA_DESCR'][$table]['refs'] as $llfile) {
302  $localLanguage = $this->includeLLFile($llfile, 0, 1);
303  // Traverse all keys
304  if (is_array($localLanguage['default'])) {
305  foreach ($localLanguage['default'] as $lkey => $lVal) {
306  // Exploding by '.':
307  // 0-n => fieldname,
308  // n+1 => type from (alttitle, description, details, syntax, image_descr,image,seeAlso),
309  // n+2 => special instruction, if any
310  $keyParts = explode('.', $lkey);
311  $keyPartsCount = count($keyParts);
312  // Check if last part is special instruction
313  // Only "+" is currently supported
314  $specialInstruction = $keyParts[$keyPartsCount - 1] === '+';
315  if ($specialInstruction) {
316  array_pop($keyParts);
317  }
318  // If there are more than 2 parts, get the type from the last part
319  // and merge back the other parts with a dot (.)
320  // Otherwise just get type and field name straightaway
321  if ($keyPartsCount > 2) {
322  $type = array_pop($keyParts);
323  $fieldName = implode('.', $keyParts);
324  } else {
325  $fieldName = $keyParts[0];
326  $type = $keyParts[1];
327  }
328  // Detecting 'hidden' labels, converting to normal fieldname
329  if ($fieldName === '_') {
330  $fieldName = '';
331  }
332  if ($fieldName !== '' && $fieldName[0] === '_') {
333  $fieldName = substr($fieldName, 1);
334  }
335  // Append label
336  $label = $lVal[0]['target'] ? :
337  $lVal[0]['source'];
338  if ($specialInstruction) {
339  $GLOBALS['TCA_DESCR'][$table]['columns'][$fieldName][$type] .= LF . $label;
340  } else {
341  // Substitute label
342  $GLOBALS['TCA_DESCR'][$table]['columns'][$fieldName][$type] = $label;
343  }
344  }
345  }
346  }
347  }
348  }
349 
359  public function includeLLFile($fileRef, $setGlobal = true, $mergeLocalOntoDefault = false)
360  {
361  $globalLanguage = [];
362  // Get default file
363  $localLanguage = $this->readLLfile($fileRef);
364  if (is_array($localLanguage) && !empty($localLanguage)) {
365  // it depends on, whether we should return the result or set it in the global $LOCAL_LANG array
366  if ($setGlobal) {
367  $globalLanguage = (array)$GLOBALS['LOCAL_LANG'];
368  ArrayUtility::mergeRecursiveWithOverrule($globalLanguage, $localLanguage);
369  } else {
370  $globalLanguage = $localLanguage;
371  }
372  // Localized addition?
373  $lFileRef = $this->localizedFileRef($fileRef);
374  if ($lFileRef && (string)$globalLanguage[$this->lang] === 'EXT') {
375  $localLanguage = $this->readLLfile($lFileRef);
376  ArrayUtility::mergeRecursiveWithOverrule($globalLanguage, $localLanguage);
377  }
378  // Merge local onto default
379  if ($mergeLocalOntoDefault && $this->lang !== 'default' && is_array($globalLanguage[$this->lang]) && is_array($globalLanguage['default'])) {
380  // array_merge can be used so far the keys are not
381  // numeric - which we assume they are not...
382  $globalLanguage['default'] = array_merge($globalLanguage['default'], $globalLanguage[$this->lang]);
383  unset($globalLanguage[$this->lang]);
384  }
385  }
386  // Return value if not global is set.
387  if (!$setGlobal) {
388  return $globalLanguage;
389  } else {
390  $GLOBALS['LOCAL_LANG'] = $globalLanguage;
391  return null;
392  }
393  }
394 
401  protected function readLLfile($fileRef)
402  {
403  // @todo: Usually, an instance of the LocalizationFactory is found in $this->parserFactory.
404  // @todo: This is not the case if $GLOBALS['LANG'] is not used to get hold of this object,
405  // @todo: but the objectManager instead. If then init() is not called, this will fatal ...
406  // @todo: To be sure, we always create an instance here for now.
408  $languageFactory = GeneralUtility::makeInstance(LocalizationFactory::class);
409 
410  if ($this->lang !== 'default') {
411  $languages = array_reverse($this->languageDependencies);
412  } else {
413  $languages = ['default'];
414  }
415  $localLanguage = [];
416  foreach ($languages as $language) {
417  $tempLL = $languageFactory->getParsedData($fileRef, $language, $this->charSet);
418  $localLanguage['default'] = $tempLL['default'];
419  if (!isset($localLanguage[$this->lang])) {
420  $localLanguage[$this->lang] = $localLanguage['default'];
421  }
422  if ($this->lang !== 'default' && isset($tempLL[$language])) {
423  // Merge current language labels onto labels from previous language
424  // This way we have a labels with fall back applied
425  ArrayUtility::mergeRecursiveWithOverrule($localLanguage[$this->lang], $tempLL[$language], true, false);
426  }
427  }
428  return $localLanguage;
429  }
430 
438  protected function localizedFileRef($fileRef)
439  {
440  if ($this->lang !== 'default' && substr($fileRef, -4) === '.php') {
442  return substr($fileRef, 0, -4) . '.' . $this->lang . '.php';
443  } else {
444  return null;
445  }
446  }
447 
456  public function overrideLL($index, $value, $overrideDefault = true)
457  {
458  if (!isset($GLOBALS['LOCAL_LANG'])) {
459  $GLOBALS['LOCAL_LANG'] = [];
460  }
461  $GLOBALS['LOCAL_LANG'][$this->lang][$index][0]['target'] = $value;
462  if ($overrideDefault) {
463  $GLOBALS['LOCAL_LANG']['default'][$index][0]['target'] = $value;
464  }
465  }
466 
475  public function getLabelsWithPrefix($prefix, $strip = '')
476  {
477  $extraction = [];
478  $labels = array_merge((array)$GLOBALS['LOCAL_LANG']['default'], (array)$GLOBALS['LOCAL_LANG'][$GLOBALS['LANG']->lang]);
479  // Regular expression to strip the selection prefix and possibly something from the label name:
480  $labelPattern = '#^' . preg_quote($prefix, '#') . '(' . preg_quote($strip, '#') . ')?#';
481  // Iterate through all locallang labels:
482  foreach ($labels as $label => $value) {
483  if (strpos($label, $prefix) === 0) {
484  $key = preg_replace($labelPattern, '', $label);
485  $extraction[$key] = $value;
486  }
487  }
488  return $extraction;
489  }
490 }
overrideLL($index, $value, $overrideDefault=true)
getLabelsWithPrefix($prefix, $strip='')
includeLLFile($fileRef, $setGlobal=true, $mergeLocalOntoDefault=false)
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
getLLL($index, $localLanguage, $hsc=false)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
$locales
Definition: be_users.php:6