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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:45:26+00:00 2026-06-01T19:45:26+00:00

How can i create a link to the current in my template? I want

  • 0

How can i create a link to the current in my template?

I want to create a language switcher which should link to the current page in varios languages, so all parameters should be the same exept for the locale.

  • 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-01T19:45:28+00:00Added an answer on June 1, 2026 at 7:45 pm

    I end up rolling my own function for this. I though at first it was included somewhere in FrameworkBundle but did not find anything about it. Here the steps I took.

    At first, I created a Twig extension function that would output the same route as the one the user is visiting currently (parameters and query string included). I left this step to you. You can look at this link from a good tutorial on Symfony2 for the description of how to create a Twig extension if you don’t know how already. I could help you with it if you need it.

    Next step is to create the function itself that will switch the locale of the current route. This function will need the Request and Router objects as dependencies. In my personal case, I put this function in a dedicated service named RoutingHelper service. This service is then used by my Twig extension function. Here the service definition I added to the dependency container:

    acme.helper.routing:
        class: Application\AcmeBundle\Helper\RoutingHelper
        scope: "request"
        arguments:
            request: "@request"
            router: "@router"
    

    And the constructor of my service:

    protected $request;
    protected $router;
    
    public function __construct($request, $router)
    {
        $this->request = $request;
        $this->router = $router;
    }
    

    The $locale parameter is new locale would like to switch to. Here the function:

    public function localizeCurrentRoute($locale)
    {
        $attributes = $this->request->attributes->all();
        $query = $this->request->query->all();
        $route = $attributes['_route'];
    
        # This will add query parameters to attributes and filter out attributes starting with _
        $attributes = array_merge($query, $this->filterPrivateKeys($attributes));
    
        $attributes['_locale'] = $locale;
    
        return $this->router->generate($route, $attributes);
    }
    

    Essentially, it does what others have put so far but it also handles parameters and query string. The method filterPrivateKeys will remove private attribute from the route attributes. Those attributes are the one starting with an underscore and should not be passed back to the route generator. Here its defintion:

    private function filterPrivateKeys($attributes)
    {
        $filteredAttributes = array();
        foreach ($attributes as $key => $value) {
            if (!empty($key) && $key[0] != '_') {
                $filteredAttributes[$key] = $value;
            }
        }
    
        return $filteredAttributes;
    }
    

    In the end, I’m able to this in my Twig view to create links to switch locales:

    {% block language_bar %}
            <a href="{{ localize_route('en') }}"> English </a>
            <a href="{{ localize_route('fr') }}"> Français </a>
    {% endblock %}
    

    Edit:

    Here my twig extension service definition:

    acme.twig.extension:
        class: Application\AcmeBundle\Twig\Extension\AcmeExtension
        arguments:
          container: "@service_container"
        tags:
          -  { name: twig.extension }
    

    And in the twig extension function I have this call: $routingHelper = $this->container->get('acme.helper.routing');

    This should solve the scope widening exception happening because the twig extension is not in the request scope.

    Update:

    It is now possible with Symfony 2.1 to have a locale switcher in an easier way than before. Indeed, the 2.1 version of Symfony introduced a new parameter for routes that make it more easy to do a locale switcher. Here the code, all in twig

    {% set route_params = app.request.attributes.get('_route_params') %}
    
    {# merge the query string params if you want to keep them when switching the locale #}
    {% set route_params = route_params|merge(app.request.query.all) %}
    
    {# merge the additional params you want to change #}
    {% set route_params = route_params|merge({'_locale': 'fr'}) %}
    
    {{ path(app.request.attributes.get('_route'), route_params) }} 
    

    It is still a few lines of twig code, but could be included in a Twig block for easier reuse. Credits to stof, from the Symfony community, for the code above.

    Hope this is what you are looking for.

    Regards,
    Matt

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a link that can be sent via email which
Can some one provide a link to create a simple server and client using
One can create an anonymous object that is initialized through constructor parameters, such as
I would like to create a new page in Elgg where I can put
I am trying to create a PDF file of current view. Can anyone please
How can i add pass Model-Specific urls to the Template. Let's say, i want
I want to create a UI as shown in the below link: http://i53.tinypic.com/sxksx5.jpg According
I can create a menu item in the Windows Explorer context menu by adding
I can create the following and reference it using area[0].states[0] area[0].cities[0] var area =
I can create a literal long by appending an L to the value; why

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.