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!
16 use TYPO3\CMS\Backend\Template\Components\ButtonBar
;
17 use TYPO3\CMS\Backend\Template\Components\Menu\Menu
;
18 use TYPO3\CMS\Backend\Template\Components\Menu\MenuItem
;
19 use TYPO3\CMS\Backend\View\BackendTemplateView
;
20 use TYPO3\CMS\Core\Imaging\Icon
;
21 use TYPO3\CMS\Core\Utility\GeneralUtility
;
22 use TYPO3\CMS\Extbase\Mvc\Controller\ActionController
;
23 use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder
;
24 use TYPO3\CMS\Extbase\Utility\LocalizationUtility
;
29 class LanguageController
extends ActionController
34 protected $defaultViewObjectName = BackendTemplateView
::class;
37 * @var BackendTemplateView
42 * @var \TYPO3\CMS\Lang\Domain\Repository\LanguageRepository
44 protected $languageRepository;
47 * @var \TYPO3\CMS\Lang\Domain\Repository\ExtensionRepository
49 protected $extensionRepository;
52 * @var \TYPO3\CMS\Lang\Service\TranslationService
54 protected $translationService;
57 * @var \TYPO3\CMS\Lang\Service\RegistryService
59 protected $registryService;
62 * @param \TYPO3\CMS\Lang\Domain\Repository\LanguageRepository $languageRepository
64 public function injectLanguageRepository(\TYPO3\CMS\Lang\Domain\Repository\LanguageRepository
$languageRepository)
66 $this->languageRepository
= $languageRepository;
70 * @param \TYPO3\CMS\Lang\Domain\Repository\ExtensionRepository $extensionRepository
72 public function injectExtensionRepository(\TYPO3\CMS\Lang\Domain\Repository\ExtensionRepository
$extensionRepository)
74 $this->extensionRepository
= $extensionRepository;
78 * @param \TYPO3\CMS\Lang\Service\TranslationService $translationService
80 public function injectTranslationService(\TYPO3\CMS\Lang\Service\TranslationService
$translationService)
82 $this->translationService
= $translationService;
86 * @param \TYPO3\CMS\Lang\Service\RegistryService $registryService
88 public function injectRegistryService(\TYPO3\CMS\Lang\Service\RegistryService
$registryService)
90 $this->registryService
= $registryService;
98 public function listLanguagesAction()
100 $this->prepareDocHeaderMenu();
101 $this->prepareDocHeaderButtons();
103 $languages = $this->languageRepository
->findAll();
104 $this->view
->assign('languages', $languages);
112 public function listTranslationsAction()
114 $this->prepareDocHeaderMenu();
116 $languages = $this->languageRepository
->findSelected();
117 $this->view
->assign('languages', $languages);
121 * Returns the translations
125 public function getTranslationsAction()
127 $this->view
->assign('extensions', $this->extensionRepository
->findAll());
128 $this->view
->assign('languages', $this->languageRepository
->findSelected());
132 * Fetch all translations for given locale
134 * @param array $data The request data
137 public function updateLanguageAction(array $data)
139 $numberOfExtensionsToUpdate = 10;
144 if (!empty($data['locale'])) {
146 for ($i = 0; $i < $numberOfExtensionsToUpdate; $i++
) {
147 $offset = (int)$data['count'] * $numberOfExtensionsToUpdate +
$i;
148 $extension = $this->extensionRepository
->findOneByOffset($offset);
149 if (empty($extension)) {
150 // No more extensions to update
153 if ($allCount === 0) {
154 $allCount = (int)$this->extensionRepository
->countAll();
156 $extensionKey = $extension->getKey();
157 $result = $this->translationService
->updateTranslation($extensionKey, $data['locale']);
158 $progress = round((($offset +
1) * 100) / $allCount, 2);
159 $response['result'][$data['locale']][$extensionKey] = $result[$data['locale']];
160 if (empty($result[$extensionKey][$data['locale']]['error'])) {
161 $response['success'] = true;
163 // Could not update an extension, stop here!
164 $response['success'] = false;
169 if ($response['success']) {
170 $this->registryService
->set($data['locale'], $GLOBALS['EXEC_TIME']);
171 $response['timestamp'] = $GLOBALS['EXEC_TIME'];
172 $response['progress'] = $progress > 100 ?
100 : $progress;
174 $this->view
->assign('response', $response);
178 * Fetch the translation for given extension and locale
180 * @param array $data The request data
183 public function updateTranslationAction(array $data)
185 $response = array('success' => false);
186 if (!empty($data['extension']) && !empty($data['locale'])) {
187 $result = $this->translationService
->updateTranslation($data['extension'], $data['locale']);
188 if (empty($result[$data['extension']][$data['locale']]['error'])) {
195 $this->view
->assign('response', $response);
199 * Activate a language
201 * @param array $data The request data
204 public function activateLanguageAction(array $data)
206 $response = array('success' => false);
207 if (!empty($data['locale'])) {
208 $response = $this->languageRepository
->activateByLocale($data['locale']);
210 $this->view
->assign('response', $response);
214 * Deactivate a language
216 * @param array $data The request data
219 public function deactivateLanguageAction(array $data)
221 $response = array('success' => false);
222 if (!empty($data['locale'])) {
223 $response = $this->languageRepository
->deactivateByLocale($data['locale']);
225 $this->view
->assign('response', $response);
231 protected function prepareDocHeaderMenu()
233 $this->view
->getModuleTemplate()->setModuleName('typo3-module-lang');
234 $this->view
->getModuleTemplate()->setModuleId('typo3-module-lang');
236 $this->view
->getModuleTemplate()->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Lang/LanguageModule');
238 $extensionKey = 'lang';
239 $addJsInlineLabels = [
240 'flashmessage.error',
241 'flashmessage.information',
242 'flashmessage.success',
243 'flashmessage.multipleErrors',
244 'flashmessage.updateComplete',
245 'flashmessage.canceled',
246 'flashmessage.languageActivated',
247 'flashmessage.languageDeactivated',
248 'flashmessage.errorOccurred',
251 'table.loadingRecords',
256 foreach ($addJsInlineLabels as $key) {
257 $label = LocalizationUtility
::translate($key, $extensionKey);
258 $this->view
->getModuleTemplate()->getPageRenderer()->addInlineLanguageLabel($key, $label);
261 $uriBuilder = $this->objectManager
->get(UriBuilder
::class);
262 $uriBuilder->setRequest($this->request
);
264 /** @var Menu $menu */
265 $menu = GeneralUtility
::makeInstance(Menu
::class);
266 $menu->setIdentifier('_languageMenu');
267 $menu->setLabel($this->getLanguageService()->sL('LLL:EXT:lang/locallang_general.xlf:LGL.language', true));
269 /** @var MenuItem $languageListMenuItem */
270 $languageListMenuItem = GeneralUtility
::makeInstance(MenuItem
::class);
271 $action = 'listLanguages';
272 $isActive = $this->request
->getControllerActionName() === $action ?
true : false;
273 $languageListMenuItem->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang.xlf:header.languages'));
274 $uri = $uriBuilder->reset()->uriFor('listLanguages', array(), 'Language');
275 $languageListMenuItem->setHref($uri)->setActive($isActive);
277 /** @var MenuItem $translationMenuItem */
278 $translationMenuItem = GeneralUtility
::makeInstance(MenuItem
::class);
279 $action = 'listTranslations';
280 $isActive = $this->request
->getControllerActionName() === $action ?
true : false;
281 $translationMenuItem->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang.xlf:header.translations'));
282 $uri = $uriBuilder->reset()->uriFor('listTranslations', array(), 'Language');
283 $translationMenuItem->setHref($uri)->setActive($isActive);
285 $menu->addMenuItem($languageListMenuItem);
286 $menu->addMenuItem($translationMenuItem);
287 $this->view
->getModuleTemplate()->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
288 $this->view
->getModuleTemplate()->setFlashMessageQueue($this->controllerContext
->getFlashMessageQueue());
292 * Returns LanguageService
294 * @return \TYPO3\CMS\Lang\LanguageService
296 protected function getLanguageService()
298 return $GLOBALS['LANG'];
304 protected function prepareDocHeaderButtons()
306 // @todo: the html structure needed to operate the buttons correctly is broken now.
307 // @todo: LanguageModule.js and backend.css -> div.typo3-module-lang div.menuItems
309 $downloadAllButton = $this->view
->getModuleTemplate()->getDocHeaderComponent()->getButtonBar()->makeLinkButton()
310 ->setIcon($this->view
->getModuleTemplate()->getIconFactory()->getIcon('actions-system-extension-download', Icon
::SIZE_SMALL
))
311 ->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang.xlf:button.downloadAll'))
312 ->setClasses('menuItem updateItem t3js-button-update')
313 ->setDataAttributes(['action' => 'updateActiveLanguages'])
315 $this->view
->getModuleTemplate()->getDocHeaderComponent()->getButtonBar()->addButton($downloadAllButton, ButtonBar
::BUTTON_POSITION_LEFT
);
317 $cancelButton = $this->view
->getModuleTemplate()->getDocHeaderComponent()->getButtonBar()->makeLinkButton()
318 ->setIcon($this->view
->getModuleTemplate()->getIconFactory()->getIcon('actions-document-close', Icon
::SIZE_SMALL
))
319 ->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang.xlf:button.cancel'))
320 ->setClasses('menuItem cancelItem disabled t3js-button-cancel')
321 ->setDataAttributes(['action' => 'cancelLanguageUpdate'])
324 $this->view
->getModuleTemplate()->getDocHeaderComponent()->getButtonBar()->addButton($cancelButton, ButtonBar
::BUTTON_POSITION_LEFT
);