TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
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  */
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 
105  public function listLanguagesAction()
106  {
107  $this->prepareDocHeaderMenu();
108  $this->prepareDocHeaderButtons();
109 
110  $languages = $this->languageRepository->findAll();
111  $this->view->assign('languages', $languages);
112  }
113 
119  public function listTranslationsAction()
120  {
121  $this->prepareDocHeaderMenu();
122 
123  $languages = $this->languageRepository->findSelected();
124  $this->view->assign('languages', $languages);
125  }
126 
132  public function getTranslationsAction()
133  {
134  $this->view->assign('extensions', $this->extensionRepository->findAll());
135  $this->view->assign('languages', $this->languageRepository->findSelected());
136  }
137 
144  public function updateLanguageAction(array $data)
145  {
146  $numberOfExtensionsToUpdate = 10;
147  $response = [
148  'success' => false,
149  'progress' => 0,
150  ];
151  $progress = 0;
152  if (!empty($data['locale'])) {
153  $allCount = 0;
154  for ($i = 0; $i < $numberOfExtensionsToUpdate; $i++) {
155  $offset = (int)$data['count'] * $numberOfExtensionsToUpdate + $i;
157  $extension = $this->extensionRepository->findOneByOffset($offset);
158  if (empty($extension)) {
159  // No more extensions to update
160  break;
161  }
162  if ($allCount === 0) {
163  $allCount = (int)$this->extensionRepository->countAll();
164  }
165  $extensionKey = $extension->getKey();
166  $result = $this->translationService->updateTranslation($extensionKey, $data['locale']);
167  $progress = round((($offset + 1) * 100) / $allCount, 2);
168  $response['result'][$data['locale']][$extensionKey] = $result[$data['locale']];
169  if (empty($result[$data['locale']]['error'])) {
170  $response['success'] = true;
171  } else {
172  // Could not update an extension, stop here!
173  $response['success'] = false;
174  break;
175  }
176  }
177  }
178  if ($response['success']) {
179  $this->registryService->set($data['locale'], $GLOBALS['EXEC_TIME']);
180  $response['timestamp'] = $GLOBALS['EXEC_TIME'];
181  $response['progress'] = $progress > 100 ? 100 : $progress;
182  }
183  $this->view->assign('response', $response);
184  // Flush language cache
185  GeneralUtility::makeInstance(CacheManager::class)->getCache('l10n')->flush();
186  }
187 
194  public function updateTranslationAction(array $data)
195  {
196  $response = ['success' => false];
197  if (!empty($data['extension']) && !empty($data['locale'])) {
198  $result = $this->translationService->updateTranslation($data['extension'], $data['locale']);
199  if (empty($result[$data['extension']][$data['locale']]['error'])) {
200  $response = [
201  'success' => true,
202  'result' => $result,
203  ];
204  }
205  }
206  $this->view->assign('response', $response);
207  // Flush language cache
208  GeneralUtility::makeInstance(CacheManager::class)->getCache('l10n')->flush();
209  }
210 
217  public function activateLanguageAction(array $data)
218  {
219  $response = ['success' => false];
220  if (!empty($data['locale'])) {
221  $response = $this->languageRepository->activateByLocale($data['locale']);
222  }
223  $this->view->assign('response', $response);
224  }
225 
232  public function deactivateLanguageAction(array $data)
233  {
234  $response = ['success' => false];
235  if (!empty($data['locale'])) {
236  $response = $this->languageRepository->deactivateByLocale($data['locale']);
237  }
238  $this->view->assign('response', $response);
239  }
240 
247  public function removeLanguageAction(array $data)
248  {
249  $response = ['success' => false];
250  if (!empty($data['locale'])) {
251  $response = $this->languageRepository->deactivateByLocale($data['locale']);
252  $absoluteLanguagePath = GeneralUtility::getFileAbsFileName(PATH_typo3conf . 'l10n/' . $data['locale']);
253  GeneralUtility::rmdir($absoluteLanguagePath, true);
254  }
255  $this->view->assign('response', $response);
256  }
257 
261  protected function prepareDocHeaderMenu()
262  {
263  $this->view->getModuleTemplate()->setModuleName('typo3-module-lang');
264  $this->view->getModuleTemplate()->setModuleId('typo3-module-lang');
265 
266  $this->view->getModuleTemplate()->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Lang/LanguageModule');
267 
268  $extensionKey = 'lang';
269  $addJsInlineLabels = [
270  'flashmessage.error',
271  'flashmessage.information',
272  'flashmessage.success',
273  'flashmessage.multipleErrors',
274  'flashmessage.updateComplete',
275  'flashmessage.canceled',
276  'flashmessage.languageActivated',
277  'flashmessage.languageDeactivated',
278  'flashmessage.languageRemoved',
279  'flashmessage.noLanguageActivated',
280  'flashmessage.errorOccurred',
281  'table.processing',
282  'table.search',
283  'table.loadingRecords',
284  'table.zeroRecords',
285  'table.emptyTable',
286  'table.dateFormat',
287  ];
288  foreach ($addJsInlineLabels as $key) {
289  $label = LocalizationUtility::translate($key, $extensionKey);
290  $this->view->getModuleTemplate()->getPageRenderer()->addInlineLanguageLabel($key, $label);
291  }
292 
293  $uriBuilder = $this->objectManager->get(UriBuilder::class);
294  $uriBuilder->setRequest($this->request);
295 
297  $menu = GeneralUtility::makeInstance(Menu::class);
298  $menu->setIdentifier('_languageMenu');
299  $menu->setLabel($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_general.xlf:LGL.language'));
300 
302  $languageListMenuItem = GeneralUtility::makeInstance(MenuItem::class);
303  $action = 'listLanguages';
304  $isActive = $this->request->getControllerActionName() === $action ? true : false;
305  $languageListMenuItem->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang.xlf:header.languages'));
306  $uri = $uriBuilder->reset()->uriFor('listLanguages', [], 'Language');
307  $languageListMenuItem->setHref($uri)->setActive($isActive);
308 
310  $translationMenuItem = GeneralUtility::makeInstance(MenuItem::class);
311  $action = 'listTranslations';
312  $isActive = $this->request->getControllerActionName() === $action ? true : false;
313  $translationMenuItem->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang.xlf:header.translations'));
314  $uri = $uriBuilder->reset()->uriFor('listTranslations', [], 'Language');
315  $translationMenuItem->setHref($uri)->setActive($isActive);
316 
317  $menu->addMenuItem($languageListMenuItem);
318  $menu->addMenuItem($translationMenuItem);
319  $this->view->getModuleTemplate()->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
320  $this->view->getModuleTemplate()->setFlashMessageQueue($this->controllerContext->getFlashMessageQueue());
321  }
322 
328  protected function getLanguageService()
329  {
330  return $GLOBALS['LANG'];
331  }
332 
336  protected function prepareDocHeaderButtons()
337  {
338  // @todo: the html structure needed to operate the buttons correctly is broken now.
339  // @todo: LanguageModule.js and backend.css -> div.typo3-module-lang div.menuItems
340 
341  $downloadAllButton = $this->view->getModuleTemplate()->getDocHeaderComponent()->getButtonBar()->makeLinkButton()
342  ->setIcon($this->view->getModuleTemplate()->getIconFactory()->getIcon('actions-system-extension-download', Icon::SIZE_SMALL))
343  ->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang.xlf:button.downloadAll'))
344  ->setClasses('menuItem updateItem t3js-button-update')
345  ->setDataAttributes(['action' => 'updateActiveLanguages'])
346  ->setHref('#');
347  $this->view->getModuleTemplate()->getDocHeaderComponent()->getButtonBar()->addButton($downloadAllButton, ButtonBar::BUTTON_POSITION_LEFT);
348 
349  $cancelButton = $this->view->getModuleTemplate()->getDocHeaderComponent()->getButtonBar()->makeLinkButton()
350  ->setIcon($this->view->getModuleTemplate()->getIconFactory()->getIcon('actions-document-close', Icon::SIZE_SMALL))
351  ->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang.xlf:button.cancel'))
352  ->setClasses('menuItem cancelItem disabled t3js-button-cancel')
353  ->setDataAttributes(['action' => 'cancelLanguageUpdate'])
354  ->setHref('#');
355 
356  $this->view->getModuleTemplate()->getDocHeaderComponent()->getButtonBar()->addButton($cancelButton, ButtonBar::BUTTON_POSITION_LEFT);
357  }
358 }
injectExtensionRepository(ExtensionRepository $extensionRepository)
static rmdir($path, $removeNonEmpty=false)
injectRegistryService(RegistryService $registryService)
static translate($key, $extensionName, $arguments=null)
if(TYPO3_MODE=== 'BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
static makeInstance($className,...$constructorArguments)
injectLanguageRepository(LanguageRepository $languageRepository)
injectTranslationService(TranslationService $translationService)
static getFileAbsFileName($filename, $_=null, $_2=null)