Skip to content

Commit 360b332

Browse files
committed
Also check for empty translation messages
1 parent 2b096c1 commit 360b332

File tree

2 files changed

+139
-5
lines changed

2 files changed

+139
-5
lines changed

Command/CheckCommand.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99
use Symfony\Component\Console\Output\OutputInterface;
1010
use Symfony\Component\Console\Style\SymfonyStyle;
1111
use Symfony\Component\Finder\Finder;
12+
use Symfony\Component\Translation\MessageCatalogueInterface;
1213
use Translation\Bundle\Catalogue\CatalogueCounter;
1314
use Translation\Bundle\Catalogue\CatalogueFetcher;
1415
use Translation\Bundle\Model\Configuration;
1516
use Translation\Bundle\Service\ConfigurationManager;
1617
use Translation\Bundle\Service\Importer;
18+
use function array_filter;
19+
use function count;
1720

1821
final class CheckCommand extends Command
1922
{
@@ -59,8 +62,7 @@ protected function configure(): void
5962
->setName(self::$defaultName)
6063
->setDescription('Check that all translations for a given locale are extracted.')
6164
->addArgument('locale', InputArgument::REQUIRED, 'The locale to check')
62-
->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default')
63-
;
65+
->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default');
6466
}
6567

6668
protected function execute(InputInterface $input, OutputInterface $output): int
@@ -95,6 +97,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9597
return 1;
9698
}
9799

100+
$emptyTranslations = $this->countEmptyTranslations($result->getMessageCatalogues()[0]);
101+
102+
if ($emptyTranslations > 0) {
103+
$io->error(
104+
sprintf('%d messages have empty translations, please provide translations for them', $emptyTranslations)
105+
);
106+
107+
return 1;
108+
}
109+
98110
$io->success('No new translation messages');
99111

100112
return 0;
@@ -115,4 +127,22 @@ private function getConfiguredFinder(Configuration $config): Finder
115127

116128
return $finder;
117129
}
130+
131+
private function countEmptyTranslations(MessageCatalogueInterface $catalogue): int
132+
{
133+
$total = 0;
134+
135+
foreach ($catalogue->getDomains() as $domain) {
136+
$emptyTranslations = array_filter(
137+
$catalogue->all($domain),
138+
function (string $message): bool {
139+
return $message === '';
140+
}
141+
);
142+
143+
$total += count($emptyTranslations);
144+
}
145+
146+
return $total;
147+
}
118148
}

Tests/Functional/Command/CheckCommandTest.php

Lines changed: 107 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Symfony\Bundle\FrameworkBundle\Console\Application;
77
use Symfony\Component\Console\Tester\CommandTester;
88
use Translation\Bundle\Tests\Functional\BaseTestCase;
9+
use function file_put_contents;
910

