0

I am using Symfony2 and i wanted to add multilanguage support to my site. I have the bundle symfony/symfony. However, when i do as the documentation says for the setting of the locale variable. I get the following error. my class looks like

<?php
// src/AppBundle/EventListener/LocaleListener.php
namespace AppBundle\EventListener;

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($request->getSession()->get('_locale', $this->defaultLocale));
    }
}

static public function getSubscribedEvents()
{
    return array(
        // must be registered before the default Locale listener
        KernelEvents::REQUEST => array(array('onKernelRequest', 17)),
    );
}
}

and the service.yml

 app.locale_listener:
        class: AppBundle\EventListener\LocaleListener
        arguments: ['%kernel.default_locale%']
        tags:
            - { name: kernel.event_subscriber }`

if any of you have a suggestion as to how to solve this, i'd be glad to hear it.

EDIT: I now fixed the problem, I noticed after a while that the class already existed somewhere else in the vendor folder. That class worked and looked identical to mine. So I inserted my code and that solved it.

gemina
  • 1
  • 3

2 Answers2

0

You don't have to user a listener to use multi-language. You have translations, you can set the locale variable on the routing. And set a default value.

Example in my app/config/routing.yml:

blog_bundle:
    resource: "@BlogBundle/Resources/config/routing.yml"
    prefix:   /{_locale}/blog
    requirements:
        _locale: fr|en|it|es
    defaults: { _locale: fr}
Stan Sarr
  • 243
  • 1
  • 8
0

It looks like the Method Signature of your getSubscribedEvents isn't identical to the interface declaration.

Try switching static public function getSubscribedEvents() to public static function getSubscribedEvents()

public static function getSubscribedEvents()
{
    return array(
        // must be registered before the default Locale listener
        KernelEvents::REQUEST => array(array('onKernelRequest', 17)),
    );
}

Edit in regard to Stans answer: If it is ok for you to always carry the local in all of your routes that is the correct way. Just add _locale to the route and symfony will automatically set it in the request. If you don't want it in your routes and set it based of a cookie value or something like your current TLD the Subscriber Method is the correct one.

Joe
  • 2,356
  • 10
  • 15
  • Thanks for the comment, I didn't see that I had them switched. And I do not want to have the _locale in my route. So like you said, I'll have to use this method. I have now tried to switch the static and public, but it didn't matter. I still get the same error. – gemina Jul 04 '16 at 08:16