Symfony 3 handle multiple langages with their URLs

Hi I’m trying to add multiple langages support into my symfony app since 3 days without success.

I want handle URLS depending on user’s locale preference and add link to switch langage when not logged in.

Every things I tried didn’t work I guess I’m doing something wrong.

I want mange two langages French and English.
myapp.com/fr/contactez-nous and myapp.com/en/contact-usfor example.

app/config/config.yml

parameters:
    locale: fr
framework:
    translator: { fallbacks: ["%locale%"] }
    default_locale: '%locale%'

app/config/routing.yml

locale:
    resource: "@StandardBundle/Controller/"
    type: annotation
    requirements:
        _locale: fr|en
    defaults:  { _locale: '%locale%' }

Controller

/**
 * @Route("/{_locale}/contactez-nous", name="contact_fr")
 * @Route("/{_locale}/contact-us", name="contact_en")
 */
public function contactAction(){
}

It works as expected even langage switching using this into my twig’s view

{% if app.request.locale == "en" %}
    <a class="nav-link" href="{{ path(app.request.get('_route'), 
    app.request.get('_route_params')|merge({'_locale': 'fr'})) }}">Français</a>
{% else %}
    <a class="nav-link" href="{{ path(app.request.get('_route'), 
    app.request.get('_route_params')|merge({'_locale': 'en'})) }}">English</a>
{% endif %}

Everything is fine, but when I’m log in with an user which locale is en, symfony doesn’t apply any changes.

Here is how my user is logged in

app/config/security.yml

firewalls:
    main:
    pattern: ^/
        anonymous: true
        guard:
            authenticators:
                - guardAuthenticator

src/MyBundle/Ressources/config/services.yml

services:
    guardAuthenticator:
        class: MyBundleSecurityLoginFormAuthenticator
        autowire: true

MyBundleSecurityLoginFormAuthenticator

class LoginFormAuthenticator extends AbstractFormLoginAuthenticator
{
    public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
    {
        $user = $token->getUser();
        $idUtil = $user->getId();
        $session->set('_locale', $user->getLocale());
        /*
         * ALSO TRIED THIS
         * but after logged in request is not updated :(
        /*
        $request->setLocale($user->getLocale());
        $session->set('_security_main', serialize($token));
        $session->save();
    }
}

I tried this Making the Locale “Sticky” during a User’s Session and many other solutions found accross google without success.

And when I’m log out, I’m redirect here

enter image description here

not to myapp.com/app_dev.php/fr whit associate controller

/**
 * @Route("/{_locale}/", name="home")
 */
public function HomeAction()
{
}

I have to enter locale in the adresse bar manually.

I will realy appreciate any help onto this it start to make me really crazy for something so simple in without framework thank you

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *