<p dir="auto">Today I had to create a command to export all translation messages to file in JSON format. The easiest way would be to get the catalogues from the <code class="notranslate">translator</code> service. But that was not possible as there is no getter.</p>
<p dir="auto">I had to work around this by doing:</p>
<div class="highlight highlight-text-html-php notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="$translator = $this-&gt;getContainer()-&gt;get('translator');
/* @var $translator \Symfony\Bundle\FrameworkBundle\Translation\Translator */
foreach ($locales as $locale) {
    $translator-&gt;setLocale($locale);
    $translator-&gt;trans(''); // this is needed to load the translation catalogue
}

// workaround to get the catalogues
$reflProp = new \ReflectionProperty(get_class($translator), 'catalogues');
$reflProp-&gt;setAccessible(true);
$catalogues = $reflProp-&gt;getValue($translator);"><pre class="notranslate"><span class="pl-s1"><span class="pl-c1">$</span>translator</span> = <span class="pl-s1"><span class="pl-c1">$</span><span class="pl-smi">this</span></span>-&gt;<span class="pl-en">getContainer</span>()-&gt;<span class="pl-en">get</span>(<span class="pl-s">'translator'</span>);
<span class="pl-c">/* @var $translator \Symfony\Bundle\FrameworkBundle\Translation\Translator */</span>
<span class="pl-k">foreach</span> (<span class="pl-s1"><span class="pl-c1">$</span>locales</span> <span class="pl-k">as</span> <span class="pl-s1"><span class="pl-c1">$</span>locale</span>) {
    <span class="pl-s1"><span class="pl-c1">$</span>translator</span>-&gt;<span class="pl-en">setLocale</span>(<span class="pl-s1"><span class="pl-c1">$</span>locale</span>);
    <span class="pl-s1"><span class="pl-c1">$</span>translator</span>-&gt;<span class="pl-en">trans</span>(<span class="pl-s">''</span>); <span class="pl-c">// this is needed to load the translation catalogue</span>
}

<span class="pl-c">// workaround to get the catalogues</span>
<span class="pl-s1"><span class="pl-c1">$</span>reflProp</span> = <span class="pl-k">new</span> \<span class="pl-v">ReflectionProperty</span>(get_class(<span class="pl-s1"><span class="pl-c1">$</span>translator</span>), <span class="pl-s">'catalogues'</span>);
<span class="pl-s1"><span class="pl-c1">$</span>reflProp</span>-&gt;<span class="pl-en">setAccessible</span>(<span class="pl-c1">true</span>);
<span class="pl-s1"><span class="pl-c1">$</span>catalogues</span> = <span class="pl-s1"><span class="pl-c1">$</span>reflProp</span>-&gt;<span class="pl-en">getValue</span>(<span class="pl-s1"><span class="pl-c1">$</span>translator</span>);</pre></div>
<p dir="auto">Doing this with reflection is of course very hacky, but it is the easiest way to get all translations.</p>
<p dir="auto">It would be great if the <code class="notranslate">translator</code> service could provide a <code class="notranslate">getCatalogue($locale)</code> method, returning the message catalogue with all available messages.</p>
<p dir="auto">Thanks in advance.</p>
<p dir="auto">When using a submit button (nearly all the forms on the internet) and you use the getErrorsAsString() method on your form you will get:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Error: Call to undefined method Symfony\Component\Form\SubmitButton::getErrorsAsString"><pre class="notranslate"><code class="notranslate">Error: Call to undefined method Symfony\Component\Form\SubmitButton::getErrorsAsString
</code></pre></div>