Skip to content

Commit

Permalink
Merge branch 'hotfix/fix_trans_untrans_mm' into 'release/2.3.0'
Browse files Browse the repository at this point in the history
Fix translations for untrans MetaModel

See merge request metamodels/core!296
  • Loading branch information
zonky2 committed Jul 10, 2024
2 parents fe52585 + 7149e84 commit aac9496
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 */

Check failure on line 91 in src/CoreBundle/Translator/MetaModelTranslatorConfigurator.php

View workflow job for this annotation

GitHub Actions / PHP: 8.1 Contao: ~4.13.0

InvalidReturnType: The declared return type 'iterable<string, iterable<int, string>>' for /home/runner/work/core/core/src/corebundle/translator/metamodeltranslatorconfigurator.php:92:2736:-:closure is incorrect, got 'array<string, array<array-key, string>>' (reported by psalm)

Check failure on line 91 in src/CoreBundle/Translator/MetaModelTranslatorConfigurator.php

View workflow job for this annotation

GitHub Actions / PHP: 8.2 Contao: ~4.13.0

InvalidReturnType: The declared return type 'iterable<string, iterable<int, string>>' for /home/runner/work/core/core/src/corebundle/translator/metamodeltranslatorconfigurator.php:92:2736:-:closure is incorrect, got 'array<string, array<array-key, string>>' (reported by psalm)

Check failure on line 91 in src/CoreBundle/Translator/MetaModelTranslatorConfigurator.php

View workflow job for this annotation

GitHub Actions / PHP: 8.3 Contao: ~4.13.0

InvalidReturnType: The declared return type 'iterable<string, iterable<int, string>>' for /home/runner/work/core/core/src/corebundle/translator/metamodeltranslatorconfigurator.php:92:2736:-:closure is incorrect, got 'array<string, array<array-key, string>>' (reported by psalm)
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;

Check failure on line 114 in src/CoreBundle/Translator/MetaModelTranslatorConfigurator.php

View workflow job for this annotation

GitHub Actions / PHP: 8.1 Contao: ~4.13.0

InvalidReturnStatement: The inferred type 'array<string, array<array-key, string>>' does not match the declared return type 'iterable<string, iterable<int, string>>' for /home/runner/work/core/core/src/corebundle/translator/metamodeltranslatorconfigurator.php:92:2736:-:closure (reported by psalm)

Check failure on line 114 in src/CoreBundle/Translator/MetaModelTranslatorConfigurator.php

View workflow job for this annotation

GitHub Actions / PHP: 8.2 Contao: ~4.13.0

InvalidReturnStatement: The inferred type 'array<string, array<array-key, string>>' does not match the declared return type 'iterable<string, iterable<int, string>>' for /home/runner/work/core/core/src/corebundle/translator/metamodeltranslatorconfigurator.php:92:2736:-:closure (reported by psalm)

Check failure on line 114 in src/CoreBundle/Translator/MetaModelTranslatorConfigurator.php

View workflow job for this annotation

GitHub Actions / PHP: 8.3 Contao: ~4.13.0

InvalidReturnStatement: The inferred type 'array<string, array<array-key, string>>' does not match the declared return type 'iterable<string, iterable<int, string>>' for /home/runner/work/core/core/src/corebundle/translator/metamodeltranslatorconfigurator.php:92:2736:-:closure (reported by psalm)
Expand Down

0 comments on commit aac9496

Please sign in to comment.