TYPO3  7.6
LanguageController.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Lang\Controller;
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  */
25 
30 {
34  protected $defaultViewObjectName = BackendTemplateView::class;
35 
39  protected $view;
40 
45 
50 
55 
59  protected $registryService;
60 
64  public function injectLanguageRepository(\TYPO3\CMS\Lang\Domain\Repository\LanguageRepository $languageRepository)
65  {
66  $this->languageRepository = $languageRepository;
67  }
68 
72  public function injectExtensionRepository(\TYPO3\CMS\Lang\Domain\Repository\ExtensionRepository $extensionRepository)
73  {
74  $this->extensionRepository = $extensionRepository;
75  }
76 
80  public function injectTranslationService(\TYPO3\CMS\Lang\Service\TranslationService $translationService)
81  {
82  $this->translationService = $translationService;
83  }
84 
88  public function injectRegistryService(\TYPO3\CMS\Lang\Service\RegistryService $registryService)
89  {
90  $this->registryService = $registryService;
91  }
92 
98  public function listLanguagesAction()
99  {
100  $this->prepareDocHeaderMenu();
101  $this->prepareDocHeaderButtons();
102 
103  $languages = $this->languageRepository->findAll();
104  $this->view->assign('languages', $languages);
105  }
106 
112  public function listTranslationsAction()
113  {
114  $this->prepareDocHeaderMenu();
115 
116  $languages = $this->languageRepository->findSelected();
117  $this->view->assign('languages', $languages);
118  }
119 
125  public function getTranslationsAction()
126  {
127  $this->view->assign('extensions', $this->extensionRepository->findAll());
128  $this->view->assign('languages', $this->languageRepository->findSelected());
129  }
130 
137  public function updateLanguageAction(array $data)
138  {
139  $numberOfExtensionsToUpdate = 10;
140  $response = array(
141  'success' => false,
142  'progress' => 0,
143  );
144  if (!empty($data['locale'])) {
145  $allCount = 0;
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
151  break;
152  }
153  if ($allCount === 0) {
154  $allCount = (int)$this->extensionRepository->countAll();
155  }
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;
162  } else {
163  // Could not update an extension, stop here!
164  $response['success'] = false;
165  break;
166  }
167  }
168  }
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;
173  }
174  $this->view->assign('response', $response);
175  }
176 
183  public function updateTranslationAction(array $data)
184  {
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'])) {
189  $response = array(
190  'success' => true,
191  'result' => $result,
192  );
193  }
194  }
195  $this->view->assign('response', $response);
196  }
197 
204  public function activateLanguageAction(array $data)
205  {
206  $response = array('success' => false);
207  if (!empty($data['locale'])) {
208  $response = $this->languageRepository->activateByLocale($data['locale']);
209  }
210  $this->view->assign('response', $response);
211  }
212 
219  public function deactivateLanguageAction(array $data)
220  {
221  $response = array('success' => false);
222  if (!empty($data['locale'])) {
223  $response = $this->languageRepository->deactivateByLocale($data['locale']);
224  }
225  $this->view->assign('response', $response);
226  }
227 
231  protected function prepareDocHeaderMenu()
232  {
233  $this->view->getModuleTemplate()->setModuleName('typo3-module-lang');
234  $this->view->getModuleTemplate()->setModuleId('typo3-module-lang');
235 
236  $this->view->getModuleTemplate()->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Lang/LanguageModule');
237 
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',
249  'table.processing',
250  'table.search',
251  'table.loadingRecords',
252  'table.zeroRecords',
253  'table.emptyTable',
254  'table.dateFormat',
255  ];
256  foreach ($addJsInlineLabels as $key) {
257  $label = LocalizationUtility::translate($key, $extensionKey);
258  $this->view->getModuleTemplate()->getPageRenderer()->addInlineLanguageLabel($key, $label);
259  }
260 
261  $uriBuilder = $this->objectManager->get(UriBuilder::class);
262  $uriBuilder->setRequest($this->request);
263 
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));
268 
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);
276 
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);
284 
285  $menu->addMenuItem($languageListMenuItem);
286  $menu->addMenuItem($translationMenuItem);
287  $this->view->getModuleTemplate()->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
288  $this->view->getModuleTemplate()->setFlashMessageQueue($this->controllerContext->getFlashMessageQueue());
289  }
290 
296  protected function getLanguageService()
297  {
298  return $GLOBALS['LANG'];
299  }
300 
304  protected function prepareDocHeaderButtons()
305  {
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
308 
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'])
314  ->setHref('#');
315  $this->view->getModuleTemplate()->getDocHeaderComponent()->getButtonBar()->addButton($downloadAllButton, ButtonBar::BUTTON_POSITION_LEFT);
316 
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'])
322  ->setHref('#');
323 
324  $this->view->getModuleTemplate()->getDocHeaderComponent()->getButtonBar()->addButton($cancelButton, ButtonBar::BUTTON_POSITION_LEFT);
325  }
326 }