TYPO3 CMS  TYPO3_7-6
LanguageController.php
Go to the documentation of this file.
1 <?php
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  */
26 
31 {
35  protected $defaultViewObjectName = BackendTemplateView::class;
36 
40  protected $view;
41 
46 
51 
56 
60  protected $registryService;
61 
65  public function injectLanguageRepository(\TYPO3\CMS\Lang\Domain\Repository\LanguageRepository $languageRepository)
66  {
67  $this->languageRepository = $languageRepository;
68  }
69 
73  public function injectExtensionRepository(\TYPO3\CMS\Lang\Domain\Repository\ExtensionRepository $extensionRepository)
74  {
75  $this->extensionRepository = $extensionRepository;
76  }
77 
81  public function injectTranslationService(\TYPO3\CMS\Lang\Service\TranslationService $translationService)
82  {
83  $this->translationService = $translationService;
84  }
85 
89  public function injectRegistryService(\TYPO3\CMS\Lang\Service\RegistryService $registryService)
90  {
91  $this->registryService = $registryService;
92  }
93 
99  public function listLanguagesAction()
100  {
101  $this->prepareDocHeaderMenu();
102  $this->prepareDocHeaderButtons();
103 
104  $languages = $this->languageRepository->findAll();
105  $this->view->assign('languages', $languages);
106  }
107 
113  public function listTranslationsAction()
114  {
115  $this->prepareDocHeaderMenu();
116 
117  $languages = $this->languageRepository->findSelected();
118  $this->view->assign('languages', $languages);
119  }
120 
126  public function getTranslationsAction()
127  {
128  $this->view->assign('extensions', $this->extensionRepository->findAll());
129  $this->view->assign('languages', $this->languageRepository->findSelected());
130  }
131 
138  public function updateLanguageAction(array $data)
139  {
140  $numberOfExtensionsToUpdate = 10;
141  $response = [
142  'success' => false,
143  'progress' => 0,
144  ];
145  if (!empty($data['locale'])) {
146  $allCount = 0;
147  for ($i = 0; $i < $numberOfExtensionsToUpdate; $i++) {
148  $offset = (int)$data['count'] * $numberOfExtensionsToUpdate + $i;
149  $extension = $this->extensionRepository->findOneByOffset($offset);
150  if (empty($extension)) {
151  // No more extensions to update
152  break;
153  }
154  if ($allCount === 0) {
155  $allCount = (int)$this->extensionRepository->countAll();
156  }
157  $extensionKey = $extension->getKey();
158  $result = $this->translationService->updateTranslation($extensionKey, $data['locale']);
159  $progress = round((($offset + 1) * 100) / $allCount, 2);
160  $response['result'][$data['locale']][$extensionKey] = $result[$data['locale']];
161  if (empty($result[$data['locale']]['error'])) {
162  $response['success'] = true;
163  } else {
164  // Could not update an extension, stop here!
165  $response['success'] = false;
166  break;
167  }
168  }
169  }
170  if ($response['success']) {
171  $this->registryService->set($data['locale'], $GLOBALS['EXEC_TIME']);
172  $response['timestamp'] = $GLOBALS['EXEC_TIME'];
173  $response['progress'] = $progress > 100 ? 100 : $progress;
174  }
175  $this->view->assign('response', $response);
176  // Flush language cache
177  GeneralUtility::makeInstance(CacheManager::class)->getCache('l10n')->flush();
178  }
179 
186  public function updateTranslationAction(array $data)
187  {
188  $response = ['success' => false];
189  if (!empty($data['extension']) && !empty($data['locale'])) {
190  $result = $this->translationService->updateTranslation($data['extension'], $data['locale']);
191  if (empty($result[$data['extension']][$data['locale']]['error'])) {
192  $response = [
193  'success' => true,
194  'result' => $result,
195  ];
196  }
197  }
198  $this->view->assign('response', $response);
199  // Flush language cache
200  GeneralUtility::makeInstance(CacheManager::class)->getCache('l10n')->flush();
201  }
202 
209  public function activateLanguageAction(array $data)
210  {
211  $response = ['success' => false];
212  if (!empty($data['locale'])) {
213  $response = $this->languageRepository->activateByLocale($data['locale']);
214  }
215  $this->view->assign('response', $response);
216  }
217 
224  public function deactivateLanguageAction(array $data)
225  {
226  $response = ['success' => false];
227  if (!empty($data['locale'])) {
228  $response = $this->languageRepository->deactivateByLocale($data['locale']);
229  }
230  $this->view->assign('response', $response);
231  }
232 
236  protected function prepareDocHeaderMenu()
237  {
238  $this->view->getModuleTemplate()->setModuleName('typo3-module-lang');
239  $this->view->getModuleTemplate()->setModuleId('typo3-module-lang');
240 
241  $this->view->getModuleTemplate()->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Lang/LanguageModule');
242 
243  $extensionKey = 'lang';
244  $addJsInlineLabels = [
245  'flashmessage.error',
246  'flashmessage.information',
247  'flashmessage.success',
248  'flashmessage.multipleErrors',
249  'flashmessage.updateComplete',
250  'flashmessage.canceled',
251  'flashmessage.languageActivated',
252  'flashmessage.languageDeactivated',
253  'flashmessage.noLanguageActivated',
254  'flashmessage.errorOccurred',
255  'table.processing',
256  'table.search',
257  'table.loadingRecords',
258  'table.zeroRecords',
259  'table.emptyTable',
260  'table.dateFormat',
261  ];
262  foreach ($addJsInlineLabels as $key) {
263  $label = LocalizationUtility::translate($key, $extensionKey);
264  $this->view->getModuleTemplate()->getPageRenderer()->addInlineLanguageLabel($key, $label);
265  }
266 
267  $uriBuilder = $this->objectManager->get(UriBuilder::class);
268  $uriBuilder->setRequest($this->request);
269 
271  $menu = GeneralUtility::makeInstance(Menu::class);
272  $menu->setIdentifier('_languageMenu');
273  $menu->setLabel($this->getLanguageService()->sL('LLL:EXT:lang/locallang_general.xlf:LGL.language', true));
274 
276  $languageListMenuItem = GeneralUtility::makeInstance(MenuItem::class);
277  $action = 'listLanguages';
278  $isActive = $this->request->getControllerActionName() === $action ? true : false;
279  $languageListMenuItem->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang.xlf:header.languages'));
280  $uri = $uriBuilder->reset()->uriFor('listLanguages', [], 'Language');
281  $languageListMenuItem->setHref($uri)->setActive($isActive);
282 
284  $translationMenuItem = GeneralUtility::makeInstance(MenuItem::class);
285  $action = 'listTranslations';
286  $isActive = $this->request->getControllerActionName() === $action ? true : false;
287  $translationMenuItem->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang.xlf:header.translations'));
288  $uri = $uriBuilder->reset()->uriFor('listTranslations', [], 'Language');
289  $translationMenuItem->setHref($uri)->setActive($isActive);
290 
291  $menu->addMenuItem($languageListMenuItem);
292  $menu->addMenuItem($translationMenuItem);
293  $this->view->getModuleTemplate()->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
294  $this->view->getModuleTemplate()->setFlashMessageQueue($this->controllerContext->getFlashMessageQueue());
295  }
296 
302  protected function getLanguageService()
303  {
304  return $GLOBALS['LANG'];
305  }
306 
310  protected function prepareDocHeaderButtons()
311  {
312  // @todo: the html structure needed to operate the buttons correctly is broken now.
313  // @todo: LanguageModule.js and backend.css -> div.typo3-module-lang div.menuItems
314 
315  $downloadAllButton = $this->view->getModuleTemplate()->getDocHeaderComponent()->getButtonBar()->makeLinkButton()
316  ->setIcon($this->view->getModuleTemplate()->getIconFactory()->getIcon('actions-system-extension-download', Icon::SIZE_SMALL))
317  ->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang.xlf:button.downloadAll'))
318  ->setClasses('menuItem updateItem t3js-button-update')
319  ->setDataAttributes(['action' => 'updateActiveLanguages'])
320  ->setHref('#');
321  $this->view->getModuleTemplate()->getDocHeaderComponent()->getButtonBar()->addButton($downloadAllButton, ButtonBar::BUTTON_POSITION_LEFT);
322 
323  $cancelButton = $this->view->getModuleTemplate()->getDocHeaderComponent()->getButtonBar()->makeLinkButton()
324  ->setIcon($this->view->getModuleTemplate()->getIconFactory()->getIcon('actions-document-close', Icon::SIZE_SMALL))
325  ->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang.xlf:button.cancel'))
326  ->setClasses('menuItem cancelItem disabled t3js-button-cancel')
327  ->setDataAttributes(['action' => 'cancelLanguageUpdate'])
328  ->setHref('#');
329 
330  $this->view->getModuleTemplate()->getDocHeaderComponent()->getButtonBar()->addButton($cancelButton, ButtonBar::BUTTON_POSITION_LEFT);
331  }
332 }
injectExtensionRepository(\TYPO3\CMS\Lang\Domain\Repository\ExtensionRepository $extensionRepository)
static translate($key, $extensionName, $arguments=null)
injectTranslationService(\TYPO3\CMS\Lang\Service\TranslationService $translationService)
injectLanguageRepository(\TYPO3\CMS\Lang\Domain\Repository\LanguageRepository $languageRepository)
injectRegistryService(\TYPO3\CMS\Lang\Service\RegistryService $registryService)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']