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\Utility\GeneralUtility
;
22 class LanguageRepository
{
25 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
28 protected $objectManager;
31 * @var \TYPO3\CMS\Core\Localization\Locales
37 * @var \TYPO3\CMS\Lang\Domain\Model\Language[]
39 protected $selectedLocales = array();
42 * @var \TYPO3\CMS\Lang\Domain\Model\Language[]
44 protected $languages = array();
49 protected $configurationPath = 'EXTCONF/lang';
52 * @var \TYPO3\CMS\Lang\Service\RegistryService
55 protected $registryService;
58 * Constructor of the language repository
60 public function __construct() {
61 $configurationManager = GeneralUtility
::makeInstance(\TYPO3\CMS\Core\Configuration\ConfigurationManager
::class);
63 $globalSettings = $configurationManager->getLocalConfigurationValueByPath($this->configurationPath
);
64 $this->selectedLocales
= (array)$globalSettings['availableLanguages'];
65 } catch (\Exception
$e) {
66 $configurationManager->setLocalConfigurationValueByPath(
67 $this->configurationPath
,
68 array('availableLanguages' => array())
74 * Returns all objects of this repository
76 * @return \TYPO3\CMS\Lang\Domain\Model\Language[] The language objects
78 public function findAll() {
79 if (empty($this->languages
)) {
80 $languages = $this->locales
->getLanguages();
81 array_shift($languages);
82 foreach ($languages as $locale => $language) {
83 $label = htmlspecialchars($GLOBALS['LANG']->sL('LLL:EXT:setup/Resources/Private/Language/locallang.xlf:lang_' . $locale));
85 $label = htmlspecialchars($language);
87 $this->languages
[$locale] = $this->objectManager
->get(
88 \TYPO3\CMS\Lang\Domain\Model\Language
::class,
91 in_array($locale, $this->selectedLocales
),
92 $this->registryService
->get($locale)
95 usort($this->languages
, function($a, $b) {
96 /** @var $a \TYPO3\CMS\Lang\Domain\Model\Language */
97 /** @var $b \TYPO3\CMS\Lang\Domain\Model\Language */
98 if ($a->getLabel() == $b->getLabel()) {
101 return $a->getLabel() < $b->getLabel() ?
-1 : 1;
104 return $this->languages
;
108 * Find selected languages
110 * @return \TYPO3\CMS\Lang\Domain\Model\Language[] The language objects
112 public function findSelected() {
113 $languages = $this->findAll();
115 foreach ($languages as $language) {
116 if ($language->getSelected()) {
117 $result[] = $language;
124 * Update selected languages
126 * @param array $languages The languages
127 * @return array Update information
129 public function updateSelectedLanguages($languages) {
130 // Add possible dependencies for selected languages
131 $dependencies = array();
132 foreach ($languages as $language) {
133 $dependencies = array_merge($dependencies, $this->locales
->getLocaleDependencies($language));
135 if (!empty($dependencies)) {
136 $languages = array_unique(array_merge($languages, $dependencies));
138 $dir = count($languages) - count($this->selectedLocales
);
139 $diff = $dir < 0 ?
array_diff($this->selectedLocales
, $languages) : array_diff($languages, $this->selectedLocales
);
140 GeneralUtility
::makeInstance(\TYPO3\CMS\Core\Configuration\ConfigurationManager
::class)->setLocalConfigurationValueByPath(
141 $this->configurationPath
,
142 array('availableLanguages' => $languages)
145 'success' => !empty($diff),
147 'diff' => array_values($diff),
148 'languages' => $languages
153 * Add a language to list of selected languages
155 * @param string $locale The locale
156 * @return array Update information
158 public function activateByLocale($locale) {
159 $languages = $this->findAll();
161 foreach ($languages as $language) {
162 if ($language->getSelected() ||
$language->getLocale() === $locale) {
163 $locales[] = $language->getLocale();
166 return $this->updateSelectedLanguages($locales);
170 * Remove a language from list of selected languages
172 * @param string $locale The locale
173 * @return array Update information
175 public function deactivateByLocale($locale) {
176 $languages = $this->findAll();
178 foreach ($languages as $language) {
179 if ($language->getSelected() && $language->getLocale() !== $locale) {
180 $locales[] = $language->getLocale();
183 return $this->updateSelectedLanguages($locales);