0

How to detect missing or unused translations from SonataAdmin bundle?

There is a nice tool in Symfony, to track missing/unused translations:

php bin/console debug:translation en AppBundle

However it can not recognize translations from SonataAdmin form field labels. When I run the command, response is:

 ---------- ---------- ------------------------------ ---------------------- 
  State      Domain     Id                             Message Preview (en)  
 ---------- ---------- ------------------------------ ---------------------- 
   unused    messages   app.treasure_type.form.title   Treasure              
 ---------- ---------- ------------------------------ ---------------------- 

It should say that translation is used, because actually it's used and is working fine:

$formMapper
    ->add('title', null, [
        'label' => 'app.treasure_type.form.title',
    ]);

translation file in src/AppBundle/Resources/translations/messages.en.yml

app.treasure_type.form.title: "Treasure"

configuration in app/config/config.yml

parameters:
    locale: en
framework:
    translator: { fallbacks: ['%locale%'] }
Maksym Moskvychev
  • 1,471
  • 8
  • 11

2 Answers2

1

The solution is to use JMSTranslationBundle, which is capable to extract translations from Admin classes.

composer require jms/translation-bundle

Next step is to enable bundle in AppKernel. And then to extract translation keys - use command:

php bin/console translation:extract en --bundle=AppBundle 
Maksym Moskvychev
  • 1,471
  • 8
  • 11
0

Try one of these:

php bin/console debug:translation en --all
php bin/console debug:translation en src/YourNamespace/YourBundle
php bin/console debug:translation en src/YourBundle
php bin/console debug:translation en app

Try narrowing the search by using these options:

--domain [DOMAIN] - The messages domain (like --domain messages, --domain validators, --domain SonataAdminBundle)

--only-missing - Displays only missing messages

--all - Loads messages from all registered bundles

And consider moving messages that are used in the backend to it's own domain.

genesst
  • 1,333
  • 1
  • 10
  • 39
  • Actually, all these will not work because debug:translation command is capable to fetch translations only from Twig templates and PHP templates. The right solution here would be to use JMSTranslationBundle for translations, which is capable to extract them from SonataAdmin – Maksym Moskvychev Oct 09 '17 at 12:15