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