| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Translation\Command; |
| 13 |
|
| 14 |
use Symfony\Component\Console\Attribute\AsCommand; |
| 15 |
use Symfony\Component\Console\Command\Command; |
| 16 |
use Symfony\Component\Console\Completion\CompletionInput; |
| 17 |
use Symfony\Component\Console\Completion\CompletionSuggestions; |
| 18 |
use Symfony\Component\Console\Input\InputInterface; |
| 19 |
use Symfony\Component\Console\Input\InputOption; |
| 20 |
use Symfony\Component\Console\Output\OutputInterface; |
| 21 |
use Symfony\Component\Console\Style\SymfonyStyle; |
| 22 |
use Symfony\Component\Translation\Exception\ExceptionInterface; |
| 23 |
use Symfony\Component\Translation\TranslatorBagInterface; |
| 24 |
use Symfony\Contracts\Translation\TranslatorInterface; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#[AsCommand(name: 'lint:translations', description: 'Lint translations files syntax and outputs encountered errors')] |
| 32 |
class TranslationLintCommand extends Command |
| 33 |
{ |
| 34 |
private SymfonyStyle $io; |
| 35 |
|
| 36 |
public function __construct( |
| 37 |
private TranslatorInterface&TranslatorBagInterface $translator, |
| 38 |
private array $enabledLocales = [], |
| 39 |
) { |
| 40 |
parent::__construct(); |
| 41 |
} |
| 42 |
|
| 43 |
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void |
| 44 |
{ |
| 45 |
if ($input->mustSuggestOptionValuesFor('locale')) { |
| 46 |
$suggestions->suggestValues($this->enabledLocales); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
protected function configure(): void |
| 51 |
{ |
| 52 |
$this |
| 53 |
->setDefinition([ |
| 54 |
new InputOption('locale', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Specify the locales to lint.', $this->enabledLocales), |
| 55 |
]) |
| 56 |
->setHelp(<<<'EOF' |
| 57 |
The <info>%command.name%</> command lint translations. |
| 58 |
|
| 59 |
<info>php %command.full_name%</> |
| 60 |
EOF |
| 61 |
); |
| 62 |
} |
| 63 |
|
| 64 |
protected function initialize(InputInterface $input, OutputInterface $output): void |
| 65 |
{ |
| 66 |
$this->io = new SymfonyStyle($input, $output); |
| 67 |
} |
| 68 |
|
| 69 |
protected function execute(InputInterface $input, OutputInterface $output): int |
| 70 |
{ |
| 71 |
$locales = $input->getOption('locale'); |
| 72 |
|
| 73 |
|
| 74 |
$errors = []; |
| 75 |
$domainsByLocales = []; |
| 76 |
|
| 77 |
foreach ($locales as $locale) { |
| 78 |
$messageCatalogue = $this->translator->getCatalogue($locale); |
| 79 |
|
| 80 |
foreach ($domainsByLocales[$locale] = $messageCatalogue->getDomains() as $domain) { |
| 81 |
foreach ($messageCatalogue->all($domain) as $id => $translation) { |
| 82 |
try { |
| 83 |
$this->translator->trans($id, [], $domain, $messageCatalogue->getLocale()); |
| 84 |
} catch (ExceptionInterface $e) { |
| 85 |
$errors[$locale][$domain][$id] = $e; |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
if (!$domainsByLocales) { |
| 92 |
$this->io->error('No translation files were found.'); |
| 93 |
|
| 94 |
return Command::SUCCESS; |
| 95 |
} |
| 96 |
|
| 97 |
$this->io->table( |
| 98 |
['Locale', 'Domains', 'Valid?'], |
| 99 |
array_map( |
| 100 |
static fn (string $locale, array $domains) => [ |
| 101 |
$locale, |
| 102 |
implode(', ', $domains), |
| 103 |
!\array_key_exists($locale, $errors) ? '<info>Yes</>' : '<error>No</>', |
| 104 |
], |
| 105 |
array_keys($domainsByLocales), |
| 106 |
$domainsByLocales |
| 107 |
), |
| 108 |
); |
| 109 |
|
| 110 |
if ($errors) { |
| 111 |
foreach ($errors as $locale => $domains) { |
| 112 |
foreach ($domains as $domain => $domainsErrors) { |
| 113 |
$this->io->section(\sprintf('Errors for locale "%s" and domain "%s"', $locale, $domain)); |
| 114 |
|
| 115 |
foreach ($domainsErrors as $id => $error) { |
| 116 |
$this->io->text(\sprintf('Translation key "%s" is invalid:', $id)); |
| 117 |
$this->io->error($error->getMessage()); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
return Command::FAILURE; |
| 123 |
} |
| 124 |
|
| 125 |
$this->io->success('All translations are valid.'); |
| 126 |
|
| 127 |
return Command::SUCCESS; |
| 128 |
} |
| 129 |
} |