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