Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
614 views
in Technique[技术] by (71.8m points)

internationalization - Symfony 2.1 set locale

Does anybody know how to set the locale in Symfony2.1?

I am trying with:

$this->get('session')->set('_locale', 'en_US');

and

$this->get('request')->setLocale('en_US');

but none of those has any effect, the devbar tells me:

Session Attributes: No session attributes

Anyway, it is always the fallback locale that is used, as defined in config.yml

(PS: I am trying to set up the translation system as described here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Even though the Symfony 2.1 states that you can simply set the locale via the Request or Session objects, I never managed to have it working, setting the locale simply has no effect.

So I ended up using a listener coupled with twig routing to handle the locale/language:

The listener:

namespace FKMyWebsiteBundleListener;

use SymfonyComponentHttpKernelEventGetResponseEvent;
use SymfonyComponentHttpKernelKernelEvents;
use SymfonyComponentEventDispatcherEventSubscriberInterface;

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)),
        );
    }
}

Register the listener in service.xml:

<service id="fk.my.listener" class="FKMyWebsiteBundleListenerLocaleListener">
    <argument>%locale%</argument>
    <tag name="kernel.event_subscriber"/>
</service>

The routing must look like:

homepage:
pattern:  /{_locale}
defaults: { _controller: FKMyWebsiteBundle:Default:index, _locale: en }
requirements:
    _locale: en|fr|zh

And handle the routing with:

{% for locale in ['en', 'fr', 'zh'] %}
    <a href="{{ path(app.request.get('_route'), app.request.get('_route_params')|merge({'_locale' : locale})) }}">
{% endfor %}

This way, the locale will automatically be set when you click on a link to change the language.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...