Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8253115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:45:55+00:00 2026-06-08T00:45:55+00:00

What i want to do is, to detect the language of a users browser

  • 0

What i want to do is, to detect the language of a users browser and redirect him to a page containing the locale in url.

I thought, the easiest way would be, to register a kernel listener. So that is what i did:

services:
    kernel__request_listener:
        class: Me\MainBundle\Listener\KernelRequestListener
        tags:
            - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
        calls:
            - [ setContainer, [ @service_container ] ]

KernelRequestListener.php

...
/**
 * @Service
 */
class KernelRequestListener
{
    /**
     * @Observe("kernel.request")
     */
    public function onKernelRequest( GetResponseEvent $response )
    {

    if( $newLocale = $this->newLocale() )
    {
        $parmArray = $request->get('_route_params');
        $parmArray['_locale'] = $newLocale;

        $redirectResponse = new RedirectResponse( $this->getContainer()->get('router')->generate($request->get('_route'), $parmArray) );
        $redirectResponse->headers->setCookie( new Cookie('b_locale', $newLocale, time() + 2592000) );

        $response->setResponse( $redirectResponse );
    }
    }
...
}

The method $this->newLocale() just detects if the user should be redirected to another language and returns the new language code (i.e. DE or FR).

Here comes the problem:
I am using assetics to compress the js files and jms/i18n-routing-bundle to do the locale-based routing. When the kernel listener switches locale, the page starts loading the js files over and over again. also, there are several pages (i.e. the profiler, login/logout etc.) where no redirect should take place as it makes no sense or breaks smthing.

Is a kernel listener the right place to do such a redirection or is there any better place. How to resolve the problems above?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T00:45:56+00:00Added an answer on June 8, 2026 at 12:45 am

    Add, before your code:

    public function onKernelRequest(GetResponseEvent $event)
    {
        if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
            return;
        }
    
        $request = $event->getRequest();
        if ($request->getRequestFormat() !== 'html') {
            return;
        }
    
        [CODE]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a multi-language page, I want to detect client browser's language then make
I want to detect the browser language so I can switch languages when people
Possible Duplicate: JavaScript for detecting browser language preference I want to detect the language
I just want to generate a code that will detect the current language of
I want to show a button only for English users, is there a way
I have question,I want to redirect page using javascript which the code is to
I want to detect the automatic language from the device .Basically , I want
I want to detect programming language with ruby For example: (PHP) $a = array(1,2,3);
I want to use Google Language Detection API in my app to detect language
I want to detect cases where an Ajax call is in progress and then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.