Commit ed8423b9 authored by Fabien Potencier's avatar Fabien Potencier

bug #1086 Fixes #1068 by checking if translation file exists before adding it...

bug #1086 Fixes #1068 by checking if translation file exists before adding it as a resource (CarsonF)

This PR was squashed before being merged into the 1.2 branch (closes #1086).

Discussion
----------

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

Some locales (such as turkish) don't have translation files, which causes errors.

Commits
-------

965f5896 Fixes #1068 by checking if translation file exists before adding it as a resource
parents b8beefa7 965f5896
......@@ -78,7 +78,10 @@ class FormServiceProvider implements ServiceProviderInterface
if (isset($app['translator'])) {
$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
$app['validator'] = $app->share(function ($app) {
if (isset($app['translator'])) {
$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(
......
......@@ -14,12 +14,14 @@ namespace Silex\Tests\Provider;
use Silex\Application;
use Silex\Provider\FormServiceProvider;
use Silex\Provider\TranslationServiceProvider;
use Silex\Provider\ValidatorServiceProvider;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface;
use Symfony\Component\Form\FormTypeGuesserChain;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Translation\Exception\NotFoundResourceException;
class FormServiceProviderTest extends \PHPUnit_Framework_TestCase
{
......@@ -112,6 +114,28 @@ class FormServiceProviderTest extends \PHPUnit_Framework_TestCase
$this->assertFalse($form->isValid());
$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
......
......@@ -12,8 +12,10 @@
namespace Silex\Tests\Provider;
use Silex\Application;
use Silex\Provider\TranslationServiceProvider;
use Silex\Provider\ValidatorServiceProvider;
use Silex\Provider\FormServiceProvider;
use Symfony\Component\Translation\Exception\NotFoundResourceException;
use Symfony\Component\Validator\Constraints as Assert;
use Silex\Tests\Provider\ValidatorServiceProviderTest\Constraint\Custom;
use Silex\Tests\Provider\ValidatorServiceProviderTest\Constraint\CustomValidator;
......@@ -103,6 +105,27 @@ class ValidatorServiceProviderTest extends \PHPUnit_Framework_TestCase
$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()
{
// 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