TYPO3 CMS  TYPO3_6-2
UpdateTranslationService.php
Go to the documentation of this file.
1 <?php
3 
21 
27  const TRANSLATION_FAILED = 2;
28  const TRANSLATION_OK = 3;
31 
37 
42  protected $repositoryHelper;
43 
48  protected $terConnection;
49 
55 
59  protected $translationStates = array();
60 
68  public function updateTranslation($extension, $locales) {
69  if (is_string($locales)) {
71  }
72  $locales = array_flip((array) $locales);
73 
74  foreach ($locales as $locale => $key) {
75  $state = static::TRANSLATION_INVALID;
76  try {
77  $state = $this->getTranslationStateForExtension($extension, $locale);
78  if ($state === static::TRANSLATION_AVAILABLE) {
79  $state = $this->updateTranslationForExtension($extension, $locale);
80  }
81  } catch (\Exception $exception) {
82  $error = $exception->getMessage();
83  }
84  $locales[$locale] = array(
85  'state' => $state,
86  'error' => $error,
87  );
88  }
89  return $locales;
90  }
91 
99  protected function getTranslationStateForExtension($extensionKey, $locale) {
100  if (empty($extensionKey) || empty($locale)) {
101  return static::TRANSLATION_INVALID;
102  }
103 
104  $identifier = $extensionKey . '-' . $locale;
105  if (isset($this->translationStates[$identifier])) {
106  return $this->translationStates[$identifier];
107  }
108 
109  $selectedLanguages = $this->languageRepository->findSelected();
110  if (empty($selectedLanguages) || !is_array($selectedLanguages)) {
111  return static::TRANSLATION_INVALID;
112  }
113 
114  $mirrorUrl = $this->getMirrorUrl($extensionKey);
115  $status = $this->terConnection->fetchTranslationStatus($extensionKey, $mirrorUrl);
116 
117  foreach ($selectedLanguages as $language) {
118  $stateLocale = $language->getLocale();
119  $stateIdentifier = $extensionKey . '-' . $stateLocale;
120  $this->translationStates[$stateIdentifier] = static::TRANSLATION_INVALID;
121 
122  if (empty($status[$stateLocale]) || !is_array($status[$stateLocale])) {
123  $this->translationStates[$stateIdentifier] = static::TRANSLATION_NOT_AVAILABLE;
124  continue;
125  }
126 
127  $md5 = $this->getTranslationFileMd5($extensionKey, $stateLocale);
128  if ($md5 !== $status[$stateLocale]['md5']) {
129  $this->translationStates[$stateIdentifier] = static::TRANSLATION_AVAILABLE;
130  continue;
131  }
132 
133  $this->translationStates[$stateIdentifier] = static::TRANSLATION_OK;
134  }
135 
136  return $this->translationStates[$identifier];
137  }
138 
146  protected function getTranslationFileMd5($extensionKey, $locale) {
147  if (empty($extensionKey) || empty($locale)) {
148  return '';
149  }
150  $fileName = PATH_site . 'typo3temp' . DIRECTORY_SEPARATOR . $extensionKey . '-l10n-' . $locale . '.zip';
151  if (is_file($fileName)) {
152  return md5_file($fileName);
153  }
154  return '';
155  }
156 
164  protected function updateTranslationForExtension($extensionKey, $locale) {
165  if (empty($extensionKey) || empty($locale)) {
166  return static::TRANSLATION_INVALID;
167  }
168 
169  $state = static::TRANSLATION_FAILED;
170  $mirrorUrl = $this->getMirrorUrl($extensionKey);
171  $updateResult = $this->terConnection->updateTranslation($extensionKey, $locale, $mirrorUrl);
172  if ($updateResult === TRUE) {
173  $state = static::TRANSLATION_UPDATED;
174  }
175 
176  return $state;
177  }
178 
185  protected function getMirrorUrl($extensionKey) {
186  $mirrorUrl = $this->repositoryHelper->getMirrors(FALSE)->getMirrorUrl();
187 
188  $mirrorUrl = $this->emitPostProcessMirrorUrlSignal($extensionKey, $mirrorUrl);
189 
190  return $mirrorUrl;
191  }
192 
200  protected function emitPostProcessMirrorUrlSignal($extensionKey, $mirrorUrl) {
201  $this->signalSlotDispatcher->dispatch(
202  __CLASS__,
203  'postProcessMirrorUrl',
204  array(
205  $extensionKey,
206  &$mirrorUrl,
207  )
208  );
209  return $mirrorUrl;
210  }
211 
212 }
static trimExplode($delim, $string, $removeEmptyValues=FALSE, $limit=0)
$locales
Definition: be_users.php:6