Skip to content

Commit d66804a

Browse files
Allow only one of included_domains or excluded_domains
1 parent 2c120a0 commit d66804a

File tree

4 files changed

+27
-11
lines changed

4 files changed

+27
-11
lines changed

src/Translator/doc/index.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ Don't worry about your final bundle size, only the translations you use will be
7474
Configuring the dumped translations
7575
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7676

77-
By default, all your translations will be dumped as JavaScript. You can restrict the dumped translations by including or
78-
excluding translation domains in your ``config/packages/ux_translator.yaml`` file:
77+
By default, all your translations will be dumped as JavaScript. You can restrict the dumped translations by either
78+
including or excluding translation domains in your ``config/packages/ux_translator.yaml`` file:
7979

8080
.. code-block:: yaml
8181

src/Translator/src/DependencyInjection/Configuration.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,20 @@ public function getConfigTreeBuilder(): TreeBuilder
2929
->children()
3030
->scalarNode('dump_directory')->defaultValue('%kernel.project_dir%/var/translations')->end()
3131
->arrayNode('excluded_domains')
32+
->info('List of domains to exclude from the generated translations')
3233
->scalarPrototype()->end()
3334
->defaultValue([])
3435
->end()
3536
->arrayNode('included_domains')
37+
->info('List of domains to include in the generated translations')
3638
->scalarPrototype()->end()
3739
->defaultValue([])
3840
->end()
3941
->end()
42+
->validate()
43+
->ifTrue(fn ($v) => $v['excluded_domains'] && $v['included_domains'])
44+
->thenInvalid('You cannot set both "excluded_domains" and "included_domains" at the same time.')
45+
->end()
4046
;
4147

4248
return $treeBuilder;

src/Translator/src/TranslationsDumper.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,21 @@ public function dump(MessageCatalogueInterface ...$catalogues): void
8888
}
8989

9090
/**
91-
* @param list<string> $domains
92-
*/
91+
* @param list<string> $domains
92+
*/
9393
public function setExcludedDomains(array $domains): void
9494
{
95+
if ($this->includedDomains && $domains) {
96+
throw new \LogicException('You cannot set both "excluded_domains" and "included_domains" at the same time.');
97+
}
9598
$this->excludedDomains = $domains;
9699
}
97100

98101
public function setIncludedDomains(array $domains): void
99102
{
103+
if ($this->excludedDomains && $domains) {
104+
throw new \LogicException('You cannot set both "excluded_domains" and "included_domains" at the same time.');
105+
}
100106
$this->includedDomains = $domains;
101107
}
102108

src/Translator/tests/TranslationsDumperTest.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function testDumpWithExcludedDomains(): void
112112
$this->assertStringNotContainsString('foobar', file_get_contents(self::$translationsDumpDir.'/index.js'));
113113
}
114114

115-
public function testDumpSpecificDomains(): void
115+
public function testDumpIncludedDomains(): void
116116
{
117117
$this->translationsDumper->setIncludedDomains(['messages']);
118118
$this->translationsDumper->dump(...$this->getMessageCatalogues());
@@ -121,16 +121,20 @@ public function testDumpSpecificDomains(): void
121121
$this->assertStringNotContainsString('foobar', file_get_contents(self::$translationsDumpDir.'/index.js'));
122122
}
123123

124-
public function testDumpWithSpecificAndExcludedDomains(): void
124+
public function testSetBothIncludedAndExcludedDomains(): void
125125
{
126+
$this->expectException(\LogicException::class);
127+
$this->expectExceptionMessage('You cannot set both "excluded_domains" and "included_domains" at the same time.');
126128
$this->translationsDumper->setIncludedDomains(['foobar']);
127129
$this->translationsDumper->setExcludedDomains(['foobar']);
128-
$this->translationsDumper->dump(...$this->getMessageCatalogues());
130+
}
129131

130-
$this->assertFileExists(self::$translationsDumpDir.'/index.js');
131-
$fileContent = file_get_contents(self::$translationsDumpDir.'/index.js');
132-
$this->assertStringNotContainsString('foobar', $fileContent);
133-
$this->assertStringNotContainsString('messages', $fileContent);
132+
public function testSetBothExcludedAndIncludedDomains(): void
133+
{
134+
$this->expectException(\LogicException::class);
135+
$this->expectExceptionMessage('You cannot set both "excluded_domains" and "included_domains" at the same time.');
136+
$this->translationsDumper->setExcludedDomains(['foobar']);
137+
$this->translationsDumper->setIncludedDomains(['foobar']);
134138
}
135139

136140
/**

0 commit comments

Comments
 (0)