silexphp / Silex-WebProfiler

MIT License
210 stars 61 forks source link

Silex Web Profiler broken TranslationService #23

Closed juanmiguelbesada closed 10 years ago

juanmiguelbesada commented 11 years ago

An example is worth a thousand words. Simple App (Silex Fat + SilexWebProfiler)

composer.json

{
    "require": {
        "silex/silex": "~1.0",
        "silex/web-profiler": "~1.0",
        "symfony/browser-kit": "~2.1",
        "symfony/console": "~2.1",
        "symfony/config": "~2.1",
        "symfony/css-selector": "~2.1",
        "symfony/dom-crawler": "~2.1",
        "symfony/filesystem": "~2.1",
        "symfony/finder": "~2.1",
        "symfony/form": "~2.1",
        "symfony/locale": "~2.1",
        "symfony/process": "~2.1",
        "symfony/security": "~2.1",
        "symfony/serializer": "~2.1",
        "symfony/translation": "~2.1",
        "symfony/validator": "~2.1",
        "symfony/monolog-bridge": "~2.1",
        "symfony/twig-bridge": "~2.1",
        "doctrine/dbal": ">=2.2.0,<2.4.0-dev",
        "swiftmailer/swiftmailer": "5.*"
    }
}

web\index.php

<?php

require_once __DIR__.'/../vendor/autoload.php';

$app = new Silex\Application();
$app->register(new Silex\Provider\ServiceControllerServiceProvider());
$app->register(new Silex\Provider\TwigServiceProvider());
$app->register(new Silex\Provider\UrlGeneratorServiceProvider());
/*$app->register(new Silex\Provider\WebProfilerServiceProvider(), array(
    'profiler.cache_dir' => __DIR__.'/../cache/profiler',
    'profiler.mount_prefix' => '/_profiler', // this is the default
));*/

$app->register(new Silex\Provider\TranslationServiceProvider(), array(
    'locale_fallback' => 'en',
));
$app['translator.domains'] = array(
    'messages' => array(
        'en' => array(
            'hello'     => 'Hello %name%',
            'goodbye'   => 'Goodbye %name%',
        ),
        'de' => array(
            'hello'     => 'Hallo %name%',
            'goodbye'   => 'Tschüss %name%',
        ),
        'fr' => array(
            'hello'     => 'Bonjour %name%',
            'goodbye'   => 'Au revoir %name%',
        ),
    ),
    'validators' => array(
        'fr' => array(
            'This value should be a valid number.' => 'Cette valeur doit être un nombre.',
        ),
    ),
);

$app->get('/{_locale}/{message}/{name}', function ($message, $name) use ($app) {
    return $app['translator']->trans($message, array('%name%' => $name));
});

$app->run();

Run:

http://localhost/silex/web/index.php/en/hello/SoutlinK
http://localhost/silex/web/index.php/de/hello/SoutlinK
http://localhost/silex/web/index.php/fr/hello/SoutlinK

Result, it works as spected.

Now uncoment this lines:

$app->register(new Silex\Provider\WebProfilerServiceProvider(), array(
    'profiler.cache_dir' => __DIR__.'/../cache/profiler',
    'profiler.mount_prefix' => '/_profiler', // this is the default
));

Run again:

http://localhost/silex/web/index.php/en/hello/SoutlinK
http://localhost/silex/web/index.php/de/hello/SoutlinK
http://localhost/silex/web/index.php/fr/hello/SoutlinK

It don't works at all, always use 'en' as locale

davidvandemaele commented 10 years ago

I have noticed the same behavior, so at the moment I don't use the web profiler

fabpot commented 10 years ago

the problem actually does not come from the Web profiler itself, but because of the way we are managing the locale in Silex.

The Web profiler toolbar registration triggers the creation of the Twig instance, which registers the translator extension, which in turn creates the translator, which gets the locale from $app['locale']. But at this point, the locale listener has not been trigered yet and so the locale is always en. Later on, when the locale listener changes the value of $app['locale'], it's too late as the translator has already been created.

I'm closing this issue and I will create a new one on Silex about this issue.

fabpot commented 10 years ago

should be fixed by fabpot/Silex#829