Description
Q | A |
---|---|
Bug report? | yes |
Feature request? | no |
BC Break report? | no |
RFC? | no |
Symfony version | master |
Problem
The InlineFragmentRenderer::createSubRequest method does not use the locale of the main request to populate its subrequest object.
How I discovered this
I discovered this when using the ESI renderer in Twig when no Surrogate-Capability
headers were set:
{{ render_esi(url('footer')) }}
In that case Symfony HTTP Kernel falls back to the InlineFragmentRenderer. The missing locale was particularly annoying because I'm using the Silex\Provider\TranslationServiceProvider()
in my subrequest to translate data. Because of this bug, the translation service provider always falls back to English.
Possible solution
Add to following line of code at the end of The InlineFragmentRenderer::createSubRequest to fix the issue:
$subRequest->setLocale($request->getLocale());
What about the unit tests?
This is the unit test that initially fails, but that passes when the locale is forwarded:
/**
* Test if the locale from the main request is properly forwarded to the subrequest
*/
public function testCreateSubRequestShouldForwardLocale()
{
$method = new \ReflectionMethod('Symfony\Component\HttpKernel\Fragment\InlineFragmentRenderer','createSubRequest');
$method->setAccessible(true);
$request = Request::create('/');
$request->setLocale('fr');
$subRequest = $method->invoke(
new InlineFragmentRenderer(
$this->getKernel(
$this->returnValue(
new Response('foo')
)
)
),'/',$request
);
$this->assertEquals($request->getLocale(),$subRequest->getLocale());
}
A side effect is that other unit tests in InlineFragmentRendererTest.php fail because getKernelExpectingRequest
no longer matches the expected null locale.
But these are a couple of isolated test cases that can be fixed by executing $request->setLocale('en');
on the expected request object.
Pull request
I have a pull request ready to go. Please review and let me know if the proposed fix is acceptable and if you agree that this is a bug and not a feature.
Thx
Thijs