TYPO3 CMS  TYPO3_8-7
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 
22 
27 {
33  const TRANSLATION_FAILED = 2;
34  const TRANSLATION_OK = 3;
37 
41  protected $terService;
42 
47 
51  protected $mirrorUrl = '';
52 
57  {
58  $this->terService = $terService;
59  }
60 
65  {
66  $this->signalSlotDispatcher = $signalSlotDispatcher;
67  }
68 
72  public function injectRepositoryHelper(Helper $helper)
73  {
74  try {
75  $this->mirrorUrl = $helper->getMirrors(false)->getMirrorUrl();
76  } catch (ExtensionManagerException $e) {
77  $this->mirrorUrl = '';
78  }
79  }
80 
88  public function updateTranslation($extensionKey, $locales)
89  {
90  if (is_string($locales)) {
92  }
93  $locales = array_flip((array)$locales);
94  foreach ($locales as $locale => $key) {
95  $state = static::TRANSLATION_INVALID;
96  $error = '';
97  try {
98  $state = $this->updateTranslationForExtension($extensionKey, $locale);
99  } catch (\Exception $exception) {
100  $error = $exception->getMessage();
101  }
102  $locales[$locale] = [
103  'state' => $state,
104  'error' => $error,
105  ];
106  }
107  return $locales;
108  }
109 
118  protected function updateTranslationForExtension($extensionKey, $locale)
119  {
120  if (empty($extensionKey) || empty($locale)) {
121  return static::TRANSLATION_INVALID;
122  }
123 
124  $mirrorUrl = $this->getMirrorUrl($extensionKey);
125  if (empty($mirrorUrl)) {
126  throw new \Exception('Not able to fetch languages files due to missing mirror url.', 1461248062);
127  }
128 
129  $state = static::TRANSLATION_FAILED;
130 
131  $updateResult = $this->terService->updateTranslation($extensionKey, $locale, $mirrorUrl);
132  if ($updateResult === true) {
133  $state = static::TRANSLATION_UPDATED;
134  }
135  return $state;
136  }
137 
144  protected function getMirrorUrl($extensionKey)
145  {
146  $this->signalSlotDispatcher->dispatch(
147  __CLASS__,
148  'postProcessMirrorUrl',
149  [
150  'extensionKey' => $extensionKey,
151  'mirrorUrl' => &$this->mirrorUrl,
152  ]
153  );
154 
155  return $this->mirrorUrl;
156  }
157 }
static trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
updateTranslationForExtension($extensionKey, $locale)
injectSignalSlotDispatcher(Dispatcher $signalSlotDispatcher)
$locales
Definition: be_users.php:6