Skip to content

Commit

Permalink
Add LoaderInterface.php
Browse files Browse the repository at this point in the history
  • Loading branch information
zonky2 committed Jul 26, 2024
1 parent d8e3e31 commit 91cc4a0
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,20 @@

namespace MetaModels\CoreBundle\DependencyInjection\CompilerPass;

use MetaModels\CoreBundle\Translator\MetaModelTranslationLoader;
use MetaModels\CoreBundle\Translator\MetaModelTranslatorConfigurator;
use Symfony\Bundle\FrameworkBundle\Translation\Translator;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

final class PrepareTranslatorPass implements CompilerPassInterface
{
use PriorityTaggedServiceTrait;

public const TAG_NAME = 'metamodels.translation-loader';

public function process(ContainerBuilder $container): void
{
$definition = $container->getDefinition('translator.default');
Expand All @@ -43,5 +49,13 @@ public function process(ContainerBuilder $container): void
$previousConfigurator
);
}
$generator = $container->getDefinition(MetaModelTranslationLoader::class);
$generator->setArgument(
'$loaders',
array_merge(
$generator->getArgument('$loaders'),
$this->findAndSortTaggedServices(self::TAG_NAME, $container)
)
);
}
}
1 change: 1 addition & 0 deletions src/CoreBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,5 @@ services:
$factory: '@metamodels.factory'
$viewCombination: '@metamodels.view_combination'
$builder: '@metamodels.view_combination.input_screen_information_builder'
$loaders: []
tags: [ { name: translation.loader, alias: 'metamodels' } ]
41 changes: 41 additions & 0 deletions src/CoreBundle/Translator/LoaderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/**
* This file is part of MetaModels/core.
*
* (c) 2012-2024 The MetaModels team.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* This project is provided in good faith and hope to be usable by anyone.
*
* @package MetaModels/core
* @author Ingolf Steinhardt <[email protected]>
* @copyright 2012-2024 The MetaModels team.
* @license https://github.com/MetaModels/core/blob/master/LICENSE LGPL-3.0-or-later
* @filesource
*/

namespace MetaModels\CoreBundle\Translator;

use MetaModels\IMetaModel;
use Symfony\Component\Translation\Exception\InvalidResourceException;
use Symfony\Component\Translation\Exception\NotFoundResourceException;
use Symfony\Component\Translation\MessageCatalogue;

interface LoaderInterface
{
/**
* Loads a locale.
*
* @param mixed $resource A resource
* @param string $locale A locale
*
* @return MessageCatalogue
*
* @throws NotFoundResourceException when the resource cannot be found
* @throws InvalidResourceException when the resource cannot be loaded
*/
public function load(IMetaModel $metaModel, string $locale): MessageCatalogue;
}
12 changes: 10 additions & 2 deletions src/CoreBundle/Translator/MetaModelTranslationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
use MetaModels\ViewCombination\InputScreenInformationBuilder;
use MetaModels\ViewCombination\ViewCombination;
use Symfony\Component\Translation\Exception\NotFoundResourceException;
use Symfony\Component\Translation\Loader\LoaderInterface;
use Symfony\Component\Translation\Loader\LoaderInterface as SymfonyLoaderInterface;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\TranslatorBagInterface;

use function is_array;
use function sprintf;

final class MetaModelTranslationLoader implements LoaderInterface
final class MetaModelTranslationLoader implements SymfonyLoaderInterface
{
/**
* The constructor.
Expand All @@ -43,12 +43,14 @@ final class MetaModelTranslationLoader implements LoaderInterface
* @param IFactory $factory The factory.
* @param ViewCombination $viewCombination The view combination.
* @param InputScreenInformationBuilder $builder The input screen builder.
* @param list<LoaderInterface> $loaders The loaders.
*/
public function __construct(
private readonly TranslatorBagInterface $baseTranslator,
private readonly IFactory $factory,
private readonly ViewCombination $viewCombination,
private readonly InputScreenInformationBuilder $builder,
private readonly array $loaders
) {
}

Expand Down Expand Up @@ -160,6 +162,12 @@ public function load($resource, string $locale, string $domain = 'messages'): Me
);
}

foreach ($this->loaders as $loader) {
foreach ($loader->load($metaModel, $locale)->all($domain) as $key => $value) {
$catalog->set($key, $value, $domain);
}
}

return $catalog;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Filter/FilterUrlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ private function getFolderUrlFragments(string $alias, string $host, string $loca
// Look for a root page whose domain name matches the host name
$languages = $pages[$host]
// empty domain
?? ($pages['*'] ?: []);
?? ($pages['*'] ?? []);
unset($pages);

$pages = [];
Expand Down

0 comments on commit 91cc4a0

Please sign in to comment.