0

I can't figure out why when I traduce with Symfony it returns me the key, not the message traduced. I am using symfony 2.7 and this is my configuration: I have enable the translator in my config.yml file:

translator:      { fallback: ["%locale%"] }

This is my function inside the controller:

/**
 * @Route("/consult", name="showConsult")
 * @Template("AppBundle:Admin:consult.html.twig")
 */
public function showConsult(Request $request)
{
    $request->setLocale('fr');
    var_dump($this->get('translator')->trans("login.version"));
    return array();
}

And this is my translations file in app/Resources/translations

#messages.fr.yml
login.version: Version APP

I have tried also with twig functions:

{{"login.version"|trans}}
{%trans%}'login.version'{%endtrans%}

EDIT: I have also clear chache EDIT2: Ok I have discover that $request->setLocale() is not working. If I force the locale in the config.yml it works. Do you know how I can fix this? I have read this but they did not work for me. I am working with Windows 10 The output is always the key. I get "login.version" instead of "Version APP" Can anybody help me? Thanks in advance

Community
  • 1
  • 1
Adrián Rodriguez
  • 430
  • 11
  • 26
  • where is the file located ? make sure it matches the needed structure http://symfony.com/doc/current/book/translation.html#translation-resource-file-names-and-locations ; try to remove all parts of the yml and make sure there is no yml parse error in it; also try to set "translator: ~" so it is using the default – nixoschu Nov 09 '15 at 12:17
  • Looks obvious but just in case: did you clean up your cache (`php app/console cache:clean --env=dev`)? – Alain Tiemblo Nov 09 '15 at 12:21
  • what file? I have already wrote the path ot eh translations.yml file. I also tried in mybundlefolder/Resources/translations. The config.yml file is app/Resources/config. I have tried with translator: ~ and is not working. – Adrián Rodriguez Nov 11 '15 at 08:08
  • @AlainTiemblo I have cleared cache several times. I have even tried deleting the content inside the folder directly, sorry I forgot to comment it. – Adrián Rodriguez Nov 11 '15 at 08:10
  • `{ fallback: ["%locale%"] }`: what if you replace `%locale%` by `fr`? by the way, i can't reproduce your issue on a fresh install: https://github.com/ninsuo/test-translate, gives me the proper `string(12) "Version APPs" ` – Alain Tiemblo Nov 11 '15 at 08:39
  • You can add locale as a fourth parameter to `trans` function. Like `$this->get('translator')->trans('login.version',null,null,'fr');` – Turdaliev Nursultan Nov 11 '15 at 08:56
  • If its working when you force on `config.yml` then probably you forgot to add `locale` variable inside your `parameters.yml` file? – Turdaliev Nursultan Nov 11 '15 at 09:36
  • Is there where I force it to fr. But if I want to change it with setLocale() it does not change. I want to modify the view can you specify locale with twig functions? – Adrián Rodriguez Nov 14 '15 at 11:10

2 Answers2

0

Your yaml files should be like...

#messages.fr.yml
login:
    version: Version APP
qooplmao
  • 17,622
  • 2
  • 44
  • 69
  • 1
    does not actually matter if login.version or with child elements like you did. you could actually use complete strings like "Login Version": "my translation" as well. it's just the "cleaner" way to do ;) – nixoschu Nov 09 '15 at 12:17
0

I know it's been a long time, but I'm posting it as a reference because I've got a similar problem just today.

I had to implement a Listener to set the locale value, as stated in http://symfony.com/doc/current/session/locale_sticky_session.html

Basically, you have to:

1) Create a Listener:

// src/AppBundle/EventListener/LocaleListener.php
namespace AppBundle\EventListener;

use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class LocaleListener implements EventSubscriberInterface
{
    private $defaultLocale;

    public function __construct($defaultLocale = 'en')
    {
        $this->defaultLocale = $defaultLocale;
    }

    public function onKernelRequest(GetResponseEvent $event)
    {
        $request = $event->getRequest();
        if (!$request->hasPreviousSession()) {
            return;
        }

        // try to see if the locale has been set as a _locale routing parameter
        if ($locale = $request->attributes->get('_locale')) {
            $request->getSession()->set('_locale', $locale);
        } else {
            // if no explicit locale has been set on this request, use one from the session
            $request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
        }
    }

    public static function getSubscribedEvents()
    {
        return array(
            // must be registered after the default Locale listener
            KernelEvents::REQUEST => array(array('onKernelRequest', 15)),
        );
    }
}

2) Register that Listener on src/AppBundle/Resources/config/config-yml:

services:
    app.locale_listener:
        class: AppBundle\EventListener\LocaleListener
        arguments: ['%kernel.default_locale%']
        tags:
            - { name: kernel.event_subscriber }

3) Symply add a _locale parameter on any of your requests and that locale will be sticky for all the user's working session.

Oscar Pérez
  • 4,377
  • 1
  • 17
  • 36