I am trying to override the Symfony translator to have some translations from my database. I am first checking if the translation is not in a catalogue and if not load it from the database
<?php
namespace Competitive\TranslationBundle\Translation;
use Competitive\TranslationBundle\Entity\TranslationManager;
use Symfony\Component\Translation\MessageSelector;
use Symfony\Component\Translation\Translator as BaseTranslator;
class Translator extends BaseTranslator
{
/**
* @var TranslationManager
*/
private $translationManager;
public function __construct($locale, TranslationManager $translationManager, MessageSelector $selector = null, $cacheDir = null, $debug = false)
{
parent::__construct($locale, $selector, $cacheDir, $debug);
$this->translationManager = $translationManager;
}
/**
* {@inheritdoc}
*/
public function trans($id, array $parameters = array(), $domain = null, $locale = null)
{
$catalogueDomain = $domain;
if (null === $catalogueDomain) {
$catalogueDomain = 'messages';
}
$locale = $locale === null ? $this->getLocale() : $locale;
if ($this->getCatalogue($locale)->has($id, $domain)) {
return parent::trans($id, $parameters, $catalogueDomain, $locale);
}
$translations = $this->translationManager->findTranslationsBy([
'key' => $id,
'locale' => $locale
]);
if (empty($translations)) {
$translation = $this->translationManager->create($id, $id, $locale);
} elseif (null === $domain) {
$translation = $translations[0];
} else {
foreach ($translations as $trans) {
if ($trans->getDomain() == $domain) {
$translation = $trans;
break;
}
}
if (!isset($translation)) {
$translation = $translations[0];
}
}
return strtr($translation->getValue(), $parameters);
}
/**
* {@inheritdoc}
*/
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
{
return $this->trans($id, $parameters, $domain, $locale);
}
}
translation.yml
services:
translator:
class: Competitive\TranslationBundle\Translation\Translator
arguments:
- %locale%
- @competitive_translation.translation_manager
The translation is loaded from the database without problem.
But $this->getCatalogue($locale)->has($id, $domain)
always returns false (the translations from the catalogue were working before the override)
And my cache translation folder app/cache/dev/translations
is not generated