Skip to content

Commit

Permalink
Configure included and excluded domains using monolog style
Browse files Browse the repository at this point in the history
  • Loading branch information
maelanleborgne committed Jul 9, 2024
1 parent ce43d25 commit f36d05d
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 37 deletions.
10 changes: 6 additions & 4 deletions src/Translator/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,13 @@ including or excluding translation domains in your ``config/packages/ux_translat
.. code-block:: yaml
ux_translator:
# By listing the domains you want to include:
included_domains: ['messages', 'admin']
domains: ~ # Include all the domains
# Or by excluding the domains you don't want:
excluded_domains: ['validators', 'security']
domains: foo # Include only domain 'foo'
domains: '!foo' # Include all domains, except 'foo'
domains: [foo, bar] # Include only domains 'foo' and 'bar'
domains: ['!foo', '!bar'] # Include all domains, except 'foo' and 'bar'
Configuring the default locale
Expand Down
66 changes: 53 additions & 13 deletions src/Translator/src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;

/**
* @author Hugo Alliaume <hugo@alliau.me>
Expand All @@ -28,21 +29,60 @@ public function getConfigTreeBuilder(): TreeBuilder
$rootNode
->children()
->scalarNode('dump_directory')->defaultValue('%kernel.project_dir%/var/translations')->end()
->arrayNode('excluded_domains')
->info('List of domains to exclude from the generated translations.')
->scalarPrototype()->end()
->defaultValue([])
->end()
->arrayNode('included_domains')
->info('List of domains to include in the generated translations. By default, all domains are included.')
->scalarPrototype()->end()
->defaultValue([])
->arrayNode('domains')
->info('List of domains to include/exclude from the generated translations. Prefix with a `!` to exclude a domain.')
->children()
->scalarNode('type')
->validate()
->ifNotInArray(['inclusive', 'exclusive'])
->thenInvalid('The type of domains has to be inclusive or exclusive')
->end()
->end()
->arrayNode('elements')
->scalarPrototype()->end()
->end()
->end()
->canBeUnset()
->beforeNormalization()
->ifString()
->then(fn ($v) => ['elements' => [$v]])
->end()
->beforeNormalization()
->ifTrue(function ($v) { return \is_array($v) && is_numeric(key($v)); })
->then(function ($v) { return ['elements' => $v]; })
->end()
->validate()
->always(function ($v) {
$isExclusive = null;
$elements = [];
if (isset($v['type'])) {
$isExclusive = 'exclusive' === $v['type'];
}
foreach ($v['elements'] as $domain) {
if (str_starts_with($domain, '!')) {
if (false === $isExclusive) {
throw new InvalidConfigurationException('You cannot mix included and excluded domains.');
}
$isExclusive = true;
$elements[] = substr($domain, 1);
} else {
if (true === $isExclusive) {
throw new InvalidConfigurationException('You cannot mix included and excluded domains.');
}
$isExclusive = false;
$elements[] = $domain;
}
}

if (!\count($elements)) {
return null;
}

return ['type' => $isExclusive ? 'exclusive' : 'inclusive', 'elements' => array_unique($elements)];
})
->end()
->end()
->end()
->validate()
->ifTrue(fn ($v) => $v['excluded_domains'] && $v['included_domains'])
->thenInvalid('You cannot set both "excluded_domains" and "included_domains" at the same time.')
->end()
;

return $treeBuilder;
Expand Down
10 changes: 5 additions & 5 deletions src/Translator/src/DependencyInjection/UxTranslatorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ public function load(array $configs, ContainerBuilder $container)
$dumperDefinition = $container->getDefinition('ux.translator.translations_dumper');
$dumperDefinition->setArgument(0, $config['dump_directory']);

if ($config['excluded_domains']) {
$dumperDefinition->addMethodCall('setExcludedDomains', [$config['excluded_domains']]);
}
if ($config['included_domains']) {
$dumperDefinition->addMethodCall('setIncludedDomains', [$config['included_domains']]);
if (isset($config['domains'])) {
$method = 'inclusive' === $config['domains']['type'] ? 'addIncludedDomain' : 'addExcludedDomain';
foreach ($config['domains']['elements'] as $domainName) {
$dumperDefinition->addMethodCall($method, [$domainName]);
}
}
}

Expand Down
15 changes: 6 additions & 9 deletions src/Translator/src/TranslationsDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,23 +87,20 @@ public function dump(MessageCatalogueInterface ...$catalogues): void
);
}

/**
* @param list<string> $domains
*/
public function setExcludedDomains(array $domains): void
public function addExcludedDomain(string $domain): void
{
if ($this->includedDomains && $domains) {
if ($this->includedDomains) {
throw new \LogicException('You cannot set both "excluded_domains" and "included_domains" at the same time.');
}
$this->excludedDomains = $domains;
$this->excludedDomains[] = $domain;
}

public function setIncludedDomains(array $domains): void
public function addIncludedDomain(string $domain): void
{
if ($this->excludedDomains && $domains) {
if ($this->excludedDomains) {
throw new \LogicException('You cannot set both "excluded_domains" and "included_domains" at the same time.');
}
$this->includedDomains = $domains;
$this->includedDomains[] = $domain;
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/Translator/tests/TranslationsDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function testDump(): void

public function testDumpWithExcludedDomains(): void
{
$this->translationsDumper->setExcludedDomains(['foobar']);
$this->translationsDumper->addExcludedDomain('foobar');
$this->translationsDumper->dump(...$this->getMessageCatalogues());

$this->assertFileExists(self::$translationsDumpDir.'/index.js');
Expand All @@ -114,7 +114,7 @@ public function testDumpWithExcludedDomains(): void

public function testDumpIncludedDomains(): void
{
$this->translationsDumper->setIncludedDomains(['messages']);
$this->translationsDumper->addIncludedDomain('messages');
$this->translationsDumper->dump(...$this->getMessageCatalogues());

$this->assertFileExists(self::$translationsDumpDir.'/index.js');
Expand All @@ -125,16 +125,16 @@ public function testSetBothIncludedAndExcludedDomains(): void
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('You cannot set both "excluded_domains" and "included_domains" at the same time.');
$this->translationsDumper->setIncludedDomains(['foobar']);
$this->translationsDumper->setExcludedDomains(['foobar']);
$this->translationsDumper->addIncludedDomain('foobar');
$this->translationsDumper->addExcludedDomain('messages');
}

public function testSetBothExcludedAndIncludedDomains(): void
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('You cannot set both "excluded_domains" and "included_domains" at the same time.');
$this->translationsDumper->setExcludedDomains(['foobar']);
$this->translationsDumper->setIncludedDomains(['foobar']);
$this->translationsDumper->addExcludedDomain('foobar');
$this->translationsDumper->addIncludedDomain('messages');
}

/**
Expand Down

0 comments on commit f36d05d

Please sign in to comment.