I experiment with Symfony2 Translation and I am very close to finish something, but I don't know what to do in this step.
I found this answer: https://stackoverflow.com/a/14331838/1162217
<?php
namespace TB\LanguageBundle\Listener;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class LocaleListener implements EventSubscriberInterface
{
private $defaultLocale;
public function __construct($defaultLocale = 'en')
{
$this->defaultLocale = $defaultLocale;
}
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
if (!$request->hasPreviousSession()) {
return;
}
if ($locale = $request->attributes->get('_locale')) {
$request->getSession()->set('_locale', $locale);
} else {
$request->setLocale("fr");
}
}
static public function getSubscribedEvents()
{
return array(
// must be registered before the default Locale listener
KernelEvents::REQUEST => array(array('onKernelRequest', 17)),
);
}
}
That works great, the locale really changes, but I have a problem with catalogues. The default_locale
is en, but when I change the locale via setLocale
to fr, the result in page looks like this:
Fr home
Another fr already translated sentence
i.did.not.translate.this.sentence.already.but.i.need.en.fallback.to.it (instead i want -> i am the english fallback sentence)
In other words the sentences which are not translated already are outputed as translation keys. Can I somehow achieve that instead of these bad translation keys the output would be the sentence from "fallback language" ?
My app/config.yml
look like (%locale%
is "en" in parameters.yml
):
translator: { fallback: "%locale%" }
default_locale: "%locale%"