From 7149e84176360be619c79f40b8afff5a9115ac41 Mon Sep 17 00:00:00 2001 From: Ingolf Steinardt Date: Wed, 10 Jul 2024 13:30:16 +0200 Subject: [PATCH] Fix translations for untrans MetaModel --- src/CoreBundle/Resources/config/services.yml | 1 + .../translations/tl_metamodel_item.de.xlf | 2 +- .../MetaModelTranslatorConfigurator.php | 19 ++++++++++++++----- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/CoreBundle/Resources/config/services.yml b/src/CoreBundle/Resources/config/services.yml index 339dcbe34..cc3493219 100644 --- a/src/CoreBundle/Resources/config/services.yml +++ b/src/CoreBundle/Resources/config/services.yml @@ -303,6 +303,7 @@ services: arguments: $factory: '@metamodels.factory' $cache: '@metamodels.cache_internal' + $localeProvider: '@contao.intl.locales' MetaModels\CoreBundle\Translator\Translator: decorates: 'contao.translation.translator' diff --git a/src/CoreBundle/Resources/translations/tl_metamodel_item.de.xlf b/src/CoreBundle/Resources/translations/tl_metamodel_item.de.xlf index 981e4e61b..0431fb4f2 100644 --- a/src/CoreBundle/Resources/translations/tl_metamodel_item.de.xlf +++ b/src/CoreBundle/Resources/translations/tl_metamodel_item.de.xlf @@ -192,4 +192,4 @@ - \ No newline at end of file + diff --git a/src/CoreBundle/Translator/MetaModelTranslatorConfigurator.php b/src/CoreBundle/Translator/MetaModelTranslatorConfigurator.php index b70ed2490..29cfe8087 100644 --- a/src/CoreBundle/Translator/MetaModelTranslatorConfigurator.php +++ b/src/CoreBundle/Translator/MetaModelTranslatorConfigurator.php @@ -20,12 +20,18 @@ namespace MetaModels\CoreBundle\Translator; +use Contao\CoreBundle\Intl\Locales; use MetaModels\IFactory; use MetaModels\ITranslatedMetaModel; use Psr\Cache\InvalidArgumentException; use Symfony\Component\Translation\Translator as SymfonyTranslator; use Symfony\Contracts\Cache\CacheInterface; +use function array_unique; +use function array_values; +use function call_user_func; +use function in_array; + /** @psalm-type TDomainList=iterable> */ final class MetaModelTranslatorConfigurator { @@ -42,6 +48,7 @@ final class MetaModelTranslatorConfigurator public function __construct( private readonly IFactory $factory, private readonly CacheInterface $cache, + private readonly Locales $localeProvider, $previous = null ) { if (null !== $previous && !is_callable($previous)) { @@ -61,7 +68,7 @@ public function __invoke(SymfonyTranslator $translator): void { // Apply previous configurator if (null !== $this->previous) { - \call_user_func($this->previous, $translator); + call_user_func($this->previous, $translator); } foreach ($this->fetchDomains() as $domain => $locales) { @@ -84,22 +91,24 @@ private function fetchDomains(): iterable /** @return TDomainList */ function (): iterable { $result = []; + + $installedLanguages = $this->localeProvider->getEnabledLocaleIds(); foreach ($this->factory->collectNames() as $metamodelName) { $instance = $this->factory->getMetaModel($metamodelName); if (!$instance instanceof ITranslatedMetaModel) { - $result[$metamodelName] = ['en']; + $result[$metamodelName] = $installedLanguages; continue; } - $locales = []; + $locales = $installedLanguages; foreach ($instance->getLanguages() as $language) { $locales[] = $language; } // Fix: Always add 'en' to the language domains, even if user only set 'af_NA' by quick save. - if (!\in_array('en', $locales, true)) { + if (!in_array('en', $locales, true)) { array_unshift($locales, 'en'); } - $result[$metamodelName] = $locales; + $result[$metamodelName] = array_values(array_unique($locales)); } return $result;