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
744 views
in Technique[技术] by (71.8m points)

symfony - Symfony2 locale languages whole page event listener

I need to create a system like the facebook lang system when a user clicks on language to example france('fr') the page will reload and all the content in messages.fr.yml will be displayed...

I tried to make route like /language/{localExtension} but when I setLocale there and then redirected it didn't work... I don't know i assuming its just for certain page not global? Because when i set setLocale('fr') at the top of my controller it works...

I found some articles when is using the www.example.com/contact/en, /contact/fr etc

But i want /contact and content displayed from previous chosen language. Or default 'en' like now when user didn't change it...

I was googling all day and i think it should be done with... service => listener and... on kernel.request? or something like that.

Here are intresting link Symfony2 wrong locale detection? i think that's what i need? or? I tried to set service and create listener but some errors appear and i don't even know if this is the way how to create it :/

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Yes for some reason you need to use a listener:

<?php

namespace YourBundleListener;

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 your listener in your service.xml:

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

An example how to implement the language switcher in your twig template:

{% for locale in ['en', 'fr','zh'] %}
    <li>
      <a href="{{ path(app.request.get('_route'), app.request.get('_route_params')|merge({'_locale' : locale})) }}">
        {% if locale == 'en' %}
        <img title="English" src="{{ asset('bundles/fkmywebsite/images/UnitedStates.png') }}" alt="English" height="30" width="30"/>
        {% elseif locale == 'fr' %}
        <img title="Fran?ais" src="{{ asset('bundles/fkmywebsite/images/France.png') }}" alt="Fran?ais" height="30" width="30"/>
        {% endif %}
      </div>
    </li>
{% endfor %}

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

...