‪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 
56  private PackageManager $packageManager;
57 
61  public function __construct(PackageManager $packageManager)
62  {
63  $this->‪packageManager = $packageManager;
64  $this->‪initialize();
65  }
66 
70  public function ‪initialize()
71  {
72  if (isset(‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority']) && trim(‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority']) !== '') {
73  $this->supportedExtensions = GeneralUtility::trimExplode(',', ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority']);
74  } else {
75  $this->supportedExtensions = ['xlf'];
76  }
77  }
78 
86  public function ‪hasData($fileReference, $languageKey)
87  {
88  if (isset($this->‪data[$fileReference][$languageKey]) && is_array($this->‪data[$fileReference][$languageKey])) {
89  return true;
90  }
91  return false;
92  }
93 
103  public function getData($fileReference)
104  {
105  return $this->‪data[$fileReference];
106  }
107 
118  public function getDataByLanguage($fileReference, $languageKey)
119  {
120  return $this->‪data[$fileReference][$languageKey] ?? [];
121  }
122 
131  public function ‪setData($fileReference, $languageKey, $data)
132  {
133  $this->‪data[$fileReference][$languageKey] = $data;
134  return $this;
135  }
136 
143  public function ‪flushData($fileReference)
144  {
145  unset($this->‪data[$fileReference]);
146  return $this;
147  }
148 
158  public function ‪setConfiguration($fileReference, $languageKey)
159  {
160  $this->configuration[$fileReference] = [
161  'fileReference' => $fileReference,
162  'fileExtension' => false,
163  'parserClass' => null,
164  'languageKey' => $languageKey,
165  ];
166  if (‪PathUtility::isExtensionPath($fileReference)) {
167  $packageKey = $this->‪packageManager->extractPackageKeyFromPackagePath($fileReference);
168  $relativeFileName = substr($fileReference, strlen($packageKey) + 5);
169  $directory = dirname($relativeFileName);
170  $fileName = basename($relativeFileName);
171  $this->configuration[$fileReference]['localizedLabelsPathPattern'] = sprintf(
172  '/%%1$s/%s/%s%%1$s.%s',
173  $packageKey,
174  ($directory ? $directory . '/' : ''),
175  $fileName
176  );
177  }
178  $fileWithoutExtension = GeneralUtility::getFileAbsFileName($this->‪getFileReferenceWithoutExtension($fileReference));
179  foreach ($this->supportedExtensions as $extension) {
180  if (@is_file($fileWithoutExtension . '.' . $extension)) {
181  $this->configuration[$fileReference]['fileReference'] = $fileWithoutExtension . '.' . $extension;
182  $this->configuration[$fileReference]['fileExtension'] = $extension;
183  break;
184  }
185  }
186  if ($this->configuration[$fileReference]['fileExtension'] === false) {
187  throw new ‪FileNotFoundException(sprintf('Source localization file (%s) not found', $fileReference), 1306410755);
188  }
189  $extension = $this->configuration[$fileReference]['fileExtension'];
190  if (isset(‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['parser'][$extension]) && trim(‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['parser'][$extension]) !== '') {
191  $this->configuration[$fileReference]['parserClass'] = ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['parser'][$extension];
192  } else {
193  throw new ‪InvalidParserException('TYPO3 Fatal Error: l10n parser for file extension "' . $extension . '" is not configured! Please check you configuration.', 1301579637);
194  }
195  if (!class_exists($this->configuration[$fileReference]['parserClass']) || trim($this->configuration[$fileReference]['parserClass']) === '') {
196  throw new ‪InvalidParserException('TYPO3 Fatal Error: l10n parser "' . $this->configuration[$fileReference]['parserClass'] . '" cannot be found or is an empty parser!', 1270853900);
197  }
198  return $this;
199  }
200 
207  public function ‪getFileReferenceWithoutExtension($fileReference)
208  {
209  if (!isset($this->configuration[$fileReference]['fileReferenceWithoutExtension'])) {
210  $this->configuration[$fileReference]['fileReferenceWithoutExtension'] = preg_replace('/\\.[a-z0-9]+$/i', '', $fileReference);
211  }
212  return $this->configuration[$fileReference]['fileReferenceWithoutExtension'];
213  }
214 
222  public function ‪getParserInstance($fileReference)
223  {
224  if (isset($this->configuration[$fileReference]['parserClass']) && trim($this->configuration[$fileReference]['parserClass']) !== '') {
225  return GeneralUtility::makeInstance((string)$this->configuration[$fileReference]['parserClass']);
226  }
227  throw new ‪InvalidParserException(sprintf('Invalid parser configuration for the current file (%s)', $fileReference), 1307293692);
228  }
229 
237  public function ‪getAbsoluteFileReference($fileReference)
238  {
239  if (isset($this->configuration[$fileReference]['fileReference']) && trim($this->configuration[$fileReference]['fileReference']) !== '') {
240  return (string)$this->configuration[$fileReference]['fileReference'];
241  }
242  throw new \InvalidArgumentException(sprintf('Invalid file reference configuration for the current file (%s)', $fileReference), 1307293693);
243  }
244 
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\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:155
‪TYPO3\CMS\Core\Localization\LanguageStore\initialize
‪initialize()
Definition: LanguageStore.php:67
‪TYPO3\CMS\Core\Localization\LanguageStore\$supportedExtensions
‪array $supportedExtensions
Definition: LanguageStore.php:36
‪TYPO3\CMS\Core\Localization\Exception\FileNotFoundException
Definition: FileNotFoundException.php:22
‪TYPO3\CMS\Core\Localization\LanguageStore\getAbsoluteFileReference
‪string getAbsoluteFileReference($fileReference)
Definition: LanguageStore.php:234
‪TYPO3\CMS\Core\Localization\LanguageStore\getFileReferenceWithoutExtension
‪string getFileReferenceWithoutExtension($fileReference)
Definition: LanguageStore.php:204
‪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:219
‪TYPO3\CMS\Core\Localization\LanguageStore\data
‪array< string, function getData( $fileReference) { return $this-> data[$fileReference]
Definition: LanguageStore.php:102
‪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:60
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:23
‪$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:83
‪TYPO3\CMS\Core\Localization\LanguageStore\flushData
‪TYPO3 CMS Core Localization LanguageStore flushData($fileReference)
Definition: LanguageStore.php:140
‪TYPO3\CMS\Core\Localization\Exception\InvalidParserException
Definition: InvalidParserException.php:22
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51
‪TYPO3\CMS\Core\Localization\LanguageStore\setData
‪TYPO3 CMS Core Localization LanguageStore setData($fileReference, $languageKey, $data)
Definition: LanguageStore.php:128
‪TYPO3\CMS\Core\Localization\LanguageStore\getLocalizedLabelsPathPattern
‪getLocalizedLabelsPathPattern(string $fileReference)
Definition: LanguageStore.php:247