2 namespace TYPO3\CMS\Lang\Domain\Repository
;
5 * This file is part of the TYPO3 CMS project.
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.
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
14 * The TYPO3 project - inspiring people to share!
17 use TYPO3\CMS\Core\Configuration\ConfigurationManager
;
18 use TYPO3\CMS\Core\Localization\Locales
;
19 use TYPO3\CMS\Core\Utility\GeneralUtility
;
20 use TYPO3\CMS\Extbase\
Object\ObjectManagerInterface
;
21 use TYPO3\CMS\Lang\Domain\Model\Language
;
22 use TYPO3\CMS\Lang\Service\RegistryService
;
27 class LanguageRepository
30 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
32 protected $objectManager;
35 * @var \TYPO3\CMS\Core\Localization\Locales
40 * @var \TYPO3\CMS\Lang\Domain\Model\Language[]
42 protected $selectedLocales = array();
45 * @var \TYPO3\CMS\Lang\Domain\Model\Language[]
47 protected $languages = array();
52 protected $configurationPath = 'EXTCONF/lang';
55 * @var \TYPO3\CMS\Lang\Service\RegistryService
57 protected $registryService;
60 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
62 public function injectObjectManager(ObjectManagerInterface
$objectManager)
64 $this->objectManager
= $objectManager;
68 * @param \TYPO3\CMS\Core\Localization\Locales $locales
70 public function injectLocales(Locales
$locales)
72 $this->locales
= $locales;
76 * @param \TYPO3\CMS\Lang\Service\RegistryService $registryService
78 public function injectRegistryService(RegistryService
$registryService)
80 $this->registryService
= $registryService;
84 * Constructor of the language repository
86 public function __construct()
88 $configurationManager = GeneralUtility
::makeInstance(ConfigurationManager
::class);
90 $globalSettings = $configurationManager->getLocalConfigurationValueByPath($this->configurationPath
);
91 $this->selectedLocales
= (array)$globalSettings['availableLanguages'];
92 } catch (\Exception
$e) {
93 $configurationManager->setLocalConfigurationValueByPath(
94 $this->configurationPath
,
95 array('availableLanguages' => array())
101 * Returns all objects of this repository
103 * @return \TYPO3\CMS\Lang\Domain\Model\Language[] The language objects
105 public function findAll()
107 if (empty($this->languages
)) {
108 $languages = $this->locales
->getLanguages();
109 array_shift($languages);
110 foreach ($languages as $locale => $language) {
111 $label = htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:setup/Resources/Private/Language/locallang.xlf:lang_' . $locale));
113 $label = htmlspecialchars($language);
115 $this->languages
[$locale] = $this->objectManager
->get(
119 in_array($locale, $this->selectedLocales
),
120 $this->registryService
->get($locale)
123 usort($this->languages
, function ($a, $b) {
124 /** @var $a \TYPO3\CMS\Lang\Domain\Model\Language */
125 /** @var $b \TYPO3\CMS\Lang\Domain\Model\Language */
126 if ($a->getLabel() == $b->getLabel()) {
129 return $a->getLabel() < $b->getLabel() ?
-1 : 1;
132 return $this->languages
;
136 * Find selected languages
138 * @return \TYPO3\CMS\Lang\Domain\Model\Language[] The language objects
140 public function findSelected()
142 $languages = $this->findAll();
144 foreach ($languages as $language) {
145 if ($language->getSelected()) {
146 $result[] = $language;
153 * Update selected languages
155 * @param array $languages The languages
156 * @return array Update information
158 public function updateSelectedLanguages($languages)
160 // Add possible dependencies for selected languages
161 $dependencies = array();
162 foreach ($languages as $language) {
163 $dependencies = array_merge($dependencies, $this->locales
->getLocaleDependencies($language));
165 if (!empty($dependencies)) {
166 $languages = array_unique(array_merge($languages, $dependencies));
168 $dir = count($languages) - count($this->selectedLocales
);
169 $diff = $dir < 0 ?
array_diff($this->selectedLocales
, $languages) : array_diff($languages, $this->selectedLocales
);
170 GeneralUtility
::makeInstance(ConfigurationManager
::class)->setLocalConfigurationValueByPath(
171 $this->configurationPath
,
172 array('availableLanguages' => $languages)
175 'success' => !empty($diff),
177 'diff' => array_values($diff),
178 'languages' => $languages
183 * Add a language to list of selected languages
185 * @param string $locale The locale
186 * @return array Update information
188 public function activateByLocale($locale)
190 $languages = $this->findAll();
192 foreach ($languages as $language) {
193 if ($language->getSelected() ||
$language->getLocale() === $locale) {
194 $locales[] = $language->getLocale();
197 return $this->updateSelectedLanguages($locales);
201 * Remove a language from list of selected languages
203 * @param string $locale The locale
204 * @return array Update information
206 public function deactivateByLocale($locale)
208 $languages = $this->findAll();
210 foreach ($languages as $language) {
211 if ($language->getSelected() && $language->getLocale() !== $locale) {
212 $locales[] = $language->getLocale();
215 return $this->updateSelectedLanguages($locales);
219 * Returns LanguageService
221 * @return \TYPO3\CMS\Lang\LanguageService
223 protected function getLanguageService()
225 return $GLOBALS['LANG'];