Skip to content
Snippets Groups Projects
Commit 16c5c810 authored by Michiel Roos's avatar Michiel Roos Committed by Wouter Wolters
Browse files

[TASK] Cleanup \TYPO3\CMS\Lang\LanguageService

* Cache hsc values of calls to sL()
* Code cleanup
* Use !empty() instead of count() for objects known to be an array

Change-Id: Ie015f9755b98c41df185865325ea15ce42179e23
Resolves: #56109
Releases: 6.2
Reviewed-on: https://review.typo3.org/27745
Reviewed-by: Steffen Ritter
Reviewed-by: Markus Klein
Tested-by: Markus Klein
Reviewed-by: Oliver Klee
Reviewed-by: Wouter Wolters
Tested-by: Wouter Wolters
parent 76706f57
No related merge requests found
...@@ -294,12 +294,15 @@ class LanguageService { ...@@ -294,12 +294,15 @@ class LanguageService {
* @return string * @return string
*/ */
public function sL($input, $hsc = FALSE) { public function sL($input, $hsc = FALSE) {
// If cached label $identifier = $input . '_' . (int)$hsc . '_' . (int)$this->debugKey;
if (!isset($this->LL_labels_cache[$this->lang][$input]) && substr($input, 0, 4) === 'LLL:') { if (isset($this->LL_labels_cache[$this->lang][$identifier])) {
return $this->LL_labels_cache[$this->lang][$identifier];
}
if (strpos($input, 'LLL:') === 0) {
$restStr = trim(substr($input, 4)); $restStr = trim(substr($input, 4));
$extPrfx = ''; $extPrfx = '';
// ll-file refered to is found in an extension. // ll-file refered to is found in an extension.
if (substr($restStr, 0, 4) === 'EXT:') { if (strpos($restStr, 'EXT:') === 0) {
$restStr = trim(substr($restStr, 4)); $restStr = trim(substr($restStr, 4));
$extPrfx = 'EXT:'; $extPrfx = 'EXT:';
} }
...@@ -310,25 +313,22 @@ class LanguageService { ...@@ -310,25 +313,22 @@ class LanguageService {
$this->LL_files_cache[$parts[0]] = $this->readLLfile($parts[0]); $this->LL_files_cache[$parts[0]] = $this->readLLfile($parts[0]);
// If the current language is found in another file, load that as well: // If the current language is found in another file, load that as well:
$lFileRef = $this->localizedFileRef($parts[0]); $lFileRef = $this->localizedFileRef($parts[0]);
if ($lFileRef && is_string($this->LL_files_cache[$parts[0]][$this->lang]) && $this->LL_files_cache[$parts[0]][$this->lang] == 'EXT') { if ($lFileRef && $this->LL_files_cache[$parts[0]][$this->lang] === 'EXT') {
$tempLL = $this->readLLfile($lFileRef); $tempLL = $this->readLLfile($lFileRef);
$this->LL_files_cache[$parts[0]][$this->lang] = $tempLL[$this->lang]; $this->LL_files_cache[$parts[0]][$this->lang] = $tempLL[$this->lang];
} }
} }
$this->LL_labels_cache[$this->lang][$input] = $this->getLLL($parts[1], $this->LL_files_cache[$parts[0]]); $output = $this->getLLL($parts[1], $this->LL_files_cache[$parts[0]]);
}
// For the cached output charset conversion has already happened!
// So perform HSC right here.
if (isset($this->LL_labels_cache[$this->lang][$input])) {
$output = $this->LL_labels_cache[$this->lang][$input];
} else { } else {
// Use a constant non-localizable label // Use a constant non-localizable label
$output = $input; $output = $input;
} }
if ($hsc) { if ($hsc) {
$output = \TYPO3\CMS\Core\Utility\GeneralUtility::deHSCentities(htmlspecialchars($output)); $output = htmlspecialchars($output, ENT_COMPAT, 'UTF-8', FALSE);
} }
return $output . $this->debugLL($input); $output .= $this->debugLL($input);
$this->LL_labels_cache[$this->lang][$identifier] = $output;
return $output;
} }
/** /**
...@@ -359,9 +359,7 @@ class LanguageService { ...@@ -359,9 +359,7 @@ class LanguageService {
$keyPartsCount = count($keyParts); $keyPartsCount = count($keyParts);
// Check if last part is special instruction // Check if last part is special instruction
// Only "+" is currently supported // Only "+" is currently supported
$specialInstruction = $keyParts[$keyPartsCount - 1] == '+' ? $specialInstruction = $keyParts[$keyPartsCount - 1] === '+';
TRUE :
FALSE;
if ($specialInstruction) { if ($specialInstruction) {
array_pop($keyParts); array_pop($keyParts);
} }
...@@ -410,7 +408,7 @@ class LanguageService { ...@@ -410,7 +408,7 @@ class LanguageService {
$globalLanguage = array(); $globalLanguage = array();
// Get default file // Get default file
$localLanguage = $this->readLLfile($fileRef); $localLanguage = $this->readLLfile($fileRef);
if (is_array($localLanguage) && count($localLanguage)) { if (is_array($localLanguage) && !empty($localLanguage)) {
// it depends on, whether we should return the result or set it in the global $LOCAL_LANG array // it depends on, whether we should return the result or set it in the global $LOCAL_LANG array
if ($setGlobal) { if ($setGlobal) {
$globalLanguage = (array)$GLOBALS['LOCAL_LANG']; $globalLanguage = (array)$GLOBALS['LOCAL_LANG'];
...@@ -420,7 +418,7 @@ class LanguageService { ...@@ -420,7 +418,7 @@ class LanguageService {
} }
// Localized addition? // Localized addition?
$lFileRef = $this->localizedFileRef($fileRef); $lFileRef = $this->localizedFileRef($fileRef);
if ($lFileRef && (string) $globalLanguage[$this->lang] == 'EXT') { if ($lFileRef && (string) $globalLanguage[$this->lang] === 'EXT') {
$localLanguage = $this->readLLfile($lFileRef); $localLanguage = $this->readLLfile($lFileRef);
\TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($globalLanguage, $localLanguage); \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($globalLanguage, $localLanguage);
} }
...@@ -475,7 +473,7 @@ class LanguageService { ...@@ -475,7 +473,7 @@ class LanguageService {
* @return string Input filename with a '.[lang-key].php' ending added if $this->lang is not 'default' * @return string Input filename with a '.[lang-key].php' ending added if $this->lang is not 'default'
*/ */
protected function localizedFileRef($fileRef) { protected function localizedFileRef($fileRef) {
if ($this->lang != 'default' && substr($fileRef, -4) == '.php') { if ($this->lang !== 'default' && substr($fileRef, -4) === '.php') {
return substr($fileRef, 0, -4) . '.' . $this->lang . '.php'; return substr($fileRef, 0, -4) . '.' . $this->lang . '.php';
} }
} }
...@@ -489,7 +487,7 @@ class LanguageService { ...@@ -489,7 +487,7 @@ class LanguageService {
* @return void * @return void
*/ */
public function overrideLL($index, $value, $overrideDefault = TRUE) { public function overrideLL($index, $value, $overrideDefault = TRUE) {
if (isset($GLOBALS['LOCAL_LANG']) === FALSE) { if (!isset($GLOBALS['LOCAL_LANG'])) {
$GLOBALS['LOCAL_LANG'] = array(); $GLOBALS['LOCAL_LANG'] = array();
} }
$GLOBALS['LOCAL_LANG'][$this->lang][$index][0]['target'] = $value; $GLOBALS['LOCAL_LANG'][$this->lang][$index][0]['target'] = $value;
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment