[TASK] Cleanup and update language module
[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 * @author Kai Vogel <k.vogel@reply.de>
21 */
22 class TranslationService implements \TYPO3\CMS\Core\SingletonInterface {
23
24 /**
25 * Status codes for AJAX response
26 */
27 const TRANSLATION_NOT_AVAILABLE = 0;
28 const TRANSLATION_AVAILABLE = 1;
29 const TRANSLATION_FAILED = 2;
30 const TRANSLATION_OK = 3;
31 const TRANSLATION_INVALID = 4;
32 const TRANSLATION_UPDATED = 5;
33
34 /**
35 * @var \TYPO3\CMS\Lang\Service\TerService
36 * @inject
37 */
38 protected $terService;
39
40 /**
41 * @var string
42 */
43 protected $mirrorUrl = '';
44
45 /**
46 * @param \TYPO3\CMS\Extensionmanager\Utility\Repository\Helper $helper The helper
47 * @return void
48 */
49 public function injectRepositoryHelper(\TYPO3\CMS\Extensionmanager\Utility\Repository\Helper $helper) {
50 $this->mirrorUrl = $helper->getMirrors()->getMirrorUrl();
51 }
52
53 /**
54 * Update translation for given extension
55 *
56 * @param string $extensionKey The extension key
57 * @param mixed $locales Comma separated list or array of locales
58 * @return array Update information
59 */
60 public function updateTranslation($extensionKey, $locales) {
61 if (is_string($locales)) {
62 $locales = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $locales);
63 }
64 $locales = array_flip((array) $locales);
65 foreach ($locales as $locale => $key) {
66 $state = static::TRANSLATION_INVALID;
67 try {
68 $state = $this->updateTranslationForExtension($extensionKey, $locale);
69 } catch (\Exception $exception) {
70 $error = $exception->getMessage();
71 }
72 $locales[$locale] = array(
73 'state' => $state,
74 'error' => $error,
75 );
76 }
77 return $locales;
78 }
79
80 /**
81 * Update the translation for an extension
82 *
83 * @param string $extensionKey The extension key
84 * @param string $locale Locale to update
85 * @return int Translation state
86 */
87 protected function updateTranslationForExtension($extensionKey, $locale) {
88 if (empty($extensionKey) || empty($locale)) {
89 return static::TRANSLATION_INVALID;
90 }
91 $state = static::TRANSLATION_FAILED;
92 $updateResult = $this->terService->updateTranslation($extensionKey, $locale, $this->mirrorUrl);
93 if ($updateResult === TRUE) {
94 $state = static::TRANSLATION_UPDATED;
95 }
96 return $state;
97 }
98
99 }