8000 [FrameworkBundle] fixed edge cases for translation:debug and tweaked … · symfony/symfony@f039bde · GitHub
[go: up one dir, main page]

Skip to content

Commit f039bde

Browse files
committed
[FrameworkBundle] fixed edge cases for translation:debug and tweaked the output
1 parent 597a310 commit f039bde

File tree

1 file changed

+41
-35
lines changed

1 file changed

+41
-35
lines changed

src/Symfony/Bundle/FrameworkBundle/Command/TranslationDebugCommand.php

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Command;
1313

1414
use Symfony\Component\Translation\Catalogue\MergeOperation;
15+
use Symfony\Component\Console\Helper\Table;
1516
use Symfony\Component\Console\Input\InputInterface;
1617
use Symfony\Component\Console\Output\OutputInterface;
1718
use Symfony\Component\Console\Input\InputArgument;
1819
use Symfony\Component\Console\Input\InputOption;
1920
use Symfony\Component\Translation\MessageCatalogue;
21+
use Symfony\Component\Translation\Translator;
2022

2123
/**
2224
* Helps finding unused or missing translation messages in a given locale
@@ -46,10 +48,11 @@ protected function configure()
4648
))
4749
->setDescription('Displays translation messages informations')
4850
->setHelp(<<<EOF
49-
The <info>%command.name%</info> command helps finding unused or missing translation messages and
50-
comparing them with the fallback ones by inspecting the templates and translation files of a given bundle.
51+
The <info>%command.name%</info> command helps finding unused or missing translation
52+
messages and comparing them with the fallback ones by inspecting the
53+
templates and translation files of a given bundle.
5154
52-
You can display informations about a bundle translations in a specific locale:
55+
You can display information about bundle translations in a specific locale:
5356
5457
<info>php %command.full_name% en AcmeDemoBundle</info>
5558
@@ -81,12 +84,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
8184

8285
// Extract used messages
8386
$extractedCatalogue = new MessageCatalogue($locale);
84-
$this->getContainer()->get('translation.extractor')
85-
->extract($bundle->getPath().'/Resources/views/', $extractedCatalogue);
87+
$this->getContainer()->get('translation.extractor')->extract($bundle->getPath().'/Resources/views', $extractedCatalogue);
8688

8789
// Load defined messages
8890
$currentCatalogue = new MessageCatalogue($locale);
89-
$loader->loadMessages($bundle->getPath().'/Resources/translations', $currentCatalogue);
91+
if (is_dir($bundle->getPath().'/Resources/translations')) {
92+
$loader->loadMessages($bundle->getPath().'/Resources/translations', $currentCatalogue);
93+
}
9094

9195
// Merge defined and extracted messages to get all message ids
9296
$mergeOperation = new MergeOperation($extractedCatalogue, $currentCatalogue);
@@ -110,32 +114,28 @@ protected function execute(InputInterface $input, OutputInterface $output)
110114

111115
// Load the fallback catalogues
112116
$fallbackCatalogues = array();
113-
foreach ($this->getContainer()->get('translator')->getFallbackLocales() as $fallbackLocale) {
114-
if ($fallbackLocale === $locale) {
115-
continue;
116-
}
117+
$translator = $this->getContainer()->get('translator');
118+
if ($translator instanceof Translator) {
119+
foreach ($translator->getFallbackLocales() as $fallbackLocale) {
120+
if ($fallbackLocale === $locale) {
121+
continue;
122+
}
117123

118-
$fallbackCatalogue = new MessageCatalogue($fallbackLocale);
119-
$loader->loadMessages($bundle->getPath().'/Resources/translations', $fallbackCatalogue);
120-
$fallbackCatalogues[] = $fallbackCatalogue;
124+
$fallbackCatalogue = new MessageCatalogue($fallbackLocale);
125+
$loader->loadMessages($bundle->getPath().'/Resources/translations', $fallbackCatalogue);
126+
$fallbackCatalogues[] = $fallbackCatalogue;
127+
}
121128
}
122129

123-
// Display legend
124-
$output->writeln(sprintf('Legend: %s Missing message %s Unused message %s Equals fallback message',
125-
$this->formatState(self::MESSAGE_MISSING),
126-
$this->formatState(self::MESSAGE_UNUSED),
127-
$this->formatState(self::MESSAGE_EQUALS_FALLBACK)
128-
));
129-
130-
/** @var \Symfony\Component\Console\Helper\TableHelper $tableHelper */
131-
$tableHelper = $this->getHelperSet()->get('table');
130+
/** @var \Symfony\Component\Console\Helper\Table $table */
131+
$table = new Table($output);
132132

