‪TYPO3CMS  11.5
LanguageStore.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
20 use TYPO3\CMS\Core\Package\PackageManager;
24 
29 {
35  protected ‪$supportedExtensions;
36 
45  protected ‪$configuration;
46 
52  protected $data;
53 
54  private PackageManager $packageManager;
55 
59  public function __construct(PackageManager $packageManager)
60  {
61  $this->‪packageManager = $packageManager;
62  $this->‪initialize();
63  }
64 
68  public function ‪initialize()
69  {
70  if (isset(‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority']) && trim(‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority']) !== '') {
71  $this->supportedExtensions = ‪GeneralUtility::trimExplode(',', ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority']);
72  } else {
73  $this->supportedExtensions = ['xlf'];
74  }
75  }
76 
84  public function ‪hasData($fileReference, $languageKey)
85  {
86  if (isset($this->‪data[$fileReference][$languageKey]) && is_array($this->‪data[$fileReference][$languageKey])) {
87  return true;
88  }
89  return false;
90  }
91 
101  public function getData($fileReference)
102  {
103  return $this->‪data[$fileReference];
104  }
105 
116  public function getDataByLanguage($fileReference, $languageKey)
117  {
118  return $this->‪data[$fileReference][$languageKey] ?? [];
119  }
120 
129  public function ‪setData($fileReference, $languageKey, $data)
130  {
131  $this->‪data[$fileReference][$languageKey] = $data;
132  return $this;
133  }
134 
141  public function ‪flushData($fileReference)
142  {
143  unset($this->‪data[$fileReference]);
144  return $this;
145  }
146 
156  public function ‪setConfiguration($fileReference, $languageKey)
157  {
158  $this->configuration[$fileReference] = [
159  'fileReference' => $fileReference,
160  'fileExtension' => false,
161  'parserClass' => null,
162  'languageKey' => $languageKey,
163  ];
164  if (‪PathUtility::isExtensionPath($fileReference)) {
165  $packageKey = $this->‪packageManager->extractPackageKeyFromPackagePath($fileReference);
166  $relativeFileName = substr($fileReference, strlen($packageKey) + 5);
167  $directory = dirname($relativeFileName);
168  $fileName = basename($relativeFileName);
169  $this->configuration[$fileReference]['localizedLabelsPathPattern'] = sprintf(
170  '/%%1$s/%s/%s%%1$s.%s',
171  $packageKey,
172  ($directory ? $directory . '/' : ''),
173  $fileName
174  );
175  }
176  $fileWithoutExtension = GeneralUtility::getFileAbsFileName($this->‪getFileReferenceWithoutExtension($fileReference));
177  foreach ($this->supportedExtensions as $extension) {
178  if (@is_file($fileWithoutExtension . '.' . $extension)) {
179  $this->configuration[$fileReference]['fileReference'] = $fileWithoutExtension . '.' . $extension;
180  $this->configuration[$fileReference]['fileExtension'] = $extension;
181  break;
182  }
183  }
184  if ($this->configuration[$fileReference]['fileExtension'] === false) {
185  throw new ‪FileNotFoundException(sprintf('Source localization file (%s) not found', $fileReference), 1306410755);
186  }
187  $extension = $this->configuration[$fileReference]['fileExtension'];
188  if (isset(‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['parser'][$extension]) && trim(‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['parser'][$extension]) !== '') {
189  $this->configuration[$fileReference]['parserClass'] = ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['parser'][$extension];
190  } else {
191  throw new ‪InvalidParserException('TYPO3 Fatal Error: l10n parser for file extension "' . $extension . '" is not configured! Please check you configuration.', 1301579637);
192  }
193  if (!class_exists($this->configuration[$fileReference]['parserClass']) || trim($this->configuration[$fileReference]['parserClass']) === '') {
194  throw new ‪InvalidParserException('TYPO3 Fatal Error: l10n parser "' . $this->configuration[$fileReference]['parserClass'] . '" cannot be found or is an empty parser!', 1270853900);
195  }
196  return $this;
197  }
198 
205  public function ‪getFileReferenceWithoutExtension($fileReference)
206  {
207  if (!isset($this->configuration[$fileReference]['fileReferenceWithoutExtension'])) {
208  $this->configuration[$fileReference]['fileReferenceWithoutExtension'] = preg_replace('/\\.[a-z0-9]+$/i', '', $fileReference);
209  }
210  return $this->configuration[$fileReference]['fileReferenceWithoutExtension'];
211  }
212 
220  public function ‪getParserInstance($fileReference)
221  {
222  if (isset($this->configuration[$fileReference]['parserClass']) && trim($this->configuration[$fileReference]['parserClass']) !== '') {
223  return GeneralUtility::makeInstance((string)$this->configuration[$fileReference]['parserClass']);
224  }
225  throw new ‪InvalidParserException(sprintf('Invalid parser configuration for the current file (%s)', $fileReference), 1307293692);
226  }
227 
235  public function ‪getAbsoluteFileReference($fileReference)
236  {
237  if (isset($this->configuration[$fileReference]['fileReference']) && trim($this->configuration[$fileReference]['fileReference']) !== '') {
238  return (string)$this->configuration[$fileReference]['fileReference'];
239  }
240  throw new \InvalidArgumentException(sprintf('Invalid file reference configuration for the current file (%s)', $fileReference), 1307293693);
241  }
242 
250  public function ‪getLocalizedLabelsPathPattern(string $fileReference): string
251  {
252  if (empty($this->configuration[$fileReference]['localizedLabelsPathPattern'])) {
253  throw new \InvalidArgumentException(sprintf('Invalid file reference configuration for the current file (%s)', $fileReference), 1635863703);
254  }
255 
256  return (string)$this->configuration[$fileReference]['localizedLabelsPathPattern'];
257  }
258 
264  public function ‪getSupportedExtensions()
265  {
267  }
268 }
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:999
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:25
‪TYPO3\CMS\Core\Localization\LanguageStore\setConfiguration
‪TYPO3 CMS Core Localization LanguageStore setConfiguration($fileReference, $languageKey)
Definition: LanguageStore.php:153
‪TYPO3\CMS\Core\Utility\PathUtility\isExtensionPath
‪static bool isExtensionPath(string $path)
Definition: PathUtility.php:121
‪TYPO3\CMS\Core\Localization\LanguageStore\initialize
‪initialize()
Definition: LanguageStore.php:65
‪TYPO3\CMS\Core\Localization\LanguageStore\$supportedExtensions
‪array $supportedExtensions
Definition: LanguageStore.php:34
‪TYPO3\CMS\Core\Localization\Exception\FileNotFoundException
Definition: FileNotFoundException.php:21
‪TYPO3\CMS\Core\Localization\LanguageStore\getAbsoluteFileReference
‪string getAbsoluteFileReference($fileReference)
Definition: LanguageStore.php:232
‪TYPO3\CMS\Core\Localization\LanguageStore\getFileReferenceWithoutExtension
‪string getFileReferenceWithoutExtension($fileReference)
Definition: LanguageStore.php:202
‪TYPO3\CMS\Core\Localization
Definition: CacheWarmer.php:18
‪TYPO3\CMS\Core\Localization\LanguageStore\$configuration
‪array $configuration
Definition: LanguageStore.php:43
‪TYPO3\CMS\Core\Localization\LanguageStore
Definition: LanguageStore.php:29
‪TYPO3\CMS\Core\Localization\LanguageStore\getParserInstance
‪TYPO3 CMS Core Localization Parser LocalizationParserInterface getParserInstance($fileReference)
Definition: LanguageStore.php:217
‪TYPO3\CMS\Core\Localization\LanguageStore\data
‪array< string, function getData( $fileReference) { return $this-> data[$fileReference]
Definition: LanguageStore.php:100
‪TYPO3\CMS\Core\Localization\LanguageStore\getSupportedExtensions
‪array getSupportedExtensions()
Definition: LanguageStore.php:261
‪TYPO3\CMS\Core\Localization\LanguageStore\packageManager
‪array< string, $data;private PackageManager $packageManager;public function __construct(PackageManager $packageManager) { $this-> packageManager
Definition: LanguageStore.php:58
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Localization\LanguageStore\hasData
‪bool hasData($fileReference, $languageKey)
Definition: LanguageStore.php:81
‪TYPO3\CMS\Core\Localization\LanguageStore\flushData
‪TYPO3 CMS Core Localization LanguageStore flushData($fileReference)
Definition: LanguageStore.php:138
‪TYPO3\CMS\Core\Localization\Exception\InvalidParserException
Definition: InvalidParserException.php:21
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Localization\LanguageStore\setData
‪TYPO3 CMS Core Localization LanguageStore setData($fileReference, $languageKey, $data)
Definition: LanguageStore.php:126
‪TYPO3\CMS\Core\Localization\LanguageStore\getLocalizedLabelsPathPattern
‪string getLocalizedLabelsPathPattern(string $fileReference)
Definition: LanguageStore.php:247