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 dfdbc04

Browse filesBrowse files
committed
[translation][loading resources] added test.
1 parent 20f7d11 commit dfdbc04
Copy full SHA for dfdbc04

File tree

8 files changed

+62
-36
lines changed
Filter options

8 files changed

+62
-36
lines changed

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TranslatorPass.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TranslatorPass.php
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public function process(ContainerBuilder $container)
4242

4343
$translatorDefinition = $container->findDefinition('translator.default');
4444
$translatorDefinition->replaceArgument(2, $loaders);
45-
$translatorDefinition->replaceArgument(3, $container->getParameter('translator.resource.directories'));
45+
if ($container->hasParameter('translator.resource_directories')) {
46+
$translatorDefinition->replaceArgument(4, $container->getParameter('translator.resource_directories'));
47+
}
4648
}
4749
}

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
683683
}
684684

685685
// Register translation resources
686-
$container->setParameter('translator.resource.directories', $dirs);
686+
$container->setParameter('translator.resource_directories', $dirs);
687687
if ($dirs) {
688688
foreach ($dirs as $dir) {
689689
$container->addResource(new DirectoryResource($dir));

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/translation.xml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/translation.xml
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@
4040
<argument type="service" id="service_container" />
4141
<argument type="service" id="translator.selector" />
4242
<argument type="collection" /> <!-- translation loaders -->
43-
<argument type="collection" /> <!-- translation resources -->
4443
<argument type="collection">
4544
<argument key="cache_dir">%kernel.cache_dir%/translations</argument>
4645
<argument key="debug">%kernel.debug%</argument>
4746
</argument>
47+
<argument type="collection" /> <!-- translation resources -->
4848
</service>
4949

5050
<service id="translator.logging" class="Symfony\Component\Translation\LoggingTranslator" public="false">

‎src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/TranslatorPassTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/TranslatorPassTest.php
+6-2Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function testValidCollector()
2828

2929
$container = $this->getMock(
3030
'Symfony\Component\DependencyInjection\ContainerBuilder',
31-
array('hasDefinition', 'getDefinition', 'findTaggedServiceIds', 'findDefinition', 'getParameter')
31+
array('hasDefinition', 'getDefinition', 'findTaggedServiceIds', 'findDefinition', 'hasParameter', 'getParameter')
3232
);
3333
$container->expects($this->any())
3434
->method('hasDefinition')
@@ -42,9 +42,13 @@ public function testValidCollector()
4242
$container->expects($this->once())
4343
->method('findDefinition')
4444
->will($this->returnValue($this->getMock('Symfony\Component\DependencyInjection\Definition')));
45+
$container->expects($this->once())
46+
->method('hasParameter')
47+
->with('translator.resource_directories')
48+
->will($this->returnValue(true));
4549
$container->expects($this->once())
4650
->method('getParameter')
47-
->with('translator.resource.directories')
51+
->with('translator.resource_directories')
4852
->will($this->returnValue(array()));
4953

5054
$pass = new TranslatorPass();

‎src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php
+8-17Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -222,31 +222,22 @@ public function testTranslator()
222222
$this->assertTrue($container->hasDefinition('translator.default'), '->registerTranslatorConfiguration() loads translation.xml');
223223
$this->assertEquals('translator.default', (string) $container->getAlias('translator'), '->registerTranslatorConfiguration() redefines translator service from identity to real translator');
224224

225-
$resources = array();
226-
foreach ($container->getDefinition('translator.default')->getMethodCalls() as $call) {
227-
if ('addResource' == $call[0]) {
228-
$resources[] = $call[1];
229-
}
230-
}
225+
$dirs = $container->getParameter('translator.resource_directories');
231226

232-
$files = array_map(function ($resource) { return realpath($resource[1]); }, $resources);
233227
$ref = new \ReflectionClass('Symfony\Component\Validator\Validation');
234228
$this->assertContains(
235-
strtr(dirname($ref->getFileName()).'/Resources/translations/validators.en.xlf', '/', DIRECTORY_SEPARATOR),
236-
$files,
237-
'->registerTranslatorConfiguration() finds Validator translation resources'
229+
dirname($ref->getFileName()).'/Resources/translations',
230+
$dirs
238231
);
239232
$ref = new \ReflectionClass('Symfony\Component\Form\Form');
240233
$this->assertContains(
241-
strtr(dirname($ref->getFileName()).'/Resources/translations/validators.en.xlf', '/', DIRECTORY_SEPARATOR),
242-
$files,
243-
'->registerTranslatorConfiguration() finds Form translation resources'
234+
dirname($ref->getFileName()).'/Resources/translations',
235+
$dirs
244236
);
245-
$ref = new \ReflectionClass('Symfony\Component\Security\Core\Security');
237+
$ref = new \ReflectionClass('Symfony\Component\Security\Core\Exception\AuthenticationException');
246238
$this->assertContains(
247-
strtr(dirname($ref->getFileName()).'/Resources/translations/security.en.xlf', '/', DIRECTORY_SEPARATOR),
248-
$files,
249-
'->registerTranslatorConfiguration() finds Security translation resources'
239+
dirname($ref->getFileName()).'/../Resources/translations',
240+
$dirs
250241
);
251242

252243
$calls = $container->getDefinition('translator.default')->getMethodCalls();
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
folder: répertoire

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php
+40-12Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,39 @@ public function testTransWithCaching()
9595
public function testTransWithCachingWithInvalidLocale()
9696
{
9797
$loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface');
98-
$translator = $this->getTranslator($loader, array('cache_dir' => $this->tmpDir), '\Symfony\Bundle\FrameworkBundle\Tests\Translation\TranslatorWithInvalidLocale');
98+
$translator = $this->getTranslator($loader, array('cache_dir' => $this->tmpDir), array(), 'loader','\Symfony\Bundle\FrameworkBundle\Tests\Translation\TranslatorWithInvalidLocale');
9999
$translator->setLocale('invalid locale');
100100

101101
$this->setExpectedException('\InvalidArgumentException');
102102
$translator->trans('foo');
103103
}
104104

105+
public function testLoadRessourcesWithCaching()
106+
{
107+
$loader = new \Symfony\Component\Translation\Loader\YamlFileLoader();
108+
109+
// prime the cache
110+
$translator = $this->getTranslator($loader, array('cache_dir' => $this->tmpDir), array(__DIR__.'/../Fixtures/Resources/translations'), 'yml');
111+
$translator->setLocale('fr');
112+
113+
$this->assertEquals('répertoire', $translator->trans('folder'));
114+
115+
// do it another time as the cache is primed now
116+
$translator = $this->getTranslator($loader, array('cache_dir' => $this->tmpDir), array(), 'yml');
117+
$translator->setLocale('fr');
118+
119+
$this->assertEquals('répertoire', $translator->trans('folder'));
120+
}
121+
122+
public function testLoadRessourcesWithoutCaching()
123+
{
124+
$loader = new \Symfony\Component\Translation\Loader\YamlFileLoader();
125+
$translator = $this->getTranslator($loader, array(), array(__DIR__.'/../Fixtures/Resources/translations'), 'yml');
126+
$translator->setLocale('fr');
127+
128+
$this->assertEquals('répertoire', $translator->trans('folder'));
129+
}
130+
105131
protected function getCatalogue($locale, $messages)
106132
{
107133
$catalogue = new MessageCatalogue($locale);
@@ -182,23 +208,25 @@ protected function getContainer($loader)
182208
return $container;
183209
}
184210

185-
public function getTranslator($loader, $options = array(), $translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator')
211+
public function getTranslator($loader, $options = array(), $resources = array(), $loaderFomat = 'loader', $translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator')
186212
{
187213
$translator = new $translatorClass(
188214
$this->getContainer($loader),
189215
new MessageSelector(),
190-
array('loader' => array('loader')),
191-
array(),
192-
$options
216+
array($loaderFomat => array($loaderFomat)),
217+
$options,
218+
$resources
193219
);
194220

195-
$translator->addResource('loader', 'foo', 'fr');
196-
$translator->addResource('loader', 'foo', 'en');
197-
$translator->addResource('loader', 'foo', 'es');
198-
$translator->addResource('loader', 'foo', 'pt-PT'); // European Portuguese
199-
$translator->addResource('loader', 'foo', 'pt_BR'); // Brazilian Portuguese
200-
$translator->addResource('loader', 'foo', 'fr.UTF-8');
201-
$translator->addResource('loader', 'foo', 'sr@latin'); // Latin Serbian
221+
if ('loader' === $loaderFomat) {
222+
$translator->addResource('loader', 'foo', 'fr');
223+
$translator->addResource('loader', 'foo', 'en');
224+
$translator->addResource('loader', 'foo', 'es');
225+
$translator->addResource('loader', 'foo', 'pt-PT'); // European Portuguese
226+
$translator->addResource('loader', 'foo', 'pt_BR'); // Brazilian Portuguese
227+
$translator->addResource('loader', 'foo', 'fr.UTF-8');
228+
$translator->addResource('loader', 'foo', 'sr@latin'); // Latin Serbian
229+
}
202230

203231
return $translator;
204232
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ class Translator extends BaseTranslator
4343
* @param ContainerInterface $container A ContainerInterface instance
4444
* @param MessageSelector $selector The message selector for pluralization
4545
* @param array $loaderIds An array of loader Ids
46-
* @param array $resourceDirs An array of resource directories
4746
* @param array $options An array of options
47+
* @param array $resourceDirs An array of resource directories
4848
*
4949
* @throws \InvalidArgumentException
5050
*/
51-
public function __construct(ContainerInterface $container, MessageSelector $selector, $loaderIds = array(), $resourceDirs = array(), array $options = array())
51+
public function __construct(ContainerInterface $container, MessageSelector $selector, $loaderIds = array(), array $options = array(), $resourceDirs = array())
5252
{
5353
$this->container = $container;
5454
$this->loaderIds = $loaderIds;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.