133133
// Display header line
134134
$headers = array('State(s)', 'Id', sprintf('Message Preview (%s)', $locale));
135135
foreach ($fallbackCatalogues as $fallbackCatalogue) {
136136
$headers[] = sprintf('Fallback Message Preview (%s)', $fallbackCatalogue->getLocale());
137137
}
138-
$tableHelper->setHeaders($headers);
138+
$table->setHeaders($headers);
139139

140140
// Iterate all message ids and determine their state
141141
foreach ($allMessages as $domain => $messages) {
@@ -157,9 +157,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
157157
}
158158

159159
foreach ($fallbackCatalogues as $fallbackCatalogue) {
160-
if ($fallbackCatalogue->defines($messageId, $domain)
161-
&& $value === $fallbackCatalogue->get($messageId, $domain)) {
160+
if ($fallbackCatalogue->defines($messageId, $domain) && $value === $fallbackCatalogue->get($messageId, $domain)) {
162161
$states[] = self::MESSAGE_EQUALS_FALLBACK;
162+
163163
break;
164164
}
165165
}
@@ -169,25 +169,31 @@ protected function execute(InputInterface $input, OutputInterface $output)
169169
$row[] = $this->sanitizeString($fallbackCatalogue->get($messageId, $domain));
170170
}
171171

172-
$tableHelper->addRow($row);
172+
$table->addRow($row);
173173
}
174174
}
175175

176-
$tableHelper->render($output);
176+
$table->render();
177+
178+
$output->writeln('');
179+
$output->writeln('<info>Legend:</info>');
180+
$output->writeln(sprintf(' %s Missing message', $this->formatState(self::MESSAGE_MISSING)));
181+
$output->writeln(sprintf(' %s Unused message', $this->formatState(self::MESSAGE_UNUSED)));
182+
$output->writeln(sprintf(' %s Same as the fallback message', $this->formatState(self::MESSAGE_EQUALS_FALLBACK)));
177183
}
178184

179185
private function formatState($state)
180186
{
181187
if (self::MESSAGE_MISSING === $state) {
182-
return '<fg=red;options=bold>x</fg=red;options=bold>';
188+
return '<fg=red>x</>';
183189
}
184190

185191
if (self::MESSAGE_UNUSED === $state) {
186-
return '<fg=yellow;options=bold>o</fg=yellow;options=bold>';
192+
return '<fg=yellow>o</>';
187193
}
188194

189195
if (self::MESSAGE_EQUALS_FALLBACK === $state) {
190-
return '<fg=green;options=bold>=</fg=green;options=bold>';
196+
return '<fg=green>=</>';
191197
}
192198

193199
return $state;
@@ -208,16 +214,16 @@ private function formatId($id)
208214
return sprintf('<fg=cyan;options=bold>%s</fg=cyan;options=bold>', $id);
209215
}
210216

211-
private function sanitizeString($string, $lenght = 40)
217+
private function sanitizeString($string, $length = 40)
212218
{
213219
$string = trim(preg_replace('/\s+/', ' ', $string));
214220

215221
if (function_exists('mb_strlen') && false !== $encoding = mb_detect_encoding($string)) {
216-
if (mb_strlen($string, $encoding) > $lenght) {
217-
return mb_substr($string, 0, $lenght - 3, $encoding).'...';
222+
if (mb_strlen($string, $encoding) > $length) {
223+
return mb_substr($string, 0, $length - 3, $encoding).'...';
218224
}
219-
} elseif (strlen($string) > $lenght) {
220-
return substr($string, 0, $lenght - 3).'...';
225+
} elseif (strlen($string) > $length) {
226+
return substr($string, 0, $length - 3).'...';
221227
}
222228

223229
return $string;

0 commit comments

Comments
 (0)
0