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  $this->mirrorUrl = $helper->getMirrors(false)->getMirrorUrl();
69  }
70 
78  public function updateTranslation($extensionKey, $locales)
79  {
80  if (is_string($locales)) {
81  $locales = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $locales);
82  }
83  $locales = array_flip((array) $locales);
84  foreach ($locales as $locale => $key) {
85  $state = static::TRANSLATION_INVALID;
86  try {
87  $state = $this->updateTranslationForExtension($extensionKey, $locale);
88  } catch (\Exception $exception) {
89  $error = $exception->getMessage();
90  }
91  $locales[$locale] = array(
92  'state' => $state,
93  'error' => $error,
94  );
95  }
96  return $locales;
97  }
98 
106  protected function updateTranslationForExtension($extensionKey, $locale)
107  {
108  if (empty($extensionKey) || empty($locale)) {
109  return static::TRANSLATION_INVALID;
110  }
111  $state = static::TRANSLATION_FAILED;
112 
113  $updateResult = $this->terService->updateTranslation($extensionKey, $locale, $this->getMirrorUrl($extensionKey));
114  if ($updateResult === true) {
115  $state = static::TRANSLATION_UPDATED;
116  }
117  return $state;
118  }
119 
126  protected function getMirrorUrl($extensionKey)
127  {
128  $this->signalSlotDispatcher->dispatch(
129  __CLASS__,
130  'postProcessMirrorUrl',
131  array(
132  'extensionKey' => $extensionKey,
133  'mirrorUrl' => &$this->mirrorUrl,
134  )
135  );
136 
137  return $this->mirrorUrl;
138  }
139 }