TYPO3 CMS  TYPO3_8-7
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  */
16 
32 
37 {
41  protected $defaultViewObjectName = BackendTemplateView::class;
42 
46  protected $view;
47 
52 
57 
62 
66  protected $registryService;
67 
72  {
73  $this->languageRepository = $languageRepository;
74  }
75 
80  {
81  $this->extensionRepository = $extensionRepository;
82  }
83 
88  {
89  $this->translationService = $translationService;
90  }
91 
96  {
97  $this->registryService = $registryService;
98  }
99 
103  public function listLanguagesAction()
104  {
105  $this->prepareDocHeaderMenu();
106  $this->prepareDocHeaderButtons();
107 
108  $languages = $this->languageRepository->findAll();
109  $this->view->assign('languages', $languages);
110  }
111 
115  public function listTranslationsAction()
116  {
117  $this->prepareDocHeaderMenu();
118 
119  $languages = $this->languageRepository->findSelected();
120  $this->view->assign('languages', $languages);
121  }
122 
126  public function getTranslationsAction()
127  {
128  $this->view->assign('extensions', $this->extensionRepository->findAll());
129  $this->view->assign('languages', $this->languageRepository->findSelected());
130  }
131 
137  public function updateLanguageAction(array $data)
138  {
139  $numberOfExtensionsToUpdate = 10;
140  $response = [
141  'success' => false,
142  'progress' => 0,
143  ];
144  $progress = 0;
145  if (!empty($data['locale'])) {
146  $allCount = 0;
147  for ($i = 0; $i < $numberOfExtensionsToUpdate; $i++) {
148  $offset = (int)$data['count'] * $numberOfExtensionsToUpdate + $i;
150  $extension = $this->extensionRepository->findOneByOffset($offset);
151  if (empty($extension)) {
152  // No more extensions to update
153  break;
154  }
155  if ($allCount === 0) {
156  $allCount = (int)$this->extensionRepository->countAll();
157  }
158  $extensionKey = $extension->getKey();
159  $result = $this->translationService->updateTranslation($extensionKey, $data['locale']);
160  $progress = round((($offset + 1) * 100) / $allCount, 2);
161  $response['result'][$data['locale']][$extensionKey] = $result[$data['locale']];
162  if (empty($result[$data['locale']]['error'])) {
163  $response['success'] = true;
164  } else {
165  // Could not update an extension, stop here!
166  $response['success'] = false;
167  break;
168  }
169  }
170  }
171  if ($response['success']) {
172  $this->registryService->set($data['locale'], $GLOBALS['EXEC_TIME']);
173  $response['timestamp'] = $GLOBALS['EXEC_TIME'];
174  $response['progress'] = $progress > 100 ? 100 : $progress;
175  }
176  $this->view->assign('response', $response);
177  // Flush language cache
178  GeneralUtility::makeInstance(CacheManager::class)->getCache('l10n')->flush();
179  }
180 
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 
208  public function activateLanguageAction(array $data)
209  {
210  $response = ['success' => false];
211  if (!empty($data['locale'])) {
212  $response = $this->languageRepository->activateByLocale($data['locale']);
213  }
214  $this->view->assign('response', $response);
215  }
216 
222  public function deactivateLanguageAction(array $data)
223  {
224  $response = ['success' => false];
225  if (!empty($data['locale'])) {
226  $response = $this->languageRepository->deactivateByLocale($data['locale']);
227  }
228  $this->view->assign('response', $response);
229  }
230 
236  public function removeLanguageAction(array $data)
237  {
238  $response = ['success' => false];
239  if (!empty($data['locale'])) {
240  $response = $this->languageRepository->deactivateByLocale($data['locale']);
241  $absoluteLanguagePath = GeneralUtility::getFileAbsFileName(PATH_typo3conf . 'l10n/' . $data['locale']);
242  GeneralUtility::rmdir($absoluteLanguagePath, true);
243  }
244  $this->view->assign('response', $response);
245  }
246 
250  protected function prepareDocHeaderMenu()
251  {
252  $this->view->getModuleTemplate()->setModuleName('typo3-module-lang');
253  $this->view->getModuleTemplate()->setModuleId('typo3-module-lang');
254 
255  $this->view->getModuleTemplate()->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Lang/LanguageModule');
256 
257  $extensionKey = 'lang';
258  $addJsInlineLabels = [
259  'flashmessage.error',
260  'flashmessage.information',
261  'flashmessage.success',
262  'flashmessage.multipleErrors',
263  'flashmessage.updateComplete',
264  'flashmessage.canceled',
265  'flashmessage.languageActivated',
266  'flashmessage.languageDeactivated',
267  'flashmessage.languageRemoved',
268  'flashmessage.noLanguageActivated',
269  'flashmessage.errorOccurred',
270  'table.processing',
271  'table.search',
272  'table.loadingRecords',
273  'table.zeroRecords',
274  'table.emptyTable',
275  'table.dateFormat',
276  ];
277  foreach ($addJsInlineLabels as $key) {
278  $label = LocalizationUtility::translate($key, $extensionKey);
279  $this->view->getModuleTemplate()->getPageRenderer()->addInlineLanguageLabel($key, $label);
280  }
281 
282  $uriBuilder = $this->objectManager->get(UriBuilder::class);
283  $uriBuilder->setRequest($this->request);
284 
286  $menu = GeneralUtility::makeInstance(Menu::class);
287  $menu->setIdentifier('_languageMenu');
288  $menu->setLabel($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_general.xlf:LGL.language'));
289 
291  $languageListMenuItem = GeneralUtility::makeInstance(MenuItem::class);
292  $action = 'listLanguages';
293  $isActive = $this->request->getControllerActionName() === $action ? true : false;
294  $languageListMenuItem->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang.xlf:header.languages'));
295  $uri = $uriBuilder->reset()->uriFor('listLanguages', [], 'Language');
296  $languageListMenuItem->setHref($uri)->setActive($isActive);
297 
299  $translationMenuItem = GeneralUtility::makeInstance(MenuItem::class);
300  $action = 'listTranslations';
301  $isActive = $this->request->getControllerActionName() === $action ? true : false;
302  $translationMenuItem->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang.xlf:header.translations'));
303  $uri = $uriBuilder->reset()->uriFor('listTranslations', [], 'Language');
304  $translationMenuItem->setHref($uri)->setActive($isActive);
305 
306  $menu->addMenuItem($languageListMenuItem);
307  $menu->addMenuItem($translationMenuItem);
308  $this->view->getModuleTemplate()->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
309  $this->view->getModuleTemplate()->setFlashMessageQueue($this->controllerContext->getFlashMessageQueue());
310  }
311 
317  protected function getLanguageService()
318  {
319  return $GLOBALS['LANG'];
320  }
321 
325  protected function prepareDocHeaderButtons()
326  {
327  // @todo: the html structure needed to operate the buttons correctly is broken now.
328  // @todo: LanguageModule.js and backend.css -> div.typo3-module-lang div.menuItems
329 
330  $downloadAllButton = $this->view->getModuleTemplate()->getDocHeaderComponent()->getButtonBar()->makeLinkButton()
331  ->setIcon($this->view->getModuleTemplate()->getIconFactory()->getIcon('actions-system-extension-download', Icon::SIZE_SMALL))
332  ->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang.xlf:button.downloadAll'))
333  ->setClasses('menuItem updateItem t3js-button-update')
334  ->setDataAttributes(['action' => 'updateActiveLanguages'])
335  ->setHref('#');
336  $this->view->getModuleTemplate()->getDocHeaderComponent()->getButtonBar()->addButton($downloadAllButton, ButtonBar::BUTTON_POSITION_LEFT);
337 
338  $cancelButton = $this->view->getModuleTemplate()->getDocHeaderComponent()->getButtonBar()->makeLinkButton()
339  ->setIcon($this->view->getModuleTemplate()->getIconFactory()->getIcon('actions-close', Icon::SIZE_SMALL))
340  ->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang.xlf:button.cancel'))
341  ->setClasses('menuItem cancelItem disabled t3js-button-cancel')
342  ->setDataAttributes(['action' => 'cancelLanguageUpdate'])
343  ->setHref('#');
344 
345  $this->view->getModuleTemplate()->getDocHeaderComponent()->getButtonBar()->addButton($cancelButton, ButtonBar::BUTTON_POSITION_LEFT);
346  }
347 }
injectLanguageRepository(LanguageRepository $languageRepository)
static getFileAbsFileName($filename, $_=null, $_2=null)
static translate($key, $extensionName=null, $arguments=null)
static makeInstance($className,... $constructorArguments)
injectExtensionRepository(ExtensionRepository $extensionRepository)
injectRegistryService(RegistryService $registryService)
static rmdir($path, $removeNonEmpty=false)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
injectTranslationService(TranslationService $translationService)