Skip to content
This repository has been archived by the owner on Jul 4, 2018. It is now read-only.

Commit

Permalink
fixed translation registration for the validators
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Sep 15, 2015
1 parent c31743e commit 4f560c9
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 25 deletions.
2 changes: 1 addition & 1 deletion doc/changelog.rst
Expand Up @@ -4,7 +4,7 @@ Changelog
1.3.4 (2015-XX-XX)
------------------

* n/a
* fixed translation registration for the validators

1.3.3 (2015-09-08)
------------------
Expand Down
8 changes: 0 additions & 8 deletions src/Silex/Provider/FormServiceProvider.php
Expand Up @@ -78,14 +78,6 @@ public function register(Application $app)

if (isset($app['validator'])) {
$extensions[] = new FormValidatorExtension($app['validator']);

if (isset($app['translator'])) {
$r = new \ReflectionClass('Symfony\Component\Form\Form');
$file = dirname($r->getFilename()).'/Resources/translations/validators.'.$app['locale'].'.xlf';
if (file_exists($file)) {
$app['translator']->addResource('xliff', $file, $app['locale'], 'validators');
}
}
}

return $extensions;
Expand Down
16 changes: 16 additions & 0 deletions src/Silex/Provider/TranslationServiceProvider.php
Expand Up @@ -40,6 +40,22 @@ public function register(Application $app)
$translator->addLoader('array', new ArrayLoader());
$translator->addLoader('xliff', new XliffFileLoader());

if (isset($app['validator'])) {
$r = new \ReflectionClass('Symfony\Component\Validator\Validation');
$file = dirname($r->getFilename()).'/Resources/translations/validators.'.$app['locale'].'.xlf';
if (file_exists($file)) {
$translator->addResource('xliff', $file, $app['locale'], 'validators');
}
}

if (isset($app['form.factory'])) {
$r = new \ReflectionClass('Symfony\Component\Form\Form');
$file = dirname($r->getFilename()).'/Resources/translations/validators.'.$app['locale'].'.xlf';
if (file_exists($file)) {
$translator->addResource('xliff', $file, $app['locale'], 'validators');
}
}

// Register default resources
foreach ($app['translator.resources'] as $resource) {
$translator->addResource($resource[0], $resource[1], $resource[2], $resource[3]);
Expand Down
14 changes: 0 additions & 14 deletions src/Silex/Provider/ValidatorServiceProvider.php
Expand Up @@ -29,20 +29,6 @@ class ValidatorServiceProvider implements ServiceProviderInterface
public function register(Application $app)
{
$app['validator'] = $app->share(function ($app) {
if (isset($app['translator'])) {
$r = new \ReflectionClass('Symfony\Component\Validator\Validation');
$file = dirname($r->getFilename()).'/Resources/translations/validators.'.$app['locale'].'.xlf';
if (file_exists($file)) {
$app->extend('translator.resources', function ($resources, $app) use ($file) {
$resources = array_merge(array(
array('xliff', $file, $app['locale'], 'validators'),
), $resources);

return $resources;
});
}
}

return $app['validator.builder']->getValidator();
});

Expand Down
14 changes: 12 additions & 2 deletions tests/Silex/Tests/Provider/ValidatorServiceProviderTest.php
Expand Up @@ -150,7 +150,10 @@ public function getTestValidatorConstraintProvider()
);
}

public function testAddResource()
/**
* @dataProvider getAddResourceData
*/
public function testAddResource($registerValidatorFirst)
{
$app = new Application();
$app['locale'] = 'fr';
Expand All @@ -163,11 +166,18 @@ public function testAddResource()
return $translator;
}));

$app['validator'];
if ($registerValidatorFirst) {
$app['validator'];
}

$this->assertEquals('Pas vide', $app['translator']->trans('This value should not be blank.', array(), 'validators', 'fr'));
}

public function getAddResourceData()
{
return array(array(false), array(true));
}

public function testAddResourceAlternate()
{
$app = new Application();
Expand Down

0 comments on commit 4f560c9

Please sign in to comment.