2 namespace TYPO3\CMS\Lang\Controller
;
5 * This file is part of the TYPO3 CMS project.
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.
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
14 * The TYPO3 project - inspiring people to share!
17 use TYPO3\CMS\Backend\Template\Components\ButtonBar
;
18 use TYPO3\CMS\Backend\Template\Components\Menu\Menu
;
19 use TYPO3\CMS\Backend\Template\Components\Menu\MenuItem
;
20 use TYPO3\CMS\Backend\View\BackendTemplateView
;
21 use TYPO3\CMS\Core\Imaging\Icon
;
22 use TYPO3\CMS\Core\Utility\GeneralUtility
;
23 use TYPO3\CMS\Lang\Domain\Model\Extension
;
24 use TYPO3\CMS\Lang\Domain\Repository\ExtensionRepository
;
25 use TYPO3\CMS\Lang\Domain\Repository\LanguageRepository
;
26 use TYPO3\CMS\Lang\Service\RegistryService
;
27 use TYPO3\CMS\Lang\Service\TranslationService
;
28 use TYPO3\CMS\Extbase\Mvc\Controller\ActionController
;
29 use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder
;
30 use TYPO3\CMS\Extbase\Utility\LocalizationUtility
;
35 class LanguageController
extends ActionController
40 protected $defaultViewObjectName = BackendTemplateView
::class;
43 * @var BackendTemplateView
48 * @var \TYPO3\CMS\Lang\Domain\Repository\LanguageRepository
50 protected $languageRepository;
53 * @var \TYPO3\CMS\Lang\Domain\Repository\ExtensionRepository
55 protected $extensionRepository;
58 * @var \TYPO3\CMS\Lang\Service\TranslationService
60 protected $translationService;
63 * @var \TYPO3\CMS\Lang\Service\RegistryService
65 protected $registryService;
68 * @param \TYPO3\CMS\Lang\Domain\Repository\LanguageRepository $languageRepository
70 public function injectLanguageRepository(LanguageRepository
$languageRepository)
72 $this->languageRepository
= $languageRepository;
76 * @param \TYPO3\CMS\Lang\Domain\Repository\ExtensionRepository $extensionRepository
78 public function injectExtensionRepository(ExtensionRepository
$extensionRepository)
80 $this->extensionRepository
= $extensionRepository;
84 * @param \TYPO3\CMS\Lang\Service\TranslationService $translationService
86 public function injectTranslationService(TranslationService
$translationService)
88 $this->translationService
= $translationService;
92 * @param \TYPO3\CMS\Lang\Service\RegistryService $registryService
94 public function injectRegistryService(RegistryService
$registryService)
96 $this->registryService
= $registryService;
104 public function listLanguagesAction()
106 $this->prepareDocHeaderMenu();
107 $this->prepareDocHeaderButtons();
109 $languages = $this->languageRepository
->findAll();
110 $this->view
->assign('languages', $languages);
118 public function listTranslationsAction()
120 $this->prepareDocHeaderMenu();
122 $languages = $this->languageRepository
->findSelected();
123 $this->view
->assign('languages', $languages);
127 * Returns the translations
131 public function getTranslationsAction()
133 $this->view
->assign('extensions', $this->extensionRepository
->findAll());
134 $this->view
->assign('languages', $this->languageRepository
->findSelected());
138 * Fetch all translations for given locale
140 * @param array $data The request data
143 public function updateLanguageAction(array $data)
145 $numberOfExtensionsToUpdate = 10;
151 if (!empty($data['locale'])) {
153 for ($i = 0; $i < $numberOfExtensionsToUpdate; $i++
) {
154 $offset = (int)$data['count'] * $numberOfExtensionsToUpdate +
$i;
155 /** @var Extension $extension */
156 $extension = $this->extensionRepository
->findOneByOffset($offset);
157 if (empty($extension)) {
158 // No more extensions to update
161 if ($allCount === 0) {
162 $allCount = (int)$this->extensionRepository
->countAll();
164 $extensionKey = $extension->getKey();
165 $result = $this->translationService
->updateTranslation($extensionKey, $data['locale']);
166 $progress = round((($offset +
1) * 100) / $allCount, 2);
167 $response['result'][$data['locale']][$extensionKey] = $result[$data['locale']];
168 if (empty($result[$extensionKey][$data['locale']]['error'])) {
169 $response['success'] = true;
171 // Could not update an extension, stop here!
172 $response['success'] = false;
177 if ($response['success']) {
178 $this->registryService
->set($data['locale'], $GLOBALS['EXEC_TIME']);
179 $response['timestamp'] = $GLOBALS['EXEC_TIME'];
180 $response['progress'] = $progress > 100 ?
100 : $progress;
182 $this->view
->assign('response', $response);
186 * Fetch the translation for given extension and locale
188 * @param array $data The request data
191 public function updateTranslationAction(array $data)
193 $response = array('success' => false);
194 if (!empty($data['extension']) && !empty($data['locale'])) {
195 $result = $this->translationService
->updateTranslation($data['extension'], $data['locale']);
196 if (empty($result[$data['extension']][$data['locale']]['error'])) {
203 $this->view
->assign('response', $response);
207 * Activate a language
209 * @param array $data The request data
212 public function activateLanguageAction(array $data)
214 $response = array('success' => false);
215 if (!empty($data['locale'])) {
216 $response = $this->languageRepository
->activateByLocale($data['locale']);
218 $this->view
->assign('response', $response);
222 * Deactivate a language
224 * @param array $data The request data
227 public function deactivateLanguageAction(array $data)
229 $response = array('success' => false);
230 if (!empty($data['locale'])) {
231 $response = $this->languageRepository
->deactivateByLocale($data['locale']);
233 $this->view
->assign('response', $response);
239 protected function prepareDocHeaderMenu()
241 $this->view
->getModuleTemplate()->setModuleName('typo3-module-lang');
242 $this->view
->getModuleTemplate()->setModuleId('typo3-module-lang');
244 $this->view
->getModuleTemplate()->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Lang/LanguageModule');
246 $extensionKey = 'lang';
247 $addJsInlineLabels = [
248 'flashmessage.error',
249 'flashmessage.information',
250 'flashmessage.success',
251 'flashmessage.multipleErrors',
252 'flashmessage.updateComplete',
253 'flashmessage.canceled',
254 'flashmessage.languageActivated',
255 'flashmessage.languageDeactivated',
256 'flashmessage.errorOccurred',
259 'table.loadingRecords',
264 foreach ($addJsInlineLabels as $key) {
265 $label = LocalizationUtility
::translate($key, $extensionKey);
266 $this->view
->getModuleTemplate()->getPageRenderer()->addInlineLanguageLabel($key, $label);
269 $uriBuilder = $this->objectManager
->get(UriBuilder
::class);
270 $uriBuilder->setRequest($this->request
);
272 /** @var Menu $menu */
273 $menu = GeneralUtility
::makeInstance(Menu
::class);
274 $menu->setIdentifier('_languageMenu');
275 $menu->setLabel($this->getLanguageService()->sL('LLL:EXT:lang/locallang_general.xlf:LGL.language', true));
277 /** @var MenuItem $languageListMenuItem */
278 $languageListMenuItem = GeneralUtility
::makeInstance(MenuItem
::class);
279 $action = 'listLanguages';
280 $isActive = $this->request
->getControllerActionName() === $action ?
true : false;
281 $languageListMenuItem->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang.xlf:header.languages'));
282 $uri = $uriBuilder->reset()->uriFor('listLanguages', array(), 'Language');
283 $languageListMenuItem->setHref($uri)->setActive($isActive);
285 /** @var MenuItem $translationMenuItem */
286 $translationMenuItem = GeneralUtility
::makeInstance(MenuItem
::class);
287 $action = 'listTranslations';
288 $isActive = $this->request
->getControllerActionName() === $action ?
true : false;
289 $translationMenuItem->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang.xlf:header.translations'));
290 $uri = $uriBuilder->reset()->uriFor('listTranslations', array(), 'Language');
291 $translationMenuItem->setHref($uri)->setActive($isActive);
293 $menu->addMenuItem($languageListMenuItem);
294 $menu->addMenuItem($translationMenuItem);
295 $this->view
->getModuleTemplate()->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
296 $this->view
->getModuleTemplate()->setFlashMessageQueue($this->controllerContext
->getFlashMessageQueue());
300 * Returns LanguageService
302 * @return \TYPO3\CMS\Lang\LanguageService
304 protected function getLanguageService()
306 return $GLOBALS['LANG'];
312 protected function prepareDocHeaderButtons()
314 // @todo: the html structure needed to operate the buttons correctly is broken now.
315 // @todo: LanguageModule.js and backend.css -> div.typo3-module-lang div.menuItems
317 $downloadAllButton = $this->view
->getModuleTemplate()->getDocHeaderComponent()->getButtonBar()->makeLinkButton()
318 ->setIcon($this->view
->getModuleTemplate()->getIconFactory()->getIcon('actions-system-extension-download', Icon
::SIZE_SMALL
))
319 ->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang.xlf:button.downloadAll'))
320 ->setClasses('menuItem updateItem t3js-button-update')
321 ->setDataAttributes(['action' => 'updateActiveLanguages'])
323 $this->view
->getModuleTemplate()->getDocHeaderComponent()->getButtonBar()->addButton($downloadAllButton, ButtonBar
::BUTTON_POSITION_LEFT
);
325 $cancelButton = $this->view
->getModuleTemplate()->getDocHeaderComponent()->getButtonBar()->makeLinkButton()
326 ->setIcon($this->view
->getModuleTemplate()->getIconFactory()->getIcon('actions-document-close', Icon
::SIZE_SMALL
))
327 ->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang.xlf:button.cancel'))
328 ->setClasses('menuItem cancelItem disabled t3js-button-cancel')
329 ->setDataAttributes(['action' => 'cancelLanguageUpdate'])
332 $this->view
->getModuleTemplate()->getDocHeaderComponent()->getButtonBar()->addButton($cancelButton, ButtonBar
::BUTTON_POSITION_LEFT
);