126a66e4dabba05eed3d6d04fa9210fd5ae973a2
[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 /**
18 * Translation service
19 */
20 class TranslationService implements \TYPO3\CMS\Core\SingletonInterface
21 {
22 /**
23 * Status codes for AJAX response
24 */
25 const TRANSLATION_NOT_AVAILABLE = 0;
26 const TRANSLATION_AVAILABLE = 1;
27 const TRANSLATION_FAILED = 2;
28 const TRANSLATION_OK = 3;
29 const TRANSLATION_INVALID = 4;
30 const TRANSLATION_UPDATED = 5;
31
32 /**
33 * @var \TYPO3\CMS\Lang\Service\TerService
34 */
35 protected $terService;
36
37 /**
38 * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
39 */
40 protected $signalSlotDispatcher;
41
42 /**
43 * @var string
44 */
45 protected $mirrorUrl = '';
46
47 /**
48 * @param \TYPO3\CMS\Lang\Service\TerService $terService
49 */
50 public function injectTerService(\TYPO3\CMS\Lang\Service\TerService $terService)
51 {
52 $this->terService = $terService;
53 }
54
55 /**
56 * @param \TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher
57 */
58 public function injectSignalSlotDispatcher(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher)
59 {
60 $this->signalSlotDispatcher = $signalSlotDispatcher;
61 }
62
63 /**
64 * @param \TYPO3\CMS\Extensionmanager\Utility\Repository\Helper $helper The helper
65 */
66 public function injectRepositoryHelper(\TYPO3\CMS\Extensionmanager\Utility\Repository\Helper $helper)
67 {
68 $this->mirrorUrl = $helper->getMirrors(false)->getMirrorUrl();
69 }
70
71 /**
72 * Update translation for given extension
73 *
74 * @param string $extensionKey The extension key
75 * @param mixed $locales Comma separated list or array of locales
76 * @return array Update information
77 */
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
99 /**
100 * Update the translation for an extension
101 *
102 * @param string $extensionKey The extension key
103 * @param string $locale Locale to update
104 * @return int Translation state
105 */
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
120 /**
121 * Returns the mirror URL for a given extension.
122 *
123 * @param string $extensionKey
124 * @return string
125 */
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 }