[TASK] EXT:lang CodeCleanup
[Packages/TYPO3.CMS.git] / typo3 / sysext / lang / Classes / Service / TranslationService.php
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
17 use TYPO3\CMS\Core\SingletonInterface;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;
20 use TYPO3\CMS\Extensionmanager\Utility\Repository\Helper;
21
22 /**
23 * Translation service
24 */
25 class TranslationService implements SingletonInterface
26 {
27 /**
28 * Status codes for AJAX response
29 */
30 const TRANSLATION_NOT_AVAILABLE = 0;
31 const TRANSLATION_AVAILABLE = 1;
32 const TRANSLATION_FAILED = 2;
33 const TRANSLATION_OK = 3;
34 const TRANSLATION_INVALID = 4;
35 const TRANSLATION_UPDATED = 5;
36
37 /**
38 * @var \TYPO3\CMS\Lang\Service\TerService
39 */
40 protected $terService;
41
42 /**
43 * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
44 */
45 protected $signalSlotDispatcher;
46
47 /**
48 * @var string
49 */
50 protected $mirrorUrl = '';
51
52 /**
53 * @param \TYPO3\CMS\Lang\Service\TerService $terService
54 */
55 public function injectTerService(TerService $terService)
56 {
57 $this->terService = $terService;
58 }
59
60 /**
61 * @param \TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher
62 */
63 public function injectSignalSlotDispatcher(Dispatcher $signalSlotDispatcher)
64 {
65 $this->signalSlotDispatcher = $signalSlotDispatcher;
66 }
67
68 /**
69 * @param \TYPO3\CMS\Extensionmanager\Utility\Repository\Helper $helper The helper
70 */
71 public function injectRepositoryHelper(Helper $helper)
72 {
73 $this->mirrorUrl = $helper->getMirrors(false)->getMirrorUrl();
74 }
75
76 /**
77 * Update translation for given extension
78 *
79 * @param string $extensionKey The extension key
80 * @param mixed $locales Comma separated list or array of locales
81 * @return array Update information
82 */
83 public function updateTranslation($extensionKey, $locales)
84 {
85 if (is_string($locales)) {
86 $locales = GeneralUtility::trimExplode(',', $locales);
87 }
88 $locales = array_flip((array) $locales);
89 foreach ($locales as $locale => $key) {
90 $state = static::TRANSLATION_INVALID;
91 $error = '';
92 try {
93 $state = $this->updateTranslationForExtension($extensionKey, $locale);
94 } catch (\Exception $exception) {
95 $error = $exception->getMessage();
96 }
97 $locales[$locale] = array(
98 'state' => $state,
99 'error' => $error,
100 );
101 }
102 return $locales;
103 }
104
105 /**
106 * Update the translation for an extension
107 *
108 * @param string $extensionKey The extension key
109 * @param string $locale Locale to update
110 * @return int Translation state
111 */
112 protected function updateTranslationForExtension($extensionKey, $locale)
113 {
114 if (empty($extensionKey) || empty($locale)) {
115 return static::TRANSLATION_INVALID;
116 }
117 $state = static::TRANSLATION_FAILED;
118
119 $updateResult = $this->terService->updateTranslation($extensionKey, $locale, $this->getMirrorUrl($extensionKey));
120 if ($updateResult === true) {
121 $state = static::TRANSLATION_UPDATED;
122 }
123 return $state;
124 }
125
126 /**
127 * Returns the mirror URL for a given extension.
128 *
129 * @param string $extensionKey
130 * @return string
131 */
132 protected function getMirrorUrl($extensionKey)
133 {
134 $this->signalSlotDispatcher->dispatch(
135 __CLASS__,
136 'postProcessMirrorUrl',
137 array(
138 'extensionKey' => $extensionKey,
139 'mirrorUrl' => &$this->mirrorUrl,
140 )
141 );
142
143 return $this->mirrorUrl;
144 }
145 }