079d8b800cebfa2fca2b286b782fbcaa7d141ce1
[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 * @inject
35 */
36 protected $terService;
37
38 /**
39 * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
40 * @inject
41 */
42 protected $signalSlotDispatcher;
43
44 /**
45 * @var string
46 */
47 protected $mirrorUrl = '';
48
49 /**
50 * @param \TYPO3\CMS\Extensionmanager\Utility\Repository\Helper $helper The helper
51 * @return void
52 */
53 public function injectRepositoryHelper(\TYPO3\CMS\Extensionmanager\Utility\Repository\Helper $helper) {
54 $this->mirrorUrl = $helper->getMirrors()->getMirrorUrl();
55 }
56
57 /**
58 * Update translation for given extension
59 *
60 * @param string $extensionKey The extension key
61 * @param mixed $locales Comma separated list or array of locales
62 * @return array Update information
63 */
64 public function updateTranslation($extensionKey, $locales) {
65 if (is_string($locales)) {
66 $locales = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $locales);
67 }
68 $locales = array_flip((array) $locales);
69 foreach ($locales as $locale => $key) {
70 $state = static::TRANSLATION_INVALID;
71 try {
72 $state = $this->updateTranslationForExtension($extensionKey, $locale);
73 } catch (\Exception $exception) {
74 $error = $exception->getMessage();
75 }
76 $locales[$locale] = array(
77 'state' => $state,
78 'error' => $error,
79 );
80 }
81 return $locales;
82 }
83
84 /**
85 * Update the translation for an extension
86 *
87 * @param string $extensionKey The extension key
88 * @param string $locale Locale to update
89 * @return int Translation state
90 */
91 protected function updateTranslationForExtension($extensionKey, $locale) {
92 if (empty($extensionKey) || empty($locale)) {
93 return static::TRANSLATION_INVALID;
94 }
95 $state = static::TRANSLATION_FAILED;
96
97 $updateResult = $this->terService->updateTranslation($extensionKey, $locale, $this->getMirrorUrl($extensionKey));
98 if ($updateResult === TRUE) {
99 $state = static::TRANSLATION_UPDATED;
100 }
101 return $state;
102 }
103
104 /**
105 * Returns the mirror URL for a given extension.
106 *
107 * @param string $extensionKey
108 * @return string
109 */
110 protected function getMirrorUrl($extensionKey) {
111 $this->signalSlotDispatcher->dispatch(
112 __CLASS__,
113 'postProcessMirrorUrl',
114 array(
115 'extensionKey' => $extensionKey,
116 'mirrorUrl' => &$this->mirrorUrl,
117 )
118 );
119
120 return $this->mirrorUrl;
121 }
122
123 }