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 8697613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:34:09+00:00 2026-06-13T01:34:09+00:00

I am trying to have a language switcher on my symfony 2.1 website. I

  • 0

I am trying to have a language switcher on my symfony 2.1 website.

I followed the official documentation, set the translation files but setting the locale with $request->setLocale(‘en_US’); doesn’t seem to work. After some research, I found this question which provides a beginning of an answer with this listener technique.

However, I still don’t manage to have it working, I’m not so sure about my listener declaration, is something wrong with it?

My controller:

public function englishAction(Request $request)
{
    $this->get('session')->set('_locale', 'en_US');
    return $this->redirect($request->headers->get('referer'));
}

Service declaration in config.yml:

services:
    my_listener:
        class:        "FK\MyWebsiteBundle\Listener\LocaleListener"

My routing:

homepage:
    pattern:  /{_locale}
    defaults: { _controller: FKMyWebsiteBundle:Default:index, _locale: en }
    requirements:
        _locale: en|fr|cn
about:
    pattern:  /{_locale}/about
    defaults: { _controller: FKMyWebsiteBundle:Default:about, _locale: en }
    requirements:
        _locale: en|fr|cn
  • 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-13T01:34:09+00:00Added an answer on June 13, 2026 at 1:34 am

    The declaration of the LocaleListener in yml (inspired by the current declaration of the new LocaleListener: \vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Resources\config\web.xml)

    services:
        my_listener:
            class: "FK\MyWebsiteBundle\Listener\LocaleListener"
            arguments: [%locale%]
            tags:
                -  { name: kernel.event_subscriber }
    

    Some snippets:

    A language switcher in your template:

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

    A redirection with locale change from a controller:

    $LocalizedUrl = $this->get('router')->generate(
        $request->attributes->get('_route'), 
        ['_locale' => $locale] + $request->attributes->get('_route_params')
    );
    
    return new \Symfony\Component\HttpFoundation\RedirectResponse($LocalizedUrl);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to build a locale switcher in Rails. I have a great
I have been trying to install the R language with homebrew but when I
I have been trying to learn a cross platform language with a fast learning
I have been trying some programs in the C Language and come across to
I have a legacy Java (not my native language) app that I'm trying to
I've been trying to use buildExpressionParser to parse a language, and I almost have
I'm currently trying to learn ACSL, a precompiled language for Fortran, and have found
I'm trying to have specific folders for each language in Views. (I know this
I have been trying to detect the browser language preference using JavaScript. If I
At work I have a programming language encoded in a database record. I'm trying

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.