Reputation: 21
My site is in 2 parts, public side with differents locales is working, admin side is translated in only 1 language.
The problem is that the lang file for public side is the same as admin side. I would like to use a different file for all admin routes
I tried to change the domain with {% trans_default_domain "admin" %}
but i need to put this line in all my twig files. I didn't find any solution to change the domain in the controller or elsewhere.
I tried also to use a specific locale for admin but the translations are not found and 'admin' is clearly not a language
Thanks for your help :)
Upvotes: 1
Views: 1162
Reputation: 21
I found an half-solution by replacing the Translator class, this post helps me Replacing the Translator service in Symfony 3
This solution change de translation domain for all the methods in the controller and the templates called in it (even the extended template and the includes)
My translations file is 'admin.fr.yml'
I added in Service folder my new Translator class
<?php
namespace App\Service;
use Symfony\Component\Translation\TranslatorBagInterface;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\Translation\Translator as BaseTranslator;
class Translator implements LegacyTranslatorInterface, TranslatorInterface, TranslatorBagInterface
{
private $defaultDomain = 'messages';
private $translator;
public function __construct(BaseTranslator $translator)
{
$this->translator = $translator;
}
public function getDefaultDomain(): string
{
return $this->defaultDomain;
}
public function setDefaultDomain(string $defaultDomain): Translator
{
$this->defaultDomain = $defaultDomain;
return $this;
}
public function trans($id, array $parameters = [], $domain = null, $locale = null)
{
if (null === $domain) {
$domain = $this->defaultDomain;
}
return $this->translator->trans($id, $parameters, $domain, $locale);
}
public function getCatalogue($locale = null)
{
return $this->translator->getCatalogue($locale);
}
public function transChoice($id, $number, array $parameters = [], $domain = null, $locale = null)
{
return $this->translator->transChoice($id, $number, $parameters, $domain, $locale);
}
public function setLocale($locale)
{
return $this->translator->setLocale($locale);
}
public function getLocale()
{
return $this->translator->getLocale();
}
}
Then in services.yaml
App\Service\Translator:
decorates: translator
Then in my admin controller folder i created an abstract class BaseAdminController
<?php
namespace App\Controller\Admin;
use App\Service\Translator;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Contracts\Translation\TranslatorInterface;
abstract class BaseAdminController extends AbstractController
{
/**
* @var Translator
*/
protected $translator;
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
$this->translator->setDefaultDomain('admin');
}
}
Now in each controller where i want admin translations, i just need to replace "extends AbstractController" with "extends BaseAdminController"
If i want to define a construct, i call the parent in it
public function __construct(Translator $translator, EntityManagerInterface $manager)
{
$this->manager = $manager;
parent::__construct($translator);
}
Upvotes: 1