[TASK] Remove second parameter of sL - Part 2/3
[Packages/TYPO3.CMS.git] / typo3 / sysext / lang / Classes / Controller / LanguageController.php
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
17 use TYPO3\CMS\Backend\Template\Components\ButtonBar;
18 use TYPO3\CMS\Backend\Template\Components\Menu\Menu;
19 use TYPO3\CMS\Backend\Template\Components\Menu\MenuItem;
20 use TYPO3\CMS\Backend\View\BackendTemplateView;
21 use TYPO3\CMS\Core\Cache\CacheManager;
22 use TYPO3\CMS\Core\Imaging\Icon;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24 use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
25 use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
26 use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
27 use TYPO3\CMS\Lang\Domain\Model\Extension;
28 use TYPO3\CMS\Lang\Domain\Repository\ExtensionRepository;
29 use TYPO3\CMS\Lang\Domain\Repository\LanguageRepository;
30 use TYPO3\CMS\Lang\Service\RegistryService;
31 use TYPO3\CMS\Lang\Service\TranslationService;
32
33 /**
34 * Language controller
35 */
36 class LanguageController extends ActionController
37 {
38 /**
39 * @var string
40 */
41 protected $defaultViewObjectName = BackendTemplateView::class;
42
43 /**
44 * @var BackendTemplateView
45 */
46 protected $view;
47
48 /**
49 * @var \TYPO3\CMS\Lang\Domain\Repository\LanguageRepository
50 */
51 protected $languageRepository;
52
53 /**
54 * @var \TYPO3\CMS\Lang\Domain\Repository\ExtensionRepository
55 */
56 protected $extensionRepository;
57
58 /**
59 * @var \TYPO3\CMS\Lang\Service\TranslationService
60 */
61 protected $translationService;
62
63 /**
64 * @var \TYPO3\CMS\Lang\Service\RegistryService
65 */
66 protected $registryService;
67
68 /**
69 * @param \TYPO3\CMS\Lang\Domain\Repository\LanguageRepository $languageRepository
70 */
71 public function injectLanguageRepository(LanguageRepository $languageRepository)
72 {
73 $this->languageRepository = $languageRepository;
74 }
75
76 /**
77 * @param \TYPO3\CMS\Lang\Domain\Repository\ExtensionRepository $extensionRepository
78 */
79 public function injectExtensionRepository(ExtensionRepository $extensionRepository)
80 {
81 $this->extensionRepository = $extensionRepository;
82 }
83
84 /**
85 * @param \TYPO3\CMS\Lang\Service\TranslationService $translationService
86 */
87 public function injectTranslationService(TranslationService $translationService)
88 {
89 $this->translationService = $translationService;
90 }
91
92 /**
93 * @param \TYPO3\CMS\Lang\Service\RegistryService $registryService
94 */
95 public function injectRegistryService(RegistryService $registryService)
96 {
97 $this->registryService = $registryService;
98 }
99
100 /**
101 * List languages
102 *
103 * @return void
104 */
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
114 /**
115 * List translations
116 *
117 * @return void
118 */
119 public function listTranslationsAction()
120 {
121 $this->prepareDocHeaderMenu();
122
123 $languages = $this->languageRepository->findSelected();
124 $this->view->assign('languages', $languages);
125 }
126
127 /**
128 * Returns the translations
129 *
130 * @return void
131 */
132 public function getTranslationsAction()
133 {
134 $this->view->assign('extensions', $this->extensionRepository->findAll());
135 $this->view->assign('languages', $this->languageRepository->findSelected());
136 }
137
138 /**
139 * Fetch all translations for given locale
140 *
141 * @param array $data The request data
142 * @return void
143 */
144 public function updateLanguageAction(array $data)
145 {
146 $numberOfExtensionsToUpdate = 10;
147 $response = array(
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;
156 /** @var Extension $extension */
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
188 /**
189 * Fetch the translation for given extension and locale
190 *
191 * @param array $data The request data
192 * @return void
193 */
194 public function updateTranslationAction(array $data)
195 {
196 $response = array('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 = array(
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
211 /**
212 * Activate a language
213 *
214 * @param array $data The request data
215 * @return void
216 */
217 public function activateLanguageAction(array $data)
218 {
219 $response = array('success' => false);
220 if (!empty($data['locale'])) {
221 $response = $this->languageRepository->activateByLocale($data['locale']);
222 }
223 $this->view->assign('response', $response);
224 }
225
226 /**
227 * Deactivate a language
228 *
229 * @param array $data The request data
230 * @return void
231 */
232 public function deactivateLanguageAction(array $data)
233 {
234 $response = array('success' => false);
235 if (!empty($data['locale'])) {
236 $response = $this->languageRepository->deactivateByLocale($data['locale']);
237 }
238 $this->view->assign('response', $response);
239 }
240
241 /**
242 * DocHeaderMenu
243 */
244 protected function prepareDocHeaderMenu()
245 {
246 $this->view->getModuleTemplate()->setModuleName('typo3-module-lang');
247 $this->view->getModuleTemplate()->setModuleId('typo3-module-lang');
248
249 $this->view->getModuleTemplate()->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Lang/LanguageModule');
250
251 $extensionKey = 'lang';
252 $addJsInlineLabels = [
253 'flashmessage.error',
254 'flashmessage.information',
255 'flashmessage.success',
256 'flashmessage.multipleErrors',
257 'flashmessage.updateComplete',
258 'flashmessage.canceled',
259 'flashmessage.languageActivated',
260 'flashmessage.languageDeactivated',
261 'flashmessage.noLanguageActivated',
262 'flashmessage.errorOccurred',
263 'table.processing',
264 'table.search',
265 'table.loadingRecords',
266 'table.zeroRecords',
267 'table.emptyTable',
268 'table.dateFormat',
269 ];
270 foreach ($addJsInlineLabels as $key) {
271 $label = LocalizationUtility::translate($key, $extensionKey);
272 $this->view->getModuleTemplate()->getPageRenderer()->addInlineLanguageLabel($key, $label);
273 }
274
275 $uriBuilder = $this->objectManager->get(UriBuilder::class);
276 $uriBuilder->setRequest($this->request);
277
278 /** @var Menu $menu */
279 $menu = GeneralUtility::makeInstance(Menu::class);
280 $menu->setIdentifier('_languageMenu');
281 $menu->setLabel($this->getLanguageService()->sL('LLL:EXT:lang/locallang_general.xlf:LGL.language'));
282
283 /** @var MenuItem $languageListMenuItem */
284 $languageListMenuItem = GeneralUtility::makeInstance(MenuItem::class);
285 $action = 'listLanguages';
286 $isActive = $this->request->getControllerActionName() === $action ? true : false;
287 $languageListMenuItem->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang.xlf:header.languages'));
288 $uri = $uriBuilder->reset()->uriFor('listLanguages', array(), 'Language');
289 $languageListMenuItem->setHref($uri)->setActive($isActive);
290
291 /** @var MenuItem $translationMenuItem */
292 $translationMenuItem = GeneralUtility::makeInstance(MenuItem::class);
293 $action = 'listTranslations';
294 $isActive = $this->request->getControllerActionName() === $action ? true : false;
295 $translationMenuItem->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang.xlf:header.translations'));
296 $uri = $uriBuilder->reset()->uriFor('listTranslations', array(), 'Language');
297 $translationMenuItem->setHref($uri)->setActive($isActive);
298
299 $menu->addMenuItem($languageListMenuItem);
300 $menu->addMenuItem($translationMenuItem);
301 $this->view->getModuleTemplate()->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
302 $this->view->getModuleTemplate()->setFlashMessageQueue($this->controllerContext->getFlashMessageQueue());
303 }
304
305 /**
306 * Returns LanguageService
307 *
308 * @return \TYPO3\CMS\Lang\LanguageService
309 */
310 protected function getLanguageService()
311 {
312 return $GLOBALS['LANG'];
313 }
314
315 /**
316 * DocHeaderButtons
317 */
318 protected function prepareDocHeaderButtons()
319 {
320 // @todo: the html structure needed to operate the buttons correctly is broken now.
321 // @todo: LanguageModule.js and backend.css -> div.typo3-module-lang div.menuItems
322
323 $downloadAllButton = $this->view->getModuleTemplate()->getDocHeaderComponent()->getButtonBar()->makeLinkButton()
324 ->setIcon($this->view->getModuleTemplate()->getIconFactory()->getIcon('actions-system-extension-download', Icon::SIZE_SMALL))
325 ->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang.xlf:button.downloadAll'))
326 ->setClasses('menuItem updateItem t3js-button-update')
327 ->setDataAttributes(['action' => 'updateActiveLanguages'])
328 ->setHref('#');
329 $this->view->getModuleTemplate()->getDocHeaderComponent()->getButtonBar()->addButton($downloadAllButton, ButtonBar::BUTTON_POSITION_LEFT);
330
331 $cancelButton = $this->view->getModuleTemplate()->getDocHeaderComponent()->getButtonBar()->makeLinkButton()
332 ->setIcon($this->view->getModuleTemplate()->getIconFactory()->getIcon('actions-document-close', Icon::SIZE_SMALL))
333 ->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang.xlf:button.cancel'))
334 ->setClasses('menuItem cancelItem disabled t3js-button-cancel')
335 ->setDataAttributes(['action' => 'cancelLanguageUpdate'])
336 ->setHref('#');
337
338 $this->view->getModuleTemplate()->getDocHeaderComponent()->getButtonBar()->addButton($cancelButton, ButtonBar::BUTTON_POSITION_LEFT);
339 }
340 }