Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2a9e65d

Browse filesBrowse files
mpdudefabpot
authored andcommitted
[Translation][FrameworkBundle] Fix resource loading order inconsistency reported in #23034
1 parent ccb6543 commit 2a9e65d
Copy full SHA for 2a9e65d

File tree

2 files changed

+67
-7
lines changed
Filter options

2 files changed

+67
-7
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php
+45Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php
+22-7Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.