12
12
namespace Symfony \Bundle \FrameworkBundle \Command ;
13
13
14
14
use Symfony \Component \Translation \Catalogue \MergeOperation ;
15
+ use Symfony \Component \Console \Helper \Table ;
15
16
use Symfony \Component \Console \Input \InputInterface ;
16
17
use Symfony \Component \Console \Output \OutputInterface ;
17
18
use Symfony \Component \Console \Input \InputArgument ;
18
19
use Symfony \Component \Console \Input \InputOption ;
19
20
use Symfony \Component \Translation \MessageCatalogue ;
21
+ use Symfony \Component \Translation \Translator ;
20
22
21
23
/**
22
24
* Helps finding unused or missing translation messages in a given locale
@@ -46,10 +48,11 @@ protected function configure()
46
48
))
47
49
->setDescription ('Displays translation messages informations ' )
48
50
->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.
51
54
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:
53
56
54
57
<info>php %command.full_name% en AcmeDemoBundle</info>
55
58
@@ -81,12 +84,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
81
84
82
85
// Extract used messages
83
86
$ 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 );
86
88
87
89
// Load defined messages
88
90
$ 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
+ }
90
94
91
95
// Merge defined and extracted messages to get all message ids
92
96
$ mergeOperation = new MergeOperation ($ extractedCatalogue , $ currentCatalogue );
@@ -110,32 +114,28 @@ protected function execute(InputInterface $input, OutputInterface $output)
110
114
111
115
// Load the fallback catalogues
112
116
$ 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
+ }
117
123
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
+ }
121
128
}
122
129
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 );
132
132
133
133
// Display header line
134
134
$ headers = array ('State(s) ' , 'Id ' , sprintf ('Message Preview (%s) ' , $ locale ));
135
135
foreach ($ fallbackCatalogues as $ fallbackCatalogue ) {
136
136
$ headers [] = sprintf ('Fallback Message Preview (%s) ' , $ fallbackCatalogue ->getLocale ());
137
137
}
138
- $ tableHelper ->setHeaders ($ headers );
138
+ $ table ->setHeaders ($ headers );
139
139
140
140
// Iterate all message ids and determine their state
141
141
foreach ($ allMessages as $ domain => $ messages ) {
@@ -157,9 +157,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
157
157
}
158
158
159
159
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 )) {
162
161
$ states [] = self ::MESSAGE_EQUALS_FALLBACK ;
162
+
163
163
break ;
164
164
}
165
165
}
@@ -169,25 +169,31 @@ protected function execute(InputInterface $input, OutputInterface $output)
169
169
$ row [] = $ this ->sanitizeString ($ fallbackCatalogue ->get ($ messageId , $ domain ));
170
170
}
171
171
172
- $ tableHelper ->addRow ($ row );
172
+ $ table ->addRow ($ row );
173
173
}
174
174
}
175
175
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 )));
177
183
}
178
184
179
185
private function formatState ($ state )
180
186
{
181
187
if (self ::MESSAGE_MISSING === $ state ) {
182
- return '<fg=red;options=bold >x</fg=red;options=bold > ' ;
188
+ return '<fg=red>x</> ' ;
183
189
}
184
190
185
191
if (self ::MESSAGE_UNUSED === $ state ) {
186
- return '<fg=yellow;options=bold >o</fg=yellow;options=bold > ' ;
192
+ return '<fg=yellow>o</> ' ;
187
193
}
188
194
189
195
if (self ::MESSAGE_EQUALS_FALLBACK === $ state ) {
190
- return '<fg=green;options=bold >=</fg=green;options=bold > ' ;
196
+ return '<fg=green>=</> ' ;
191
197
}
192
198
193
199
return $ state ;
@@ -208,16 +214,16 @@ private function formatId($id)
208
214
return sprintf ('<fg=cyan;options=bold>%s</fg=cyan;options=bold> ' , $ id );
209
215
}
210
216
211
- private function sanitizeString ($ string , $ lenght = 40 )
217
+ private function sanitizeString ($ string , $ length = 40 )
212
218
{
213
219
$ string = trim (preg_replace ('/\s+/ ' , ' ' , $ string ));
214
220
215
221
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 ).'... ' ;
218
224
}
219
- } elseif (strlen ($ string ) > $ lenght ) {
220
- return substr ($ string , 0 , $ lenght - 3 ).'... ' ;
225
+ } elseif (strlen ($ string ) > $ length ) {
226
+ return substr ($ string , 0 , $ length - 3 ).'... ' ;
221
227
}
222
228
223
229
return $ string ;
0 commit comments