Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit baf988d

Browse files
committed
bug #23057 [Translation][FrameworkBundle] Fix resource loading order inconsistency reported in #23034 (mpdude)
This PR was squashed before being merged into the 2.7 branch (closes #23057). Discussion ---------- [Translation][FrameworkBundle] Fix resource loading order inconsistency reported in #23034 | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #23034 | License | MIT | Doc PR | Fixes the bug reported in #23034: When mixing `addResource()` calls and providing the `resource_files` option, the order in which resources are loaded depends on the `kernel.debug` setting and whether a cache is used. In particular, when several loaders provide translations for the same message, the one that "wins" may change between development and production mode. Commits ------- 2a9e65d [Translation][FrameworkBundle] Fix resource loading order inconsistency reported in #23034
2 parents d44f143 + 2a9e65d commit baf988d

File tree

2 files changed

+67
-7
lines changed

2 files changed

+67
-7
lines changed

src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,51 @@ public function testGetDefaultLocale()
135135
$this->assertSame('en', $translator->getLocale());
136136
}
137137

138+
/** @dataProvider getDebugModeAndCacheDirCombinations */
139+
public function testResourceFilesOptionLoadsBeforeOtherAddedResources($debug, $enableCache)
140+
{
141+
$someCatalogue = $this->getCatalogue('some_locale', array());
142+
143+
$loader = $this->getMockBuilder('Symfony\Component\Translation\Loader\LoaderInterface')->getMock();
144+
145+
$loader->expects($this->at(0))
146+
->method('load')
147+
/* The "messages.some_locale.loader" is passed via the resource_file option and shall be loaded first */
148+
->with('messages.some_locale.loader', 'some_locale', 'messages')
149+
->willReturn($someCatalogue);
150+
151+
$loader->expects($this->at(1))
152+
->method('load')
153+
/* This resource is added by an addResource() call and shall be loaded after the resource_files */
154+
->with('second_resource.some_locale.loader', 'some_locale', 'messages')
155+
->willReturn($someCatalogue);
156+
157+
$options = array(
158+
'resource_files' => array('some_locale' => array('messages.some_locale.loader')),
159+
'debug' => $debug,
160+
);
161+
162+
if ($enableCache) {
163+
$options['cache_dir'] = $this->tmpDir;
164+
}
165+
166+
/** @var Translator $translator */
167+
$translator = $this->createTranslator($loader, $options);
168+
$translator->addResource('loader', 'second_resource.some_locale.loader', 'some_locale', 'messages');
169+
170+
$translator->trans('some_message', array(), null, 'some_locale');
171+
}
172+
173+
public function getDebugModeAndCacheDirCombinations()
174+
{
175+
return array(
176+
array(false, false),
177+
array(true, false),
178+
array(false, true),
179+
array(true, true),
180+
);
181+
}
182+
138183
protected function getCatalogue($locale, $messages, $resources = array())
139184
{
140185
$catalogue = new MessageCatalogue($locale);

src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ class Translator extends BaseTranslator implements WarmableInterface
3737
*/
3838
private $resourceLocales;
3939

40+
/**
41+
* Holds parameters from addResource() calls so we can defer the actual
42+
* parent::addResource() calls until initialize() is executed.
43+
*
44+
* @var array
45+
*/
46+
private $resources = array();
47+
4048
/**
4149
* Constructor.
4250
*
@@ -65,9 +73,7 @@ public function __construct(ContainerInterface $container, MessageSelector $sele
6573

6674
$this->options = array_merge($this->options, $options);
6775
$this->resourceLocales = array_keys($this->options['resource_files']);
68-
if (null !== $this->options['cache_dir'] && $this->options['debug']) {
69-
$this->loadResources();
70-
}
76+
$this->addResourceFiles($this->options['resource_files']);
7177

7278
parent::__construct($container->getParameter('kernel.default_locale'), $selector, $this->options['cache_dir'], $this->options['debug']);
7379
}
@@ -93,6 +99,11 @@ public function warmUp($cacheDir)
9399
}
94100
}
95101

102+
public function addResource($format, $resource, $locale, $domain = null)
103+
{
104+
$this->resources[] = array($format, $resource, $locale, $domain);
105+
}
106+
96107
/**
97108
* {@inheritdoc}
98109
*/
@@ -104,22 +115,26 @@ protected function initializeCatalogue($locale)
104115

105116
protected function initialize()
106117
{
107-
$this->loadResources();
118+
foreach ($this->resources as $key => $params) {
119+
list($format, $resource, $locale, $domain) = $params;
120+
parent::addResource($format, $resource, $locale, $domain);
121+
}
122+
$this->resources = array();
123+
108124
foreach ($this->loaderIds as $id => $aliases) {
109125
foreach ($aliases as $alias) {
110126
$this->addLoader($alias, $this->container->get($id));
111127
}
112128
}
113129
}
114130

115-
private function loadResources()
131+
private function addResourceFiles($filesByLocale)
116132
{
117-
foreach ($this->options['resource_files'] as $locale => $files) {
133+
foreach ($filesByLocale as $locale => $files) {
118134
foreach ($files as $key => $file) {
119135
// filename is domain.locale.format
120136
list($domain, $locale, $format) = explode('.', basename($file), 3);
121137
$this->addResource($format, $file, $locale, $domain);
122-
unset($this->options['resource_files'][$locale][$key]);
123138
}
124139
}
125140
}

0 commit comments

Comments
 (0)