Skip to content

Commit

Permalink
Fix translations for untrans MetaModel
Browse files Browse the repository at this point in the history
  • Loading branch information
zonky2 committed Jul 10, 2024
1 parent fe52585 commit 7149e84
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/CoreBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,4 @@
</trans-unit>
</body>
</file>
</xliff>
</xliff>
19 changes: 14 additions & 5 deletions src/CoreBundle/Translator/MetaModelTranslatorConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, iterable<int, string>> */
final class MetaModelTranslatorConfigurator
{
Expand All @@ -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)) {
Expand All @@ -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) {
Expand All @@ -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;
Expand Down

0 comments on commit 7149e84

Please sign in to comment.