-
Notifications
You must be signed in to change notification settings - Fork 93
Introduce a translation:check-missing command #403
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2b096c1
Introduce a translation:check command, to check the current translati…
nicwortel 360b332
Also check for empty translation messages
nicwortel 59bc570
Fix code style issues
nicwortel aed52a4
Search for a shorter string in the test
nicwortel e473d14
Merge branch 'master' of github.com:php-translation/symfony-bundle in…
nicwortel f6609f1
Rename translation:check to translation:check-missing
nicwortel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Translation\Bundle\Command; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Symfony\Component\Finder\Finder; | ||
use Symfony\Component\Translation\MessageCatalogueInterface; | ||
use Translation\Bundle\Catalogue\CatalogueCounter; | ||
use Translation\Bundle\Catalogue\CatalogueFetcher; | ||
use Translation\Bundle\Model\Configuration; | ||
use Translation\Bundle\Service\ConfigurationManager; | ||
use Translation\Bundle\Service\Importer; | ||
|
||
final class CheckMissingCommand extends Command | ||
{ | ||
protected static $defaultName = 'translation:check-missing'; | ||
|
||
/** | ||
* @var ConfigurationManager | ||
*/ | ||
private $configurationManager; | ||
|
||
/** | ||
* @var CatalogueFetcher | ||
*/ | ||
private $catalogueFetcher; | ||
|
||
/** | ||
* @var Importer | ||
*/ | ||
private $importer; | ||
|
||
/** | ||
* @var CatalogueCounter | ||
*/ | ||
private $catalogueCounter; | ||
|
||
public function __construct( | ||
ConfigurationManager $configurationManager, | ||
CatalogueFetcher $catalogueFetcher, | ||
Importer $importer, | ||
CatalogueCounter $catalogueCounter | ||
) { | ||
parent::__construct(); | ||
|
||
$this->configurationManager = $configurationManager; | ||
$this->catalogueFetcher = $catalogueFetcher; | ||
$this->importer = $importer; | ||
$this->catalogueCounter = $catalogueCounter; | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this | ||
->setName(self::$defaultName) | ||
->setDescription('Check that all translations for a given locale are extracted.') | ||
->addArgument('locale', InputArgument::REQUIRED, 'The locale to check') | ||
->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$config = $this->configurationManager->getConfiguration($input->getArgument('configuration')); | ||
|
||
$locale = $input->getArgument('locale'); | ||
|
||
$catalogues = $this->catalogueFetcher->getCatalogues($config, [$locale]); | ||
$finder = $this->getConfiguredFinder($config); | ||
|
||
$result = $this->importer->extractToCatalogues( | ||
$finder, | ||
$catalogues, | ||
[ | ||
'blacklist_domains' => $config->getBlacklistDomains(), | ||
'whitelist_domains' => $config->getWhitelistDomains(), | ||
'project_root' => $config->getProjectRoot(), | ||
] | ||
); | ||
|
||
$definedBefore = $this->catalogueCounter->getNumberOfDefinedMessages($catalogues[0]); | ||
$definedAfter = $this->catalogueCounter->getNumberOfDefinedMessages($result->getMessageCatalogues()[0]); | ||
|
||
$newMessages = $definedAfter - $definedBefore; | ||
|
||
$io = new SymfonyStyle($input, $output); | ||
|
||
if ($newMessages > 0) { | ||
$io->error(\sprintf('%d new message(s) have been found, run bin/console translation:extract', $newMessages)); | ||
|
||
return 1; | ||
} | ||
|
||
$emptyTranslations = $this->countEmptyTranslations($result->getMessageCatalogues()[0]); | ||
|
||
if ($emptyTranslations > 0) { | ||
$io->error( | ||
\sprintf('%d messages have empty translations, please provide translations for them', $emptyTranslations) | ||
); | ||
|
||
return 1; | ||
} | ||
|
||
$io->success('No new translation messages'); | ||
|
||
return 0; | ||
} | ||
|
||
private function getConfiguredFinder(Configuration $config): Finder | ||
{ | ||
$finder = new Finder(); | ||
$finder->in($config->getDirs()); | ||
|
||
foreach ($config->getExcludedDirs() as $exclude) { | ||
$finder->notPath($exclude); | ||
} | ||
|
||
foreach ($config->getExcludedNames() as $exclude) { | ||
$finder->notName($exclude); | ||
} | ||
|
||
return $finder; | ||
} | ||
|
||
private function countEmptyTranslations(MessageCatalogueInterface $catalogue): int | ||
{ | ||
$total = 0; | ||
|
||
foreach ($catalogue->getDomains() as $domain) { | ||
$emptyTranslations = \array_filter( | ||
$catalogue->all($domain), | ||
function (string $message): bool { | ||
return '' === $message; | ||
} | ||
); | ||
|
||
$total += \count($emptyTranslations); | ||
} | ||
|
||
return $total; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Translation\Bundle\Tests\Functional\Command; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Console\Application; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
use Translation\Bundle\Tests\Functional\BaseTestCase; | ||
|
||
class CheckMissingCommandTest extends BaseTestCase | ||
{ | ||
/** | ||
* @var Application | ||
*/ | ||
private $application; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->kernel->addConfigFile(__DIR__.'/../app/config/normal_config.yaml'); | ||
$this->bootKernel(); | ||
$this->application = new Application($this->kernel); | ||
|
||
\file_put_contents( | ||
__DIR__.'/../app/Resources/translations/messages.sv.xlf', | ||
<<<'XML' | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0" srcLang="en" trgLang="sv"> | ||
<file id="messages.sv"> | ||
<unit id="xx1"> | ||
<segment> | ||
<source>translated.heading</source> | ||
<target>My translated heading</target> | ||
</segment> | ||
</unit> | ||
<unit id="xx2"> | ||
<segment> | ||
<source>translated.paragraph0</source> | ||
<target>My translated paragraph0</target> | ||
</segment> | ||
</unit> | ||
<unit id="xx3"> | ||
<notes> | ||
<note category="file-source" priority="1">foobar.html.twig:9</note> | ||
</notes> | ||
<segment> | ||
<source>translated.paragraph1</source> | ||
<target>My translated paragraph1</target> | ||
</segment> | ||
</unit> | ||
<unit id="xx4"> | ||
<segment> | ||
<source>not.in.source</source> | ||
<target>This is not in the source code</target> | ||
</segment> | ||
</unit> | ||
</file> | ||
</xliff> | ||
XML | ||
); | ||
} | ||
|
||
public function testReportsMissingTranslations(): void | ||
{ | ||
$commandTester = new CommandTester($this->application->find('translation:check-missing')); | ||
|
||
$commandTester->execute(['locale' => 'sv', 'configuration' => 'app']); | ||
|
||
$this->assertStringContainsString( | ||
'4 new message(s) have been found, run bin/console translation:extract', | ||
$commandTester->getDisplay() | ||
); | ||
$this->assertGreaterThan(0, $commandTester->getStatusCode()); | ||
} | ||
|
||
public function testReportsEmptyTranslationMessages(): void | ||
{ | ||
// run translation:extract first, so all translations are extracted | ||
(new CommandTester($this->application->find('translation:extract')))->execute(['locale' => 'sv']); | ||
|
||
$commandTester = new CommandTester($this->application->find('translation:check-missing')); | ||
|
||
$commandTester->execute(['locale' => 'sv', 'configuration' => 'app']); | ||
|
||
$this->assertStringContainsString( | ||
'4 messages have empty translations, please provide translations', | ||
$commandTester->getDisplay() | ||
); | ||
$this->assertGreaterThan(0, $commandTester->getStatusCode()); | ||
} | ||
|
||
public function testReportsNoNewTranslationMessages(): void | ||
{ | ||
\file_put_contents( | ||
__DIR__.'/../app/Resources/translations/messages.sv.xlf', | ||
<<<'XML' | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0" srcLang="en" trgLang="sv"> | ||
<file id="messages.sv"> | ||
<unit id="gwCXP88" name="translated.title"> | ||
<notes> | ||
<note category="file-source" priority="1">Resources/views/translated.html.twig:5</note> | ||
<note category="state" priority="1">new</note> | ||
</notes> | ||
<segment> | ||
<source>translated.title</source> | ||
<target>My translated title</target> | ||
</segment> | ||
</unit> | ||
<unit id="MVOZYWq" name="translated.heading"> | ||
<notes> | ||
<note category="file-source" priority="1">Resources/views/translated.html.twig:8</note> | ||
</notes> | ||
<segment> | ||
<source>translated.heading</source> | ||
<target>My translated heading</target> | ||
</segment> | ||
</unit> | ||
<unit id="bJFCP77" name="translated.paragraph0"> | ||
<notes> | ||
<note category="file-source" priority="1">Resources/views/translated.html.twig:9</note> | ||
</notes> | ||
<segment> | ||
<source>translated.paragraph0</source> | ||
<target>My translated paragraph0</target> | ||
</segment> | ||
</unit> | ||
<unit id="1QAmWwr" name="translated.paragraph1"> | ||
<notes> | ||
<note category="file-source" priority="1">Resources/views/translated.html.twig:9</note> | ||
</notes> | ||
<segment> | ||
<source>translated.paragraph1</source> | ||
<target>My translated paragraph1</target> | ||
</segment> | ||
</unit> | ||
<unit id="7AdXS54" name="translated.paragraph2"> | ||
<notes> | ||
<note category="file-source" priority="1">Resources/views/translated.html.twig:11</note> | ||
<note category="state" priority="1">new</note> | ||
</notes> | ||
<segment> | ||
<source>translated.paragraph2</source> | ||
<target>My translated paragraph2</target> | ||
</segment> | ||
</unit> | ||
<unit id="WvnvT8X" name="localized.email"> | ||
<notes> | ||
<note category="file-source" priority="1">Resources/views/translated.html.twig:12</note> | ||
<note category="file-source" priority="1">Resources/views/translated.html.twig:12</note> | ||
<note category="state" priority="1">new</note> | ||
</notes> | ||
<segment> | ||
<source>localized.email</source> | ||
<target>My localized email</target> | ||
</segment> | ||
</unit> | ||
<unit id="ETjQiEP" name="translated.attribute"> | ||
<notes> | ||
<note category="file-source" priority="1">Resources/views/translated.html.twig:14</note> | ||
<note category="state" priority="1">new</note> | ||
</notes> | ||
<segment> | ||
<source>translated.attribute</source> | ||
<target>My translated attribute</target> | ||
</segment> | ||
</unit> | ||
<unit id="GO15Lkx" name="not.in.source"> | ||
<notes> | ||
<note category="state" priority="1">obsolete</note> | ||
</notes> | ||
<segment> | ||
<source>not.in.source</source> | ||
<target>This is not in the source code</target> | ||
</segment> | ||
</unit> | ||
</file> | ||
</xliff> | ||
XML | ||
); | ||
|
||
$commandTester = new CommandTester($this->application->find('translation:check-missing')); | ||
|
||
$commandTester->execute(['locale' => 'sv', 'configuration' => 'app']); | ||
|
||
$this->assertStringContainsString( | ||
'No new translation messages', | ||
$commandTester->getDisplay() | ||
); | ||
$this->assertSame(0, $commandTester->getStatusCode()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's not needed when defining the static property.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe you are right, the Symfony documentation example also doesn't call
setName()
anymore.However, I copied this from the other commands in this bundle which also call
->setName(self::$defaultName)
. So perhaps this should be fixed for all commands at once in a separate PR.