@@ -106,106 +106,103 @@ protected function execute(InputInterface $input, OutputInterface $output)
106
106
$ kernel = $ this ->getContainer ()->get ('kernel ' );
107
107
108
108
// Define Root Path to App folder
109
- $ rootPaths = array ($ kernel ->getRootDir ());
109
+ $ transPaths = array ($ kernel ->getRootDir (). ' /Resources/ ' );
110
110
111
111
// Override with provided Bundle info
112
112
if (null !== $ input ->getArgument ('bundle ' )) {
113
113
try {
114
- $ rootPaths = array ($ kernel ->getBundle ($ input ->getArgument ('bundle ' ))->getPath ());
114
+ $ bundle = $ kernel ->getBundle ($ input ->getArgument ('bundle ' ));
115
+ $ transPaths = array (
116
+ $ bundle ->getPath ().'/Resources/ ' ,
117
+ sprintf ('%s/Resources/%s/ ' , $ kernel ->getRootDir (), $ bundle ->getName ()),
118
+ );
115
119
} catch (\InvalidArgumentException $ e ) {
116
120
// such a bundle does not exist, so treat the argument as path
117
- $ rootPaths = array ($ input ->getArgument ('bundle ' ));
121
+ $ transPaths = array ($ input ->getArgument ('bundle ' ). ' /Resources/ ' );
118
122
119
- if (!is_dir ($ rootPaths [0 ])) {
120
- throw new \InvalidArgumentException (sprintf ('"%s" is neither an enabled bundle nor a directory.</error> ' , $ rootPaths [0 ]));
123
+ if (!is_dir ($ transPaths [0 ])) {
124
+ throw new \InvalidArgumentException (sprintf ('"%s" is neither an enabled bundle nor a directory. ' , $ transPaths [0 ]));
121
125
}
122
126
}
123
127
} elseif ($ input ->getOption ('all ' )) {
124
128
foreach ($ kernel ->getBundles () as $ bundle ) {
125
- $ rootPaths [] = $ bundle ->getPath ();
129
+ $ transPaths [] = $ bundle ->getPath ().'/Resources/ ' ;
130
+ $ transPaths [] = sprintf ('%s/Resources/%s/ ' , $ kernel ->getRootDir (), $ bundle ->getName ());
126
131
}
127
132
}
128
133
129
- foreach ($ rootPaths as $ rootPath ) {
130
- // get bundle directory
131
- $ translationsPath = $ rootPath .'/Resources/translations ' ;
134
+ // Extract used messages
135
+ $ extractedCatalogue = $ this ->extractMessages ($ locale , $ transPaths );
132
136
133
- $ output ->writeln (sprintf ('Translations in <info>%s</info> ' , $ translationsPath ));
137
+ // Load defined messages
138
+ $ currentCatalogue = $ this ->loadCurrentMessages ($ locale , $ transPaths , $ loader );
134
139
135
- // Extract used messages
136
- $ extractedCatalogue = $ this ->extractMessages ($ locale , $ rootPath );
140
+ // Merge defined and extracted messages to get all message ids
141
+ $ mergeOperation = new MergeOperation ($ extractedCatalogue , $ currentCatalogue );
142
+ $ allMessages = $ mergeOperation ->getResult ()->all ($ domain );
143
+ if (null !== $ domain ) {
144
+ $ allMessages = array ($ domain => $ allMessages );
145
+ }
137
146
138
- // Load defined messages
139
- $ currentCatalogue = $ this ->loadCurrentMessages ($ locale , $ translationsPath , $ loader );
147
+ // No defined or extracted messages
148
+ if (empty ($ allMessages ) || null !== $ domain && empty ($ allMessages [$ domain ])) {
149
+ $ outputMessage = sprintf ('No defined or extracted messages for locale "%s" ' , $ locale );
140
150
141
- // Merge defined and extracted messages to get all message ids
142
- $ mergeOperation = new MergeOperation ($ extractedCatalogue , $ currentCatalogue );
143
- $ allMessages = $ mergeOperation ->getResult ()->all ($ domain );
144
151
if (null !== $ domain ) {
145
- $ allMessages = array ($ domain => $ allMessages );
146
- }
147
-
148
- // No defined or extracted messages
149
- if (empty ($ allMessages ) || null !== $ domain && empty ($ allMessages [$ domain ])) {
150
- $ outputMessage = sprintf ('<info>No defined or extracted messages for locale "%s"</info> ' , $ locale );
151
-
152
- if (null !== $ domain ) {
153
- $ outputMessage .= sprintf (' <info>and domain "%s"</info> ' , $ domain );
154
- }
155
-
156
- $ output ->writeln ($ outputMessage );
157
-
158
- continue ;
152
+ $ outputMessage .= sprintf (' and domain "%s" ' , $ domain );
159
153
}
160
154
161
- // Load the fallback catalogues
162
- $ fallbackCatalogues = $ this ->loadFallbackCatalogues ($ locale , $ translationsPath , $ loader );
155
+ $ output ->warning ($ outputMessage );
163
156
164
- // Display header line
165
- $ headers = array ('State ' , 'Domain ' , 'Id ' , sprintf ('Message Preview (%s) ' , $ locale ));
166
- foreach ($ fallbackCatalogues as $ fallbackCatalogue ) {
167
- $ headers [] = sprintf ('Fallback Message Preview (%s) ' , $ fallbackCatalogue ->getLocale ());
168
- }
157
+ return ;
158
+ }
169
159
170
- // Iterate all message ids and determine their state
171
- $ rows = array ();
172
- foreach ($ allMessages as $ domain => $ messages ) {
173
- foreach (array_keys ($ messages ) as $ messageId ) {
174
- $ value = $ currentCatalogue ->get ($ messageId , $ domain );
175
- $ states = array ();
176
-
177
- if ($ extractedCatalogue ->defines ($ messageId , $ domain )) {
178
- if (!$ currentCatalogue ->defines ($ messageId , $ domain )) {
179
- $ states [] = self ::MESSAGE_MISSING ;
180
- }
181
- } elseif ($ currentCatalogue ->defines ($ messageId , $ domain )) {
182
- $ states [] = self ::MESSAGE_UNUSED ;
183
- }
160
+ // Load the fallback catalogues
161
+ $ fallbackCatalogues = $ this ->loadFallbackCatalogues ($ locale , $ transPaths , $ loader );
184
162
185
- if (!in_array (self ::MESSAGE_UNUSED , $ states ) && true === $ input ->getOption ('only-unused ' )
186
- || !in_array (self ::MESSAGE_MISSING , $ states ) && true === $ input ->getOption ('only-missing ' )) {
187
- continue ;
163
+ // Display header line
164
+ $ headers = array ('State ' , 'Domain ' , 'Id ' , sprintf ('Message Preview (%s) ' , $ locale ));
165
+ foreach ($ fallbackCatalogues as $ fallbackCatalogue ) {
166
+ $ headers [] = sprintf ('Fallback Message Preview (%s) ' , $ fallbackCatalogue ->getLocale ());
167
+ }
168
+ $ rows = array ();
169
+ // Iterate all message ids and determine their state
170
+ foreach ($ allMessages as $ domain => $ messages ) {
171
+ foreach (array_keys ($ messages ) as $ messageId ) {
172
+ $ value = $ currentCatalogue ->get ($ messageId , $ domain );
173
+ $ states = array ();
174
+
175
+ if ($ extractedCatalogue ->defines ($ messageId , $ domain )) {
176
+ if (!$ currentCatalogue ->defines ($ messageId , $ domain )) {
177
+ $ states [] = self ::MESSAGE_MISSING ;
188
178
}
179
+ } elseif ($ currentCatalogue ->defines ($ messageId , $ domain )) {
180
+ $ states [] = self ::MESSAGE_UNUSED ;
181
+ }
189
182
190
- foreach ($ fallbackCatalogues as $ fallbackCatalogue ) {
191
- if ($ fallbackCatalogue ->defines ($ messageId , $ domain ) && $ value === $ fallbackCatalogue ->get ($ messageId , $ domain )) {
192
- $ states [] = self ::MESSAGE_EQUALS_FALLBACK ;
183
+ if (!in_array (self ::MESSAGE_UNUSED , $ states ) && true === $ input ->getOption ('only-unused ' )
184
+ || !in_array (self ::MESSAGE_MISSING , $ states ) && true === $ input ->getOption ('only-missing ' )) {
185
+ continue ;
186
+ }
193
187
194
- break ;
195
- }
196
- }
188
+ foreach ( $ fallbackCatalogues as $ fallbackCatalogue ) {
189
+ if ( $ fallbackCatalogue -> defines ( $ messageId , $ domain ) && $ value === $ fallbackCatalogue -> get ( $ messageId , $ domain )) {
190
+ $ states [] = self :: MESSAGE_EQUALS_FALLBACK ;
197
191
198
- $ row = array ($ this ->formatStates ($ states ), $ domain , $ this ->formatId ($ messageId ), $ this ->sanitizeString ($ value ));
199
- foreach ($ fallbackCatalogues as $ fallbackCatalogue ) {
200
- $ row [] = $ this ->sanitizeString ($ fallbackCatalogue ->get ($ messageId , $ domain ));
192
+ break ;
201
193
}
194
+ }
202
195
203
- $ rows [] = $ row ;
196
+ $ row = array ($ this ->formatStates ($ states ), $ domain , $ this ->formatId ($ messageId ), $ this ->sanitizeString ($ value ));
197
+ foreach ($ fallbackCatalogues as $ fallbackCatalogue ) {
198
+ $ row [] = $ this ->sanitizeString ($ fallbackCatalogue ->get ($ messageId , $ domain ));
204
199
}
205
- }
206
200
207
- $ output ->table ($ headers , $ rows );
201
+ $ rows [] = $ row ;
202
+ }
208
203
}
204
+
205
+ $ output ->table ($ headers , $ rows );
209
206
}
210
207
211
208
private function formatState ($ state )
@@ -257,45 +254,51 @@ private function sanitizeString($string, $length = 40)
257
254
258
255
/**
259
256
* @param string $locale
260
- * @param string $rootPath
257
+ * @param array $transPaths
261
258
*
262
259
* @return MessageCatalogue
263
260
*/
264
- private function extractMessages ($ locale , $ rootPath )
261
+ private function extractMessages ($ locale , $ transPaths )
265
262
{
266
263
$ extractedCatalogue = new MessageCatalogue ($ locale );
267
- if (is_dir ($ rootPath .'/Resources/views ' )) {
268
- $ this ->getContainer ()->get ('translation.extractor ' )->extract ($ rootPath .'/Resources/views ' , $ extractedCatalogue );
264
+ foreach ($ transPaths as $ path ) {
265
+ $ path = $ path .'views ' ;
266
+ if (is_dir ($ path )) {
267
+ $ this ->getContainer ()->get ('translation.extractor ' )->extract ($ path , $ extractedCatalogue );
268
+ }
269
269
}
270
270
271
271
return $ extractedCatalogue ;
272
272
}
273
273
274
274
/**
275
275
* @param string $locale
276
- * @param string $translationsPath
276
+ * @param array $transPaths
277
277
* @param TranslationLoader $loader
278
278
*
279
279
* @return MessageCatalogue
280
280
*/
281
- private function loadCurrentMessages ($ locale , $ translationsPath , TranslationLoader $ loader )
281
+ private function loadCurrentMessages ($ locale , $ transPaths , TranslationLoader $ loader )
282
282
{
283
283
$ currentCatalogue = new MessageCatalogue ($ locale );
284
- if (is_dir ($ translationsPath )) {
285
- $ loader ->loadMessages ($ translationsPath , $ currentCatalogue );
284
+ foreach ($ transPaths as $ path ) {
285
+ $ path = $ path .'translations ' ;
286
+ if (is_dir ($ path )) {
287
+ $ loader ->loadMessages ($ path , $ currentCatalogue );
288
+ }
286
289
}
287
290
288
291
return $ currentCatalogue ;
289
292
}
290
293
291
294
/**
292
295
* @param string $locale
293
- * @param string $translationsPath
296
+ * @param array $transPaths
294
297
* @param TranslationLoader $loader
295
298
*
296
299
* @return MessageCatalogue[]
297
300
*/
298
- private function loadFallbackCatalogues ($ locale , $ translationsPath , TranslationLoader $ loader )
301
+ private function loadFallbackCatalogues ($ locale , $ transPaths , TranslationLoader $ loader )
299
302
{
300
303
$ fallbackCatalogues = array ();
301
304
$ translator = $ this ->getContainer ()->get ('translator ' );
@@ -306,7 +309,12 @@ private function loadFallbackCatalogues($locale, $translationsPath, TranslationL
306
309
}
307
310
308
311
$ fallbackCatalogue = new MessageCatalogue ($ fallbackLocale );
309
- $ loader ->loadMessages ($ translationsPath , $ fallbackCatalogue );
312
+ foreach ($ transPaths as $ path ) {
313
+ $ path = $ path .'translations ' ;
314
+ if (is_dir ($ path )) {
315
+ $ loader ->loadMessages ($ path , $ fallbackCatalogue );
316
+ }
317
+ }
310
318
$ fallbackCatalogues [] = $ fallbackCatalogue ;
311
319
}
312
320
}
0 commit comments