Skip to content

Instantly share code, notes, and snippets.

@kunicmarko20
Last active December 23, 2022 03:59
Show Gist options
  • Select an option

  • Save kunicmarko20/02a42c76f638322d58b1def7d2e770d7 to your computer and use it in GitHub Desktop.

Select an option

Save kunicmarko20/02a42c76f638322d58b1def7d2e770d7 to your computer and use it in GitHub Desktop.

Revisions

  1. kunicmarko20 revised this gist Apr 29, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion LocaleListener.php
    Original file line number Diff line number Diff line change
    @@ -34,7 +34,7 @@ public function __construct(RouterInterface $router, $languages, $defaultLanguag
    $this->languages = $languages;
    $this->defaultLanguage = $defaultLanguage;
    $context = new RequestContext("/");
    }

    public function onKernelRequest(GetResponseEvent $event)
    {
  2. kunicmarko20 revised this gist Apr 21, 2017. 3 changed files with 16 additions and 20 deletions.
    20 changes: 0 additions & 20 deletions LocaleListener.php
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,3 @@
    //////////////////////////////////////////////
    //parameters:
    // locale: de
    // languages: [de, en]
    //
    //services:
    // app.locale.listener:
    // class: YourBundle\EventListener\LocaleListener
    // arguments: ["@router","%languages%","%locale%"]
    // tags:
    // - { name: kernel.event_subscriber }

    // in default routing import your routing file with routes that have multilanguage
    //language_routing:
    // resource: "@YourBundle/Resources/config/multilanguage-routes.yml"
    // prefix: /{_locale}
    // requirements:
    // _locale: de|en
    // defaults: { _locale: de}

    <?php
    namespace YourBundle\EventListener;

    6 changes: 6 additions & 0 deletions routing.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    language_routing:
    resource: "@YourBundle/Resources/config/multilanguage-routes.yml"
    prefix: /{_locale}
    requirements:
    _locale: de|en
    defaults: { _locale: de}
    10 changes: 10 additions & 0 deletions services.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    parameters:
    locale: de
    languages: [de, en]

    services:
    app.locale.listener:
    class: YourBundle\EventListener\LocaleListener
    arguments: ["@router","%languages%","%locale%"]
    tags:
    - { name: kernel.event_subscriber }
  3. kunicmarko20 revised this gist Feb 7, 2017. 1 changed file with 25 additions and 19 deletions.
    44 changes: 25 additions & 19 deletions LocaleListener.php
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,14 @@
    ////services:
    //////////////////////////////////////////////
    //parameters:
    // locale: de
    // languages: [de, en]
    //
    //services:
    // app.locale.listener:
    // class: YourBundle\EventListener\LocaleListener
    // arguments: ["@router"]
    // tags:
    // - { name: kernel.event_subscriber }
    // arguments: ["@router","%languages%","%locale%"]
    // tags:
    // - { name: kernel.event_subscriber }

    // in default routing import your routing file with routes that have multilanguage
    //language_routing:
    @@ -37,16 +42,19 @@ class LocaleListener implements EventSubscriberInterface
    */
    private $urlMatcher;


    private $oldUrl;
    private $newUrl;

    public function __construct(RouterInterface $router)
    private $languages;
    private $defaultLanguage;

    public function __construct(RouterInterface $router, $languages, $defaultLanguage = 'de')
    {
    $this->routeCollection = $router->getRouteCollection();
    $this->languages = $languages;
    $this->defaultLanguage = $defaultLanguage;
    $context = new RequestContext("/");
    $this->urlMatcher = new UrlMatcher($this->routeCollection, $context);
    }


    public function onKernelRequest(GetResponseEvent $event)
    {
    @@ -76,17 +84,15 @@ public function onKernelRequest(GetResponseEvent $event)

    }
    }
    private function checkLanguage(){
    if(preg_match_all("/\/en\//", $this->newUrl)){
    return null;
    }
    if(preg_match_all("/\/de\//", $this->newUrl)){
    return null;
    private function checkLanguage(){
    foreach($this->languages as $language){
    if(preg_match_all("/\/$language\//", $this->newUrl))
    return null;
    if(preg_match_all("/\/$language\//", $this->oldUrl))
    return $language;
    }
    return $this->checkOldUrl();
    }
    private function checkOldUrl(){
    return preg_match_all("/\/en\//", $this->oldUrl) ? 'en' : 'de';

    return $this->defaultLanguage;
    }
    public static function getSubscribedEvents()
    {
  4. kunicmarko20 revised this gist Jan 10, 2017. 1 changed file with 12 additions and 32 deletions.
    44 changes: 12 additions & 32 deletions LocaleListener.php
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    ////services:
    // app.locale.redirect:
    // class: YourBundle\EventListener\LocaleRedirectListener
    // arguments: ["@router", "%kernel.default_locale%"]
    // app.locale.listener:
    // class: YourBundle\EventListener\LocaleListener
    // arguments: ["@router"]
    // tags:
    // - { name: kernel.event_subscriber }

    @@ -17,20 +17,15 @@
    namespace YourBundle\EventListener;

    use Symfony\Component\HttpFoundation\RedirectResponse;

    use Symfony\Component\Routing\RouterInterface;
    use Symfony\Component\HttpKernel\Event\GetResponseEvent;
    use Symfony\Component\HttpKernel\KernelEvents;
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    use Symfony\Component\Routing\Matcher\UrlMatcher;
    use Symfony\Component\Routing\RequestContext;

    class LocaleRedirectListener implements EventSubscriberInterface
    class LocaleListener implements EventSubscriberInterface
    {
    /**
    * @var Symfony\Component\Routing\RouterInterface
    */
    private $router;

    /**
    * @var routeCollection \Symfony\Component\Routing\RouteCollection
    @@ -42,32 +37,13 @@ class LocaleRedirectListener implements EventSubscriberInterface
    */
    private $urlMatcher;

    /**
    * @var string
    */
    private $defaultLocale;

    /**
    * @var string
    */
    private $localeRouteParam;

    /**
    * @var string
    */
    private $oldUrl;

    /**
    * @var string
    */
    private $newUrl;

    public function __construct(RouterInterface $router, $defaultLocale = 'de', $localeRouteParam = '_locale')
    public function __construct(RouterInterface $router)
    {
    $this->router = $router;
    $this->routeCollection = $router->getRouteCollection();
    $this->defaultLocale = $defaultLocale;
    $this->localeRouteParam = $localeRouteParam;
    $context = new RequestContext("/");
    $this->urlMatcher = new UrlMatcher($this->routeCollection, $context);
    }
    @@ -83,25 +59,29 @@ public function onKernelRequest(GetResponseEvent $event)
    $this->newUrl = $request->getPathInfo();
    $this->oldUrl = $request->headers->get('referer');
    $locale = $this->checkLanguage();
    if($locale === null) return;

    $request->setLocale($locale);


    $pathLocale = "/".$locale.$this->newUrl;

    //We have to catch the ResourceNotFoundException
    try {
    //Try to match the path with the local prefix
    $this->urlMatcher->match($pathLocale);
    $event->setResponse(new RedirectResponse($pathLocale));
    } catch (\Symfony\Component\Routing\Exception\ResourceNotFoundException $e) {

    } catch (\Symfony\Component\Routing\Exception\MethodNotAllowedException $e) {

    }
    }
    private function checkLanguage(){
    if(preg_match_all("/\/en\//", $this->newUrl)){
    return 'en';
    return null;
    }
    if(preg_match_all("/\/de\//", $this->newUrl)){
    return 'de';
    return null;
    }
    return $this->checkOldUrl();
    }
  5. kunicmarko20 created this gist Dec 15, 2016.
    118 changes: 118 additions & 0 deletions LocaleListener.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,118 @@
    ////services:
    // app.locale.redirect:
    // class: YourBundle\EventListener\LocaleRedirectListener
    // arguments: ["@router", "%kernel.default_locale%"]
    // tags:
    // - { name: kernel.event_subscriber }

    // in default routing import your routing file with routes that have multilanguage
    //language_routing:
    // resource: "@YourBundle/Resources/config/multilanguage-routes.yml"
    // prefix: /{_locale}
    // requirements:
    // _locale: de|en
    // defaults: { _locale: de}

    <?php
    namespace YourBundle\EventListener;

    use Symfony\Component\HttpFoundation\RedirectResponse;

    use Symfony\Component\Routing\RouterInterface;
    use Symfony\Component\HttpKernel\Event\GetResponseEvent;
    use Symfony\Component\HttpKernel\KernelEvents;
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    use Symfony\Component\Routing\Matcher\UrlMatcher;
    use Symfony\Component\Routing\RequestContext;

    class LocaleRedirectListener implements EventSubscriberInterface
    {
    /**
    * @var Symfony\Component\Routing\RouterInterface
    */
    private $router;

    /**
    * @var routeCollection \Symfony\Component\Routing\RouteCollection
    */
    private $routeCollection;

    /**
    * @var urlMatcher \Symfony\Component\Routing\Matcher\UrlMatcher;
    */
    private $urlMatcher;

    /**
    * @var string
    */
    private $defaultLocale;

    /**
    * @var string
    */
    private $localeRouteParam;

    /**
    * @var string
    */
    private $oldUrl;

    /**
    * @var string
    */
    private $newUrl;

    public function __construct(RouterInterface $router, $defaultLocale = 'de', $localeRouteParam = '_locale')
    {
    $this->router = $router;
    $this->routeCollection = $router->getRouteCollection();
    $this->defaultLocale = $defaultLocale;
    $this->localeRouteParam = $localeRouteParam;
    $context = new RequestContext("/");
    $this->urlMatcher = new UrlMatcher($this->routeCollection, $context);
    }

    public function onKernelRequest(GetResponseEvent $event)
    {
    //GOAL:
    // Redirect all incoming requests to their /locale/route equivalent when exists.
    // Do nothing if it already has /locale/ in the route to prevent redirect loops
    // Do nothing if the route requested has no locale param

    $request = $event->getRequest();
    $this->newUrl = $request->getPathInfo();
    $this->oldUrl = $request->headers->get('referer');
    $locale = $this->checkLanguage();
    $request->setLocale($locale);


    $pathLocale = "/".$locale.$this->newUrl;
    //We have to catch the ResourceNotFoundException
    try {
    //Try to match the path with the local prefix
    $this->urlMatcher->match($pathLocale);
    $event->setResponse(new RedirectResponse($pathLocale));
    } catch (\Symfony\Component\Routing\Exception\ResourceNotFoundException $e) {

    }
    }
    private function checkLanguage(){
    if(preg_match_all("/\/en\//", $this->newUrl)){
    return 'en';
    }
    if(preg_match_all("/\/de\//", $this->newUrl)){
    return 'de';
    }
    return $this->checkOldUrl();
    }
    private function checkOldUrl(){
    return preg_match_all("/\/en\//", $this->oldUrl) ? 'en' : 'de';
    }
    public static function getSubscribedEvents()
    {
    return array(
    // must be registered before the default Locale listener
    KernelEvents::REQUEST => array(array('onKernelRequest', 17)),
    );
    }
    }