4
votes

I'm having a strange issue with Symfony2.2. I have a project using two languages : en/fr. So I create as usual (like Symfony2.0) two translation files "messages.en.yml" and "messages.fr.yml" in Ressources/Views/translations/. But Translations in twig could not change even if we set the request object and the locale session. Translation is always set by the default_locale (config.php).

Example : if default_locale = en, all my website (in twig) is translated in en, even if i set the _locale object in fr (request and session). Of course if I manually change the default_locale to fr the website is naturally in fr...

However, _locale session works but I don't know if locale request works, and of course translation works in controllers too...

There is my files :

config.yml:

framework:
#esi:            ~
translator:      { fallback: %locale% } # = en
# ...
default_locale: %locale% # = en

Controller :

public function indexAction()
{
    $this->get('session')->set('_locale', 'fr');
    $this->getRequest()->setLocale($lang);
    exit($this->getRequest()->getLocale()); // = fr
    exit($this->get('translator')->trans('Symfony2 is great')); // = Symfony2 est génial

    return $this->render('TestBundle:Controller:test.html.twig');

View :

{% block content %}
<p>lang : {{ app.request.locale }}</p> {#} = "fr", OK{#}
<p>{{ 'Symfony2 is great'|trans }}</p> {#} = "Symfony2 is great", WAIT WHAT?{#}

I must resign myself to force the locale at the beginning of the method controller to have the requested locale (stored in session) like that :

Controller:

if($this->get('session')->get('_locale')){
    $lang = $this->get('session')->get('_locale');
    $this->getRequest()->setLocale($lang);
}

In other words, I do have a problem with the registration of the request object... Because the last code works well in the controller, and shows well the locale in twig page with app.request.locale, but not the translations... (sorry for my bad english and thanks for helping)

2
Not sure if this could be related but have you cleared the cache?cheesemacfly
I'm in dev, but yes i tried to clear the cache, no result...Benji_X80

2 Answers

1
votes

I had the same issue due to the low priority of my event listener. The locale would be overridden by the Translator's TranslatorListener. Increasing the priority of my event listener did the trick for me:

services:
    app.locale_listener:
        class: AppBundle\EventListener\LocaleListener
        tags:
            - { name: kernel.event_listener, priority: 11, ... }

Source: https://github.com/symfony/symfony/issues/12878#issuecomment-68628808

0
votes

Parameter _locale in routing holds your locale value.

Look here on this page

Symfony - Book - Translation - Local and the URL

From Symfony 2.1 they have this kind of logic:

Since you can store the locale of the user in the session, it may be tempting to use the same URL to display a resource in many different languages based on the user's locale. For example, http://www.example.com/contact could show content in English for one user and French for another user. Unfortunately, this violates a fundamental rule of the Web: that a particular URL returns the same resource regardless of the user. To further muddy the problem, which version of the content would be indexed by search engines?

A better policy is to include the locale in the URL. This is fully-supported by the routing system using the special _locale parameter:

Now when you want to sel local, this doesn't work any more

$this->get('session')->set('_locale', 'fr');

You can use request insted of session now but you can not have session-logic with _local from Symfony 2.0 unless you simulate it with event listener on kernel request.