@@ -329,9 +329,9 @@ protected function getContainer($loader)
329
329
return $ container ;
330
330
}
331
331
332
- public function getTranslator ($ loader , $ options = [], $ loaderFomat = 'loader ' , $ translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator ' , $ defaultLocale = 'en ' )
332
+ public function getTranslator ($ loader , $ options = [], $ loaderFomat = 'loader ' , $ translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator ' , $ defaultLocale = 'en ' , array $ enabledLocales = null )
333
333
{
334
- $ translator = $ this ->createTranslator ($ loader , $ options , $ translatorClass , $ loaderFomat , $ defaultLocale );
334
+ $ translator = $ this ->createTranslator ($ loader , $ options , $ translatorClass , $ loaderFomat , $ defaultLocale, $ enabledLocales );
335
335
336
336
if ('loader ' === $ loaderFomat ) {
337
337
$ translator ->addResource ('loader ' , 'foo ' , 'fr ' );
@@ -371,6 +371,31 @@ public function testWarmup()
371
371
$ this ->assertEquals ('répertoire ' , $ translator ->trans ('folder ' ));
372
372
}
373
373
374
+ public function testEnabledLocales ()
375
+ {
376
+ $ loader = new \Symfony \Component \Translation \Loader \YamlFileLoader ();
377
+ $ resourceFiles = [
378
+ 'fr ' => [
379
+ __DIR__ .'/../Fixtures/Resources/translations/messages.fr.yml ' ,
380
+ ],
381
+ ];
382
+
383
+ // prime the cache without configuring the enabled locales
384
+ $ translator = $ this ->getTranslator ($ loader , ['cache_dir ' => $ this ->tmpDir , 'resource_files ' => $ resourceFiles ], 'yml ' , Translator::class, 'en ' , null );
385
+ $ translator ->setFallbackLocales (['fr ' ]);
386
+ $ translator ->warmup ($ this ->tmpDir );
387
+
388
+ $ this ->assertSame (2 , count (glob ($ this ->tmpDir .'/catalogue.*.*.php ' )), 'Both "en" and "fr" catalogues are generated. ' );
389
+
390
+ // prime the cache and configure the enabled locales
391
+ $ this ->deleteTmpDir ();
392
+ $ translator = $ this ->getTranslator ($ loader , ['cache_dir ' => $ this ->tmpDir , 'resource_files ' => $ resourceFiles ], 'yml ' , Translator::class, 'en ' , ['fr ' ]);
393
+ $ translator ->setFallbackLocales (['fr ' ]);
394
+ $ translator ->warmup ($ this ->tmpDir );
395
+
396
+ $ this ->assertSame (1 , count (glob ($ this ->tmpDir .'/catalogue.*.*.php ' )), 'Only the "fr" catalogue is generated. ' );
397
+ }
398
+
374
399
public function testLoadingTranslationFilesWithDotsInMessageDomain ()
375
400
{
376
401
$ loader = new \Symfony \Component \Translation \Loader \YamlFileLoader ();
@@ -386,14 +411,15 @@ public function testLoadingTranslationFilesWithDotsInMessageDomain()
386
411
$ this ->assertEquals ('It works! ' , $ translator ->trans ('message ' , [], 'domain.with.dots ' ));
387
412
}
388
413
389
- private function createTranslator ($ loader , $ options , $ translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator ' , $ loaderFomat = 'loader ' , $ defaultLocale = 'en ' )
414
+ private function createTranslator ($ loader , $ options , $ translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator ' , $ loaderFomat = 'loader ' , $ defaultLocale = 'en ' , array $ enabledLocales = null )
390
415
{
391
416
if (null === $ defaultLocale ) {
392
417
return new $ translatorClass (
393
418
$ this ->getContainer ($ loader ),
394
419
new MessageFormatter (),
395
420
[$ loaderFomat => [$ loaderFomat ]],
396
- $ options
421
+ $ options ,
422
+ $ enabledLocales
397
423
);
398
424
}
399
425
@@ -402,7 +428,8 @@ private function createTranslator($loader, $options, $translatorClass = '\Symfon
402
428
new MessageFormatter (),
403
429
$ defaultLocale ,
404
430
[$ loaderFomat => [$ loaderFomat ]],
405
- $ options
431
+ $ options ,
432
+ $ enabledLocales
406
433
);
407
434
}
408
435
}
0 commit comments