Commit 965f5896 authored by Carson Full's avatar Carson Full Committed by Fabien Potencier

Fixes #1068 by checking if translation file exists before adding it as a resource

parent f64ac7b8
...@@ -78,7 +78,10 @@ class FormServiceProvider implements ServiceProviderInterface ...@@ -78,7 +78,10 @@ class FormServiceProvider implements ServiceProviderInterface
if (isset($app['translator'])) { if (isset($app['translator'])) {
$r = new \ReflectionClass('Symfony\Component\Form\Form'); $r = new \ReflectionClass('Symfony\Component\Form\Form');
$app['translator']->addResource('xliff', dirname($r->getFilename()).'/Resources/translations/validators.'.$app['locale'].'.xlf', $app['locale'], 'validators'); $file = dirname($r->getFilename()).'/Resources/translations/validators.'.$app['locale'].'.xlf';
if (file_exists($file)) {
$app['translator']->addResource('xliff', $file, $app['locale'], 'validators');
}
} }
} }
......
...@@ -31,7 +31,10 @@ class ValidatorServiceProvider implements ServiceProviderInterface ...@@ -31,7 +31,10 @@ class ValidatorServiceProvider implements ServiceProviderInterface
$app['validator'] = $app->share(function ($app) { $app['validator'] = $app->share(function ($app) {
if (isset($app['translator'])) { if (isset($app['translator'])) {
$r = new \ReflectionClass('Symfony\Component\Validator\Validator'); $r = new \ReflectionClass('Symfony\Component\Validator\Validator');
$app['translator']->addResource('xliff', dirname($r->getFilename()).'/Resources/translations/validators.'.$app['locale'].'.xlf', $app['locale'], 'validators'); $file = dirname($r->getFilename()).'/Resources/translations/validators.'.$app['locale'].'.xlf';
if (file_exists($file)) {
$app['translator']->addResource('xliff', $file, $app['locale'], 'validators');
}
} }
return new Validator( return new Validator(
......
...@@ -14,12 +14,14 @@ namespace Silex\Tests\Provider; ...@@ -14,12 +14,14 @@ namespace Silex\Tests\Provider;
use Silex\Application; use Silex\Application;
use Silex\Provider\FormServiceProvider; use Silex\Provider\FormServiceProvider;
use Silex\Provider\TranslationServiceProvider; use Silex\Provider\TranslationServiceProvider;
use Silex\Provider\ValidatorServiceProvider;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\AbstractTypeExtension; use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface; use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface;
use Symfony\Component\Form\FormTypeGuesserChain; use Symfony\Component\Form\FormTypeGuesserChain;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\OptionsResolver\OptionsResolverInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Translation\Exception\NotFoundResourceException;
class FormServiceProviderTest extends \PHPUnit_Framework_TestCase class FormServiceProviderTest extends \PHPUnit_Framework_TestCase
{ {
...@@ -112,6 +114,28 @@ class FormServiceProviderTest extends \PHPUnit_Framework_TestCase ...@@ -112,6 +114,28 @@ class FormServiceProviderTest extends \PHPUnit_Framework_TestCase
$this->assertFalse($form->isValid()); $this->assertFalse($form->isValid());
$this->assertContains('ERROR: German translation', $form->getErrorsAsString()); $this->assertContains('ERROR: German translation', $form->getErrorsAsString());
} }
public function testFormServiceProviderWillNotAddNonexistentTranslationFiles()
{
$app = new Application(array(
'locale' => 'nonexistent',
));
$app->register(new FormServiceProvider());
$app->register(new ValidatorServiceProvider());
$app->register(new TranslationServiceProvider(), array(
'locale_fallbacks' => array(),
));
$app['form.factory'];
$translator = $app['translator'];
try {
$translator->trans('test');
} catch (NotFoundResourceException $e) {
$this->fail('Form factory should not add a translation resource that does not exist');
}
}
} }
class DummyFormType extends AbstractType class DummyFormType extends AbstractType
......
...@@ -12,8 +12,10 @@ ...@@ -12,8 +12,10 @@
namespace Silex\Tests\Provider; namespace Silex\Tests\Provider;
use Silex\Application; use Silex\Application;
use Silex\Provider\TranslationServiceProvider;
use Silex\Provider\ValidatorServiceProvider; use Silex\Provider\ValidatorServiceProvider;
use Silex\Provider\FormServiceProvider; use Silex\Provider\FormServiceProvider;
use Symfony\Component\Translation\Exception\NotFoundResourceException;
use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\Constraints as Assert;
use Silex\Tests\Provider\ValidatorServiceProviderTest\Constraint\Custom; use Silex\Tests\Provider\ValidatorServiceProviderTest\Constraint\Custom;
use Silex\Tests\Provider\ValidatorServiceProviderTest\Constraint\CustomValidator; use Silex\Tests\Provider\ValidatorServiceProviderTest\Constraint\CustomValidator;
...@@ -103,6 +105,27 @@ class ValidatorServiceProviderTest extends \PHPUnit_Framework_TestCase ...@@ -103,6 +105,27 @@ class ValidatorServiceProviderTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($nbEmailError, count($form->offsetGet('email')->getErrors())); $this->assertEquals($nbEmailError, count($form->offsetGet('email')->getErrors()));
} }
public function testValidatorWillNotAddNonexistentTranslationFiles()
{
$app = new Application(array(
'locale' => 'nonexistent',
));
$app->register(new ValidatorServiceProvider());
$app->register(new TranslationServiceProvider(), array(
'locale_fallbacks' => array(),
));
$app['validator'];
$translator = $app['translator'];
try {
$translator->trans('test');
} catch (NotFoundResourceException $e) {
$this->fail('Validator should not add a translation resource that does not exist');
}
}
public function testValidatorConstraintProvider() public function testValidatorConstraintProvider()
{ {
// Email, form is valid , nb global error, nb email error // Email, form is valid , nb global error, nb email error
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment