1
votes

I develop websites based on the Symfony2 framework, and I have quality problems due to missing translations. This is particular true for non visible translations like image alt attribut or page meta-data.

To solve that problem, I would like Symfony to stop the page render on missing translation, by throwing an exception.

How can I tell the Symfony translator not to accept any missing translation in the dev environment? (for translations made from code, using the translator service and from templates, using the trans filter)

1
Why would you do this, the symfony developer toolbar informs you about missing translations!Nickolaus
@Nickolaus I don't have that. I'm using Symfony 2.6.11, with stock log and translation config. In which version are you seeing that ?Congelli501

1 Answers

1
votes

If you are referring to translations inside your code and not inside the templates you will have to write a custom function:

private function translate($translationKey, array $parameters = array(), $translationDomain = 'messages') 
{

    $tranlation = $this->get('translator.default')->trans($translationKey, $parameters, $translationDomain);
    if ($tranlation != $translationKey) {
        return $tranlation;
    }
    else {
        throw new \Exception();
    }
}

Hint: You can/should create a custom exception for this case

Update:

What you want will only be possible if you create your own translator... this translator class should return null if the translation could not be found:
https://github.com/Orbitale/TranslationBundle/blob/master/Translation/Translator.php

Additional Info (referring to the comment): The info field for missing translations in the symfony developer toolbar seems to be quite new, introduced in a late 2.6 or an early 2.7 release...
anyway it would look like this:

enter image description here

But: I just noticed that you can see missing translation-warnings also in earlier versions, you will find them in the toolbar profiler in Logs section