47e86ff83f3ff3e8f7e585c98e763b32a91d872e
[Packages/TYPO3.CMS.git] / typo3 / sysext / lang / Classes / Command / LanguageCommandController.php
1 <?php
2 namespace TYPO3\CMS\Lang\Command;
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\Package\PackageInterface;
18 use TYPO3\CMS\Core\Package\PackageManager;
19
20 /**
21 * Language command controller updates translation packages
22 */
23 class LanguageCommandController extends \TYPO3\CMS\Extbase\Mvc\Controller\CommandController
24 {
25 /**
26 * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
27 */
28 protected $signalSlotDispatcher;
29
30 /**
31 * @var \TYPO3\CMS\Lang\Service\RegistryService
32 */
33 protected $registryService;
34
35 /**
36 * @param \TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher
37 */
38 public function injectSignalSlotDispatcher(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher)
39 {
40 $this->signalSlotDispatcher = $signalSlotDispatcher;
41 }
42
43 /**
44 * @param \TYPO3\CMS\Lang\Service\RegistryService $registryService
45 */
46 public function injectRegistryService(\TYPO3\CMS\Lang\Service\RegistryService $registryService)
47 {
48 $this->registryService = $registryService;
49 }
50
51 /**
52 * Update language file for each extension
53 *
54 * @param string $localesToUpdate Comma separated list of locales that needs to be updated
55 * @return void
56 */
57 public function updateCommand($localesToUpdate = '')
58 {
59 /** @var $translationService \TYPO3\CMS\Lang\Service\TranslationService */
60 $translationService = $this->objectManager->get(\TYPO3\CMS\Lang\Service\TranslationService::class);
61 /** @var $languageRepository \TYPO3\CMS\Lang\Domain\Repository\LanguageRepository */
62 $languageRepository = $this->objectManager->get(\TYPO3\CMS\Lang\Domain\Repository\LanguageRepository::class);
63 $locales = array();
64 if (!empty($localesToUpdate)) {
65 $locales = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $localesToUpdate, true);
66 } else {
67 $languages = $languageRepository->findSelected();
68 foreach ($languages as $language) {
69 /** @var $language \TYPO3\CMS\Lang\Domain\Model\Language */
70 $locales[] = $language->getLocale();
71 }
72 }
73 /** @var PackageManager $packageManager */
74 $packageManager = $this->objectManager->get(\TYPO3\CMS\Core\Package\PackageManager::class);
75 $this->emitPackagesMayHaveChangedSignal();
76 $packages = $packageManager->getAvailablePackages();
77 $this->outputLine((sprintf('Updating language packs of all activated extensions for locales "%s"', implode(', ', $locales))));
78 $this->output->progressStart(count($locales) * count($packages));
79 foreach ($locales as $locale) {
80 /** @var PackageInterface $package */
81 foreach ($packages as $package) {
82 $extensionKey = $package->getPackageKey();
83 $result = $translationService->updateTranslation($extensionKey, $locale);
84 if (empty($result[$extensionKey][$locale]['error'])) {
85 $this->registryService->set($locale, $GLOBALS['EXEC_TIME']);
86 }
87 $this->output->progressAdvance();
88 }
89 }
90 $this->output->progressFinish();
91 }
92
93 /**
94 * Emits packages may have changed signal
95 */
96 protected function emitPackagesMayHaveChangedSignal()
97 {
98 $this->signalSlotDispatcher->dispatch('PackageManagement', 'packagesMayHaveChanged');
99 }
100 }