TYPO3 CMS  TYPO3_7-6
TranslationService.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Lang\Service;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
21 {
27  const TRANSLATION_FAILED = 2;
28  const TRANSLATION_OK = 3;
31 
35  protected $terService;
36 
41 
45  protected $mirrorUrl = '';
46 
50  public function injectTerService(\TYPO3\CMS\Lang\Service\TerService $terService)
51  {
52  $this->terService = $terService;
53  }
54 
58  public function injectSignalSlotDispatcher(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher)
59  {
60  $this->signalSlotDispatcher = $signalSlotDispatcher;
61  }
62 
66  public function injectRepositoryHelper(\TYPO3\CMS\Extensionmanager\Utility\Repository\Helper $helper)
67  {
68  try {
69  $this->mirrorUrl = $helper->getMirrors(false)->getMirrorUrl();
70  } catch (\TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException $e) {
71  $this->mirrorUrl = '';
72  }
73  }
74 
82  public function updateTranslation($extensionKey, $locales)
83  {
84  if (is_string($locales)) {
86  }
87  $locales = array_flip((array) $locales);
88  foreach ($locales as $locale => $key) {
89  $state = static::TRANSLATION_INVALID;
90  try {
91  $state = $this->updateTranslationForExtension($extensionKey, $locale);
92  } catch (\Exception $exception) {
93  $error = $exception->getMessage();
94  }
95  $locales[$locale] = [
96  'state' => $state,
97  'error' => $error,
98  ];
99  }
100  return $locales;
101  }
102 
111  protected function updateTranslationForExtension($extensionKey, $locale)
112  {
113  if (empty($extensionKey) || empty($locale)) {
114  return static::TRANSLATION_INVALID;
115  }
116 
117  $mirrorUrl = $this->getMirrorUrl($extensionKey);
118  if (empty($mirrorUrl)) {
119  throw new \Exception('Not able to fetch languages files due to missing mirror url.', 1461248062);
120  }
121 
122  $state = static::TRANSLATION_FAILED;
123 
124  $updateResult = $this->terService->updateTranslation($extensionKey, $locale, $mirrorUrl);
125  if ($updateResult === true) {
126  $state = static::TRANSLATION_UPDATED;
127  }
128  return $state;
129  }
130 
137  protected function getMirrorUrl($extensionKey)
138  {
139  $this->signalSlotDispatcher->dispatch(
140  __CLASS__,
141  'postProcessMirrorUrl',
142  [
143  'extensionKey' => $extensionKey,
144  'mirrorUrl' => &$this->mirrorUrl,
145  ]
146  );
147 
148  return $this->mirrorUrl;
149  }
150 }
injectTerService(\TYPO3\CMS\Lang\Service\TerService $terService)
static trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
updateTranslationForExtension($extensionKey, $locale)
injectSignalSlotDispatcher(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher)
injectRepositoryHelper(\TYPO3\CMS\Extensionmanager\Utility\Repository\Helper $helper)
$locales
Definition: be_users.php:6