1

So in the top navigation bar a user can select a language. It works. This is the code: routing.yml

homepage:
  path: /{_locale}
  defaults: { _controller: MpShopBundle:Homepage:index, "_locale": "es"}
  requirements:
         _locale: es|en

twig:

<li><a href="{{ path('homepage', {'_locale':'en'}) }}">EN</a></li>
<li><a href="{{ path('homepage', {'_locale':'es'}) }}">ES</a></li>

Now for the problem. Lets say a user added some products to the cart and accessed the shopping cart page www.domain.com/cart/shopping-cart. If he presses one of the links he is redirected to the main index page and his language is changed.

How can I change my code, so that when a user selects a language he is not redirected to a new page, but the same page refreshes with the new locale.

For example:

Current page : www.domain.com/cart/shopping-cart

after language select: www.domain.com/cart/shopping-cart/en

and not: www.domain.com/en

The navbar is being extended by all of my pages. So I need to somehow get the current url and add the /en to it.

I get the url like this:

{% set currentPath = path(app.request.attributes.get('_route'),
                     app.request.attributes.get('_route_params')) %}

But how can I add the /en part in twig?

Dominykas55
  • 1,231
  • 14
  • 45

1 Answers1

5

I think this code block answers your question:

<a href="{{ path(app.request.get('_route'), app.request.get('_route_params')|merge({'_locale': 'en'})) }}">English</a>

Taken from this StackOverflow question.

Community
  • 1
  • 1
Rikijs
  • 728
  • 1
  • 12
  • 48