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 * @author Sebastian Fischer <typo3@evoweb.de>
23 * @author Kai Vogel <k.vogel@reply.de>
25 class LanguageRepository
{
28 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
31 protected $objectManager;
34 * @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
58 protected $registryService;
61 * Constructor of the language repository
63 public function __construct() {
64 $configurationManager = GeneralUtility
::makeInstance(\TYPO3\CMS\Core\Configuration\ConfigurationManager
::class);
66 $globalSettings = $configurationManager->getLocalConfigurationValueByPath($this->configurationPath
);
67 $this->selectedLocales
= (array)$globalSettings['availableLanguages'];
68 } catch (\Exception
$e) {
69 $configurationManager->setLocalConfigurationValueByPath(
70 $this->configurationPath
,
71 array('availableLanguages' => array())
77 * Returns all objects of this repository
79 * @return \TYPO3\CMS\Lang\Domain\Model\Language[] The language objects
81 public function findAll() {
82 if (!count($this->languages
)) {
83 $languages = $this->locales
->getLanguages();
84 array_shift($languages);
85 foreach ($languages as $locale => $language) {
86 $label = htmlspecialchars($GLOBALS['LANG']->sL('LLL:EXT:setup/mod/locallang.xlf:lang_' . $locale));
88 $label = htmlspecialchars($language);
90 $this->languages
[$locale] = $this->objectManager
->get(
91 \TYPO3\CMS\Lang\Domain\Model\Language
::class,
94 in_array($locale, $this->selectedLocales
),
95 $this->registryService
->get($locale)
98 usort($this->languages
, function($a, $b) {
99 /** @var $a \TYPO3\CMS\Lang\Domain\Model\Language */
100 /** @var $b \TYPO3\CMS\Lang\Domain\Model\Language */
101 if ($a->getLabel() == $b->getLabel()) {
104 return $a->getLabel() < $b->getLabel() ?
-1 : 1;
107 return $this->languages
;
111 * Find selected languages
113 * @return \TYPO3\CMS\Lang\Domain\Model\Language[] The language objects
115 public function findSelected() {
116 $languages = $this->findAll();
118 foreach ($languages as $language) {
119 if ($language->getSelected()) {
120 $result[] = $language;
127 * Update selected languages
129 * @param array $languages The languages
130 * @return array Update information
132 public function updateSelectedLanguages($languages) {
133 // Add possible dependencies for selected languages
134 $dependencies = array();
135 foreach ($languages as $language) {
136 $dependencies = array_merge($dependencies, $this->locales
->getLocaleDependencies($language));
138 if (count($dependencies)) {
139 $languages = array_unique(array_merge($languages, $dependencies));
141 $dir = count($languages) - count($this->selectedLocales
);
142 $diff = $dir < 0 ?
array_diff($this->selectedLocales
, $languages) : array_diff($languages, $this->selectedLocales
);
143 GeneralUtility
::makeInstance(\TYPO3\CMS\Core\Configuration\ConfigurationManager
::class)->setLocalConfigurationValueByPath(
144 $this->configurationPath
,
145 array('availableLanguages' => $languages)
148 'success' => count($diff) > 0,
150 'diff' => array_values($diff),
151 'languages' => $languages
156 * Add a language to list of selected languages
158 * @param string $locale The locale
159 * @return array Update information
161 public function activateByLocale($locale) {
162 $languages = $this->findAll();
164 foreach ($languages as $language) {
165 if ($language->getSelected() ||
$language->getLocale() === $locale) {
166 $locales[] = $language->getLocale();
169 return $this->updateSelectedLanguages($locales);
173 * Remove a language from list of selected languages
175 * @param string $locale The locale
176 * @return array Update information
178 public function deactivateByLocale($locale) {
179 $languages = $this->findAll();
181 foreach ($languages as $language) {
182 if ($language->getSelected() && $language->getLocale() !== $locale) {
183 $locales[] = $language->getLocale();
186 return $this->updateSelectedLanguages($locales);