Commit ca78178c authored by Benni Mack's avatar Benni Mack
Browse files

[TASK] Cleanup various LanguageService-related code

Over the past few commits, a lot of changes
happened with LanguageService-related code, mainly
to streamline the initialization and creation
of LanguageService code.

For this reason, various parts have now been sorted
out:
* Proper injection of Locales class
* Consistent usage of LanguageService->init()
* Avoid additional pageRendererSetupTrait call in LoginController
* Avoid using LanguageService->init() if Factory is used
* Leftover "ISOcode" value is removed

Resolves: #99693
Releases: main
Change-Id: Ib4bbc5b38505d712c86eb7e6a3c8455ba49225de
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/77557


Tested-by: Benni Mack's avatarBenni Mack <benni@typo3.org>
Tested-by: Christian Kuhn's avatarChristian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Susanne Moog's avatarSusanne Moog <look@susi.dev>
Tested-by: core-ci's avatarcore-ci <typo3@b13.com>
Reviewed-by: Christian Kuhn's avatarChristian Kuhn <lolli@schwarzbu.ch>
Tested-by: Susanne Moog's avatarSusanne Moog <look@susi.dev>
Reviewed-by: Benni Mack's avatarBenni Mack <benni@typo3.org>
parent 82868d6d
......@@ -96,7 +96,6 @@ class TranslationConfigurationProvider
$allSystemLanguages[$languageId] = [
'uid' => $languageId,
'title' => $language->getTitle() . ($includeSiteSuffix ? ' [Site: ' . $site->getIdentifier() . ']' : ''),
'ISOcode' => $language->getTwoLetterIsoCode(),
'flagIcon' => $language->getFlagIdentifier(),
];
}
......
......@@ -115,7 +115,8 @@ class LoginController
protected readonly LoginProviderResolver $loginProviderResolver,
protected readonly ExtensionConfiguration $extensionConfiguration,
protected readonly BackendEntryPointResolver $backendEntryPointResolver,
protected readonly FormProtectionFactory $formProtectionFactory
protected readonly FormProtectionFactory $formProtectionFactory,
protected readonly Locales $locales,
) {
}
......@@ -194,6 +195,16 @@ class LoginController
$parsedBody = $request->getParsedBody();
$queryParams = $request->getQueryParams();
// Try to get the preferred browser language
$httpAcceptLanguage = $request->getServerParams()['HTTP_ACCEPT_LANGUAGE'] ?? '';
$preferredBrowserLanguage = $this->locales->getPreferredClientLanguage($httpAcceptLanguage);
// If we found a $preferredBrowserLanguage, which is not the default language, while no user is logged in,
// initialize $this->getLanguageService()
if (empty($backendUser->user['uid'])) {
$languageService->init($preferredBrowserLanguage);
}
$this->setUpBasicPageRendererForBackend($this->pageRenderer, $this->extensionConfiguration, $request, $languageService);
$this->pageRenderer->setTitle('TYPO3 CMS Login: ' . ($GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] ?? ''));
......@@ -203,16 +214,6 @@ class LoginController
$this->loginRefresh = (bool)($parsedBody['loginRefresh'] ?? $queryParams['loginRefresh'] ?? false);
// Value of "Login" button. If set, the login button was pressed.
$this->submitValue = $parsedBody['commandLI'] ?? $queryParams['commandLI'] ?? null;
// Try to get the preferred browser language
$httpAcceptLanguage = $request->getServerParams()['HTTP_ACCEPT_LANGUAGE'] ?? '';
$preferredBrowserLanguage = GeneralUtility::makeInstance(Locales::class)->getPreferredClientLanguage($httpAcceptLanguage);
// If we found a $preferredBrowserLanguage, and it is not the default language and no be_user is logged in,
// initialize $this->getLanguageService() again with $preferredBrowserLanguage
if ($preferredBrowserLanguage !== 'default' && empty($backendUser->user['uid'])) {
$languageService->init($preferredBrowserLanguage);
$this->pageRenderer->setLanguage($preferredBrowserLanguage);
}
// Setting the redirect URL to "index.php?M=main" if no alternative input is given
if ($this->redirectUrl) {
......
......@@ -197,11 +197,10 @@ class ResetPasswordController
$httpAcceptLanguage = $request->getServerParams()['HTTP_ACCEPT_LANGUAGE'] ?? '';
$preferredBrowserLanguage = $this->locales->getPreferredClientLanguage($httpAcceptLanguage);
// If we found a $preferredBrowserLanguage and it is not the default language
// If we found a $preferredBrowserLanguage, which is not the default language
// initialize $this->getLanguageService() again with $preferredBrowserLanguage
if ($preferredBrowserLanguage !== 'default') {
$languageService->init($preferredBrowserLanguage);
$this->pageRenderer->setLanguage($preferredBrowserLanguage);
}
$this->setUpBasicPageRendererForBackend($this->pageRenderer, $this->extensionConfiguration, $request, $languageService);
......
......@@ -30,6 +30,7 @@ use TYPO3\CMS\Core\FormProtection\FormProtectionFactory;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Core\Information\Typo3Information;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Localization\Locales;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\Routing\BackendEntryPointResolver;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
......@@ -60,6 +61,7 @@ class LoginControllerTest extends UnitTestCase
new ExtensionConfiguration(),
new BackendEntryPointResolver(),
$formProtectionFactory,
new Locales(),
],
);
......
......@@ -159,7 +159,7 @@ class TcaColumnsProcessFieldDescriptionsTest extends UnitTestCase
$this->createMock(LocalizationFactory::class),
$this->createMock(FrontendInterface::class)
);
$GLOBALS['LANG']->lang = 'fr';
$GLOBALS['LANG']->init('fr');
$expected = $input;
$expected['processedTca']['columns']['aField']['description'] = 'aDescriptionOverride';
......
......@@ -191,7 +191,7 @@ class TcaColumnsProcessFieldLabelsTest extends UnitTestCase
$this->createMock(LocalizationFactory::class),
$this->createMock(FrontendInterface::class)
);
$GLOBALS['LANG']->lang = 'fr';
$GLOBALS['LANG']->init('fr');
$expected = $input;
$expected['processedTca']['columns']['aField']['label'] = 'aLabelOverride';
......
......@@ -121,7 +121,6 @@ class LocalizationUtility
$cache = self::getRuntimeCache();
if (!$cache->get($languageKeyHash)) {
$languageService = GeneralUtility::makeInstance(LanguageServiceFactory::class)->create($languageKey);
$languageService->init($languageKey);
if ($alternativeLanguageKeys !== []) {
$languageService->setDependencies($alternativeLanguageKeys);
}
......
......@@ -497,7 +497,6 @@ class TranslationService implements SingletonInterface
$languageKeyHash = sha1($languageKey . '_' . $languageFilePath);
if (!$this->runtimeCache->get($languageKeyHash)) {
$languageService = $this->languageServiceFactory->create($languageKey);
$languageService->init($languageKey);
if ($languageFilePath) {
$languageService->includeLLFile($languageFilePath);
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment