Commit 9ae407fc authored by Fabien Potencier's avatar Fabien Potencier

Merge branch '1.3'

* 1.3:
  fixed translation override
  removed obsolete cookbook entry
  fixed CS
  updated the docs to show how to restrict a firewall by more than just the URL
parents 6679a4ea 79dec6aa
Translating Validation Messages
===============================
When working with Symfony validator, a common task would be to show localized
validation messages.
In order to do that, you will need to register translator and point to
translated resources::
$app->register(new Silex\Provider\TranslationServiceProvider(), array(
'locale' => 'sr_Latn',
'translator.domains' => array(),
));
$app->before(function () use ($app) {
$app['translator']->addLoader('xlf', new Symfony\Component\Translation\Loader\XliffFileLoader());
$app['translator']->addResource('xlf', __DIR__.'/vendor/symfony/validator/Symfony/Component/Validator/Resources/translations/validators/validators.sr_Latn.xlf', 'sr_Latn', 'validators');
});
And that's all you need to load translations from Symfony ``xlf`` files.
......@@ -129,10 +129,24 @@ under ``/admin/``::
),
);
The ``pattern`` is a regular expression (it can also be a `RequestMatcher
The ``pattern`` is a regular expression on the URL path; the ``http`` setting
tells the security layer to use HTTP basic authentication and the ``users``
entry defines valid users.
If you want to restrict the firewall by more than the URL pattern (like the
HTTP method, the client IP, the hostname, or any Request attributes), use an
instance of a `RequestMatcher
<http://api.symfony.com/master/Symfony/Component/HttpFoundation/RequestMatcher.html>`_
instance); the ``http`` setting tells the security layer to use HTTP basic
authentication and the ``users`` entry defines valid users.
for the ``pattern`` option::
use Symfony/Component/HttpFoundation/RequestMatcher;
$app['security.firewalls'] = array(
'admin' => array(
'pattern' => new RequestMatcher('^/admin', 'example.com', 'POST'),
// ...
),
);
Each user is defined with the following information:
......
......@@ -94,6 +94,22 @@ The above example will result in following routes:
* ``/it/hello/igor`` will return ``Hello igor`` (because of the fallback).
Using Resources
---------------
When translations are stored in a file, you can load them as follows::
$app = new Application();
$app->register(new TranslationServiceProvider());
$app->extend('translator.resources', function ($resources, $app) {
$resources = array_merge($resources, array(
array('array', array('This value should be a valid number.' => 'Cette valeur doit être un nombre.'), 'fr', 'validators'),
));
return $resources;
});
Traits
------
......
......@@ -40,6 +40,11 @@ class TranslationServiceProvider implements ServiceProviderInterface, EventListe
$translator->addLoader('array', new ArrayLoader());
$translator->addLoader('xliff', new XliffFileLoader());
// Register default resources
foreach ($app['translator.resources'] as $resource) {
$translator->addResource($resource[0], $resource[1], $resource[2], $resource[3]);
}
foreach ($app['translator.domains'] as $domain => $data) {
foreach ($data as $locale => $messages) {
$translator->addResource('array', $messages, $locale, $domain);
......@@ -57,6 +62,10 @@ class TranslationServiceProvider implements ServiceProviderInterface, EventListe
return new MessageSelector();
};
$app['translator.resources'] = function ($app) {
return array();
};
$app['translator.domains'] = array();
$app['locale_fallbacks'] = array('en');
$app['translator.cache_dir'] = null;
......
......@@ -36,7 +36,13 @@ class ValidatorServiceProvider implements ServiceProviderInterface
$r = new \ReflectionClass('Symfony\Component\Validator\Validation');
$file = dirname($r->getFilename()).'/Resources/translations/validators.'.$app['locale'].'.xlf';
if (file_exists($file)) {
$app['translator']->addResource('xliff', $file, $app['locale'], 'validators');
$app->extend('translator.resources', function ($resources, $app) use ($file) {
$resources = array_merge(array(
array('xliff', $file, $app['locale'], 'validators'),
), $resources);
return $resources;
});
}
}
......
......@@ -162,22 +162,22 @@ class ExceptionHandlerTest extends \PHPUnit_Framework_TestCase
$errors = 0;
$app->error(function ($e) use (&$errors) {
$errors++;
++$errors;
});
$app->error(function ($e) use (&$errors) {
$errors++;
++$errors;
});
$app->error(function ($e) use (&$errors) {
$errors++;
++$errors;
return new Response('foo exception handler');
});
$app->error(function ($e) use (&$errors) {
// should not execute
$errors++;
++$errors;
});
$request = Request::create('/foo');
......@@ -198,7 +198,7 @@ class ExceptionHandlerTest extends \PHPUnit_Framework_TestCase
$errors = 0;
$app->error(function ($e) use (&$errors) {
$errors++;
++$errors;
});
try {
......
......@@ -31,17 +31,17 @@ class MiddlewareTest extends \PHPUnit_Framework_TestCase
$app->before(function () use (&$i, $test) {
$test->assertEquals(0, $i);
$i++;
++$i;
});
$app->match('/foo', function () use (&$i, $test) {
$test->assertEquals(1, $i);
$i++;
++$i;
});
$app->after(function () use (&$i, $test) {
$test->assertEquals(2, $i);
$i++;
++$i;
});
$request = Request::create('/foo');
......@@ -57,13 +57,13 @@ class MiddlewareTest extends \PHPUnit_Framework_TestCase
$app = new Application();
$app->match('/foo', function () use (&$i) {
$i++;
++$i;
return new Response('foo');
});
$app->after(function () use (&$i) {
$i++;
++$i;
});
$request = Request::create('/foo');
......@@ -81,27 +81,27 @@ class MiddlewareTest extends \PHPUnit_Framework_TestCase
$app->before(function () use (&$i, $test) {
$test->assertEquals(0, $i);
$i++;
++$i;
});
$app->before(function () use (&$i, $test) {
$test->assertEquals(1, $i);
$i++;
++$i;
});
$app->match('/foo', function () use (&$i, $test) {
$test->assertEquals(2, $i);
$i++;
++$i;
});
$app->after(function () use (&$i, $test) {
$test->assertEquals(3, $i);
$i++;
++$i;
});
$app->after(function () use (&$i, $test) {
$test->assertEquals(4, $i);
$i++;
++$i;
});
$request = Request::create('/foo');
......@@ -117,7 +117,7 @@ class MiddlewareTest extends \PHPUnit_Framework_TestCase
$app = new Application();
$app->before(function () use (&$i) {
$i++;
++$i;
});
$app->match('/foo', function () {
......@@ -125,7 +125,7 @@ class MiddlewareTest extends \PHPUnit_Framework_TestCase
});
$app->after(function () use (&$i) {
$i++;
++$i;
});
$app->error(function () {
......@@ -145,11 +145,11 @@ class MiddlewareTest extends \PHPUnit_Framework_TestCase
$app = new Application();
$app->before(function () use (&$i) {
$i++;
++$i;
}, Application::EARLY_EVENT);
$app->after(function () use (&$i) {
$i++;
++$i;
});
$app->error(function () {
......
......@@ -132,4 +132,42 @@ class ValidatorServiceProviderTest extends \PHPUnit_Framework_TestCase
array('email@sample.com', true, 0, 0),
);
}
public function testAddResource()
{
$app = new Application();
$app['locale'] = 'fr';
$app->register(new ValidatorServiceProvider());
$app->register(new TranslationServiceProvider());
$app['translator'] = $app->extend('translator', function ($translator, $app) {
$translator->addResource('array', array('This value should not be blank.' => 'Pas vide'), 'fr', 'validators');
return $translator;
});
$app['validator'];
$this->assertEquals('Pas vide', $app['translator']->trans('This value should not be blank.', array(), 'validators', 'fr'));
}
public function testAddResourceAlternate()
{
$app = new Application();
$app['locale'] = 'fr';
$app->register(new ValidatorServiceProvider());
$app->register(new TranslationServiceProvider());
$app->factory($app->extend('translator.resources', function ($resources, $app) {
$resources = array_merge($resources, array(
array('array', array('This value should not be blank.' => 'Pas vide'), 'fr', 'validators'),
));
return $resources;
}));
$app['validator'];
$this->assertEquals('Pas vide', $app['translator']->trans('This value should not be blank.', array(), 'validators', 'fr'));
}
}
......@@ -35,7 +35,7 @@ class StreamTest extends \PHPUnit_Framework_TestCase
$i = 0;
$stream = function () use (&$i) {
$i++;
++$i;
};
$app = new Application();
......
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