TYPO3 CMS  TYPO3_6-2
LanguageService.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Lang;
3 
18 
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 
122  public function init($lang) {
123  // Initialize the conversion object:
124  $this->csConvObj = GeneralUtility::makeInstance('TYPO3\CMS\Core\Charset\CharsetConverter');
125  $this->charSetArray = $this->csConvObj->charSetArray;
126  // Initialize the parser factory object
127  $this->parserFactory = GeneralUtility::makeInstance('TYPO3\CMS\Core\Localization\LocalizationFactory');
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');
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  return $this->parserFactory;
153  }
154 
163  public function addModuleLabels($arr, $prefix) {
164  if (is_array($arr)) {
165  foreach ($arr as $k => $larr) {
166  if (!isset($this->moduleLabels[$k])) {
167  $this->moduleLabels[$k] = array();
168  }
169  if (is_array($larr)) {
170  foreach ($larr as $l => $v) {
171  $this->moduleLabels[$k][$prefix . $l] = $v;
172  }
173  }
174  }
175  }
176  }
177 
190  public function makeEntities($str) {
191  // Convert string back again, but using the full entity conversion:
192  return $this->csConvObj->utf8_to_entities($str);
193  }
194 
207  public function JScharCode($str) {
209 
210  // Convert the UTF-8 string into a array of char numbers:
211  $nArr = $this->csConvObj->utf8_to_numberarray($str);
212  return 'String.fromCharCode(' . implode(',', $nArr) . ')';
213  }
214 
221  public function debugLL($value) {
222  return $this->debugKey ? '[' . $value . ']' : '';
223  }
224 
233  public function getLL($index, $hsc = FALSE) {
234  // Get Local Language
235  if (isset($GLOBALS['LOCAL_LANG'][$this->lang][$index][0]['target'])) {
236  $output = $GLOBALS['LOCAL_LANG'][$this->lang][$index][0]['target'];
237  } else {
238  $output = $GLOBALS['LOCAL_LANG']['default'][$index][0]['target'];
239  }
240  if ($hsc) {
241  $output = htmlspecialchars($output);
242  }
243  return $output . $this->debugLL($index);
244  }
245 
255  public function getLLL($index, $localLanguage, $hsc = FALSE) {
256  // Get Local Language. Special handling for all extensions that
257  // read PHP LL files and pass arrays here directly.
258  $value = is_string($localLanguage[$this->lang][$index]) ?
259  $localLanguage[$this->lang][$index] :
260  $localLanguage[$this->lang][$index][0]['target'];
261  // Fallback to default language
262  if (trim($value) === '') {
263  $value = is_string($localLanguage['default'][$index]) ?
264  $localLanguage['default'][$index] :
265  $localLanguage['default'][$index][0]['target'];
266  }
267  if ($hsc) {
268  $value = htmlspecialchars($value);
269  }
270  return $value . $this->debugLL($index);
271  }
272 
284  public function sL($input, $hsc = FALSE) {
285  $identifier = $input . '_' . (int)$hsc . '_' . (int)$this->debugKey;
286  if (isset($this->LL_labels_cache[$this->lang][$identifier])) {
287  return $this->LL_labels_cache[$this->lang][$identifier];
288  }
289  if (strpos($input, 'LLL:') === 0) {
290  $restStr = trim(substr($input, 4));
291  $extPrfx = '';
292  // ll-file refered to is found in an extension.
293  if (strpos($restStr, 'EXT:') === 0) {
294  $restStr = trim(substr($restStr, 4));
295  $extPrfx = 'EXT:';
296  }
297  $parts = explode(':', $restStr);
298  $parts[0] = $extPrfx . $parts[0];
299  // Getting data if not cached
300  if (!isset($this->LL_files_cache[$parts[0]])) {
301  $this->LL_files_cache[$parts[0]] = $this->readLLfile($parts[0]);
302  // If the current language is found in another file, load that as well:
303  $lFileRef = $this->localizedFileRef($parts[0]);
304  if ($lFileRef && $this->LL_files_cache[$parts[0]][$this->lang] === 'EXT') {
305  $tempLL = $this->readLLfile($lFileRef);
306  $this->LL_files_cache[$parts[0]][$this->lang] = $tempLL[$this->lang];
307  }
308  }
309  $output = $this->getLLL($parts[1], $this->LL_files_cache[$parts[0]]);
310  } else {
311  // Use a constant non-localizable label
312  $output = $input;
313  }
314  if ($hsc) {
315  $output = htmlspecialchars($output, ENT_COMPAT, 'UTF-8', FALSE);
316  }
317  $output .= $this->debugLL($input);
318  $this->LL_labels_cache[$this->lang][$identifier] = $output;
319  return $output;
320  }
321 
330  public function loadSingleTableDescription($table) {
331  // First the 'table' cannot already be loaded in [columns]
332  // and secondly there must be a references to locallang files available in [refs]
333  if (is_array($GLOBALS['TCA_DESCR'][$table]) && !isset($GLOBALS['TCA_DESCR'][$table]['columns']) && is_array($GLOBALS['TCA_DESCR'][$table]['refs'])) {
334  // Init $TCA_DESCR for $table-key
335  $GLOBALS['TCA_DESCR'][$table]['columns'] = array();
336  // Get local-lang for each file in $TCA_DESCR[$table]['refs'] as they are ordered.
337  foreach ($GLOBALS['TCA_DESCR'][$table]['refs'] as $llfile) {
338  $localLanguage = $this->includeLLFile($llfile, 0, 1);
339  // Traverse all keys
340  if (is_array($localLanguage['default'])) {
341  foreach ($localLanguage['default'] as $lkey => $lVal) {
342  // Exploding by '.':
343  // 0-n => fieldname,
344  // n+1 => type from (alttitle, description, details, syntax, image_descr,image,seeAlso),
345  // n+2 => special instruction, if any
346  $keyParts = explode('.', $lkey);
347  $keyPartsCount = count($keyParts);
348  // Check if last part is special instruction
349  // Only "+" is currently supported
350  $specialInstruction = $keyParts[$keyPartsCount - 1] === '+';
351  if ($specialInstruction) {
352  array_pop($keyParts);
353  }
354  // If there are more than 2 parts, get the type from the last part
355  // and merge back the other parts with a dot (.)
356  // Otherwise just get type and field name straightaway
357  if ($keyPartsCount > 2) {
358  $type = array_pop($keyParts);
359  $fieldName = implode('.', $keyParts);
360  } else {
361  $fieldName = $keyParts[0];
362  $type = $keyParts[1];
363  }
364  // Detecting 'hidden' labels, converting to normal fieldname
365  if ($fieldName === '_') {
366  $fieldName = '';
367  }
368  if ($fieldName !== '' && $fieldName[0] === '_') {
369  $fieldName = substr($fieldName, 1);
370  }
371  // Append label
372  $label = $lVal[0]['target'] ? :
373  $lVal[0]['source'];
374  if ($specialInstruction) {
375  $GLOBALS['TCA_DESCR'][$table]['columns'][$fieldName][$type] .= LF . $label;
376  } else {
377  // Substitute label
378  $GLOBALS['TCA_DESCR'][$table]['columns'][$fieldName][$type] = $label;
379  }
380  }
381  }
382  }
383  }
384  }
385 
395  public function includeLLFile($fileRef, $setGlobal = TRUE, $mergeLocalOntoDefault = FALSE) {
396  $globalLanguage = array();
397  // Get default file
398  $localLanguage = $this->readLLfile($fileRef);
399  if (is_array($localLanguage) && !empty($localLanguage)) {
400  // it depends on, whether we should return the result or set it in the global $LOCAL_LANG array
401  if ($setGlobal) {
402  $globalLanguage = (array)$GLOBALS['LOCAL_LANG'];
403  ArrayUtility::mergeRecursiveWithOverrule($globalLanguage, $localLanguage);
404  } else {
405  $globalLanguage = $localLanguage;
406  }
407  // Localized addition?
408  $lFileRef = $this->localizedFileRef($fileRef);
409  if ($lFileRef && (string) $globalLanguage[$this->lang] === 'EXT') {
410  $localLanguage = $this->readLLfile($lFileRef);
411  ArrayUtility::mergeRecursiveWithOverrule($globalLanguage, $localLanguage);
412  }
413  // Merge local onto default
414  if ($mergeLocalOntoDefault && $this->lang !== 'default' && is_array($globalLanguage[$this->lang]) && is_array($globalLanguage['default'])) {
415  // array_merge can be used so far the keys are not
416  // numeric - which we assume they are not...
417  $globalLanguage['default'] = array_merge($globalLanguage['default'], $globalLanguage[$this->lang]);
418  unset($globalLanguage[$this->lang]);
419  }
420  }
421  // Return value if not global is set.
422  if (!$setGlobal) {
423  return $globalLanguage;
424  } else {
425  $GLOBALS['LOCAL_LANG'] = $globalLanguage;
426  return NULL;
427  }
428  }
429 
436  protected function readLLfile($fileRef) {
437  if ($this->lang !== 'default') {
438  $languages = array_reverse($this->languageDependencies);
439  } else {
440  $languages = array('default');
441  }
442  $localLanguage = array();
443  foreach ($languages as $language) {
444  $tempLL = GeneralUtility::readLLfile($fileRef, $language, $this->charSet);
445  $localLanguage['default'] = $tempLL['default'];
446  if (!isset($localLanguage[$this->lang])) {
447  $localLanguage[$this->lang] = $localLanguage['default'];
448  }
449  if ($this->lang !== 'default' && isset($tempLL[$language])) {
450  // Merge current language labels onto labels from previous language
451  // This way we have a labels with fall back applied
452  ArrayUtility::mergeRecursiveWithOverrule($localLanguage[$this->lang], $tempLL[$language], TRUE, FALSE);
453  }
454  }
455  return $localLanguage;
456  }
457 
464  protected function localizedFileRef($fileRef) {
465  if ($this->lang !== 'default' && substr($fileRef, -4) === '.php') {
466  return substr($fileRef, 0, -4) . '.' . $this->lang . '.php';
467  } else {
468  return NULL;
469  }
470  }
471 
480  public function overrideLL($index, $value, $overrideDefault = TRUE) {
481  if (!isset($GLOBALS['LOCAL_LANG'])) {
482  $GLOBALS['LOCAL_LANG'] = array();
483  }
484  $GLOBALS['LOCAL_LANG'][$this->lang][$index][0]['target'] = $value;
485  if ($overrideDefault) {
486  $GLOBALS['LOCAL_LANG']['default'][$index][0]['target'] = $value;
487  }
488  }
489 }
overrideLL($index, $value, $overrideDefault=TRUE)
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=TRUE, $includeEmptyValues=TRUE, $enableUnsetFeature=TRUE)
includeLLFile($fileRef, $setGlobal=TRUE, $mergeLocalOntoDefault=FALSE)
getLLL($index, $localLanguage, $hsc=FALSE)
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
$locales
Definition: be_users.php:6