1011
class CheckCommandTest extends BaseTestCase
1112
{
@@ -18,11 +19,13 @@ protected function setUp(): void
1819
{
1920
parent::setUp();
2021

21-
$this->kernel->addConfigFile(__DIR__.'/../app/config/normal_config.yaml');
22+
$this->kernel->addConfigFile(__DIR__ . '/../app/config/normal_config.yaml');
2223
$this->bootKernel();
2324
$this->application = new Application($this->kernel);
2425

25-
\file_put_contents(__DIR__.'/../app/Resources/translations/messages.sv.xlf', <<<'XML'
26+
file_put_contents(
27+
__DIR__ . '/../app/Resources/translations/messages.sv.xlf',
28+
<<<'XML'
2629
<?xml version="1.0" encoding="utf-8"?>
2730
<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0" srcLang="en" trgLang="sv">
2831
<file id="messages.sv">
@@ -72,7 +75,7 @@ public function testReportsMissingTranslations(): void
7275
$this->assertGreaterThan(0, $commandTester->getStatusCode());
7376
}
7477

75-
public function testReportsNoNewTranslationMessages(): void
78+
public function testReportsEmptyTranslationMessages(): void
7679
{
7780
// run translation:extract first, so all translations are extracted
7881
(new CommandTester($this->application->find('translation:extract')))->execute(['locale' => 'sv']);
@@ -81,6 +84,107 @@ public function testReportsNoNewTranslationMessages(): void
8184

8285
$commandTester->execute(['locale' => 'sv', 'configuration' => 'app']);
8386

87+
$this->assertStringContainsString(
88+
'4 messages have empty translations, please provide translations for them',
89+
$commandTester->getDisplay()
90+
);
91+
$this->assertGreaterThan(0, $commandTester->getStatusCode());
92+
}
93+
94+
public function testReportsNoNewTranslationMessages(): void
95+
{
96+
file_put_contents(
97+
__DIR__ . '/../app/Resources/translations/messages.sv.xlf',
98+
<<<'XML'
99+
<?xml version="1.0" encoding="utf-8"?>
100+
<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0" srcLang="en" trgLang="sv">
101+
<file id="messages.sv">
102+
<unit id="gwCXP88" name="translated.title">
103+
<notes>
104+
<note category="file-source" priority="1">Resources/views/translated.html.twig:5</note>
105+
<note category="state" priority="1">new</note>
106+
</notes>
107+
<segment>
108+
<source>translated.title</source>
109+
<target>My translated title</target>
110+
</segment>
111+
</unit>
112+
<unit id="MVOZYWq" name="translated.heading">
113+
<notes>
114+
<note category="file-source" priority="1">Resources/views/translated.html.twig:8</note>
115+
</notes>
116+
<segment>
117+
<source>translated.heading</source>
118+
<target>My translated heading</target>
119+
</segment>
120+
</unit>
121+
<unit id="bJFCP77" name="translated.paragraph0">
122+
<notes>
123+
<note category="file-source" priority="1">Resources/views/translated.html.twig:9</note>
124+
</notes>
125+
<segment>
126+
<source>translated.paragraph0</source>
127+
<target>My translated paragraph0</target>
128+
</segment>
129+
</unit>
130+
<unit id="1QAmWwr" name="translated.paragraph1">
131+
<notes>
132+
<note category="file-source" priority="1">Resources/views/translated.html.twig:9</note>
133+
</notes>
134+
<segment>
135+
<source>translated.paragraph1</source>
136+
<target>My translated paragraph1</target>
137+
</segment>
138+
</unit>
139+
<unit id="7AdXS54" name="translated.paragraph2">
140+
<notes>
141+
<note category="file-source" priority="1">Resources/views/translated.html.twig:11</note>
142+
<note category="state" priority="1">new</note>
143+
</notes>
144+
<segment>
145+
<source>translated.paragraph2</source>
146+
<target>My translated paragraph2</target>
147+
</segment>
148+
</unit>
149+
<unit id="WvnvT8X" name="localized.email">
150+
<notes>
151+
<note category="file-source" priority="1">Resources/views/translated.html.twig:12</note>
152+
<note category="file-source" priority="1">Resources/views/translated.html.twig:12</note>
153+
<note category="state" priority="1">new</note>
154+
</notes>
155+
<segment>
156+
<source>localized.email</source>
157+
<target>My localized email</target>
158+
</segment>
159+
</unit>
160+
<unit id="ETjQiEP" name="translated.attribute">
161+
<notes>
162+
<note category="file-source" priority="1">Resources/views/translated.html.twig:14</note>
163+
<note category="state" priority="1">new</note>
164+
</notes>
165+
<segment>
166+
<source>translated.attribute</source>
167+
<target>My translated attribute</target>
168+
</segment>
169+
</unit>
170+
<unit id="GO15Lkx" name="not.in.source">
171+
<notes>
172+
<note category="state" priority="1">obsolete</note>
173+
</notes>
174+
<segment>
175+
<source>not.in.source</source>
176+
<target>This is not in the source code</target>
177+
</segment>
178+
</unit>
179+
</file>
180+
</xliff>
181+
XML
182+
);
183+
184+
$commandTester = new CommandTester($this->application->find('translation:check'));
185+
186+
$commandTester->execute(['locale' => 'sv', 'configuration' => 'app']);
187+
84188
$this->assertStringContainsString(
85189
'No new translation messages',
86190
$commandTester->getDisplay()

0 commit comments

Comments
 (0)