1<?php
2
3declare(strict_types=1);
4
5/*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18namespace TYPO3\CMS\Core\Localization;
19
20use TYPO3\CMS\Core\Authentication\AbstractUserAuthentication;
21use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
22use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
23
24class LanguageServiceFactory
25{
26    protected Locales $locales;
27    protected LocalizationFactory $localizationFactory;
28    protected FrontendInterface $runtimeCache;
29
30    public function __construct(
31        Locales $locales,
32        LocalizationFactory $localizationFactory,
33        FrontendInterface $runtimeCache
34    ) {
35        $this->locales = $locales;
36        $this->localizationFactory = $localizationFactory;
37        $this->runtimeCache = $runtimeCache;
38    }
39
40    /**
41     * Factory method to create a language service object.
42     *
43     * @param string $locale the locale (= the TYPO3-internal locale given)
44     * @return LanguageService
45     */
46    public function create(string $locale): LanguageService
47    {
48        $obj = new LanguageService($this->locales, $this->localizationFactory, $this->runtimeCache);
49        $obj->init($locale);
50        return $obj;
51    }
52
53    public function createFromUserPreferences(?AbstractUserAuthentication $user): LanguageService
54    {
55        if ($user && ($user->user['lang'] ?? false)) {
56            return $this->create($user->user['lang']);
57        }
58        return $this->create('default');
59    }
60
61    public function createFromSiteLanguage(SiteLanguage $language): LanguageService
62    {
63        return $this->create($language->getTypo3Language());
64    }
65}
66