Vlamir Carbonari
Vlamir Carbonari

Reputation: 336

Missing translations in a Symfony application

enter image description here

I have a web app build on Symfony 5.2. In some views, the profiler says that are missing translations.

There is a way to use translations only for validators and security messages, and not to the app strings at all?

This is my translation.yml:

framework:
    default_locale: pt_BR
    translator:
        default_path: '%kernel.project_dir%/translations'
        enabled: true
        fallbacks:
            - en

Upvotes: 0

Views: 807

Answers (1)

dbrumann
dbrumann

Reputation: 17166

I know it sounds a bit silly, but you can just not use the translator for strings that you don't need to have translated.

In practice that means not using |trans in your template and most likely telling your forms that labels should not be translated. You can achieve this by disabling the option translation_domain on your form types, either manually or by creating a FormExtension that does it by default.

The FormTypeExtension can probably look something like this:

class TranslationDomainDefaultDisableExtension extends AbstractTypeExtension
{
    public static function getExtendedTypes()
    {
        return [FormType::class]; // The most generic type means, all types extending from it are also affected
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefault('translation_domain', false);
    }
}

If you want to keep translations to have them for future use and just need to get rid of the warnings for missing translations, you can update your existing translations instead:

php bin/console translation:update

The translation:update command extracts translation strings from templates of a given bundle or the default translations directory. It can display them or merge the new ones into the translation files.

When new translation strings are found it can automatically add a prefix to the translation message.

This will update (or create) the translation files you keep in translations/ with the missing strings using the key as text, which probably is the behavior you want. Be careful though, that your fallback to english might be disrupted by these new translations. You will have to check and then likely adjust the changes in the file for this.

Upvotes: 1

Related Questions