Commit dd270386 authored by Fabien Potencier's avatar Fabien Potencier

fixed translation override

parent 6d3cc41b
...@@ -93,6 +93,22 @@ The above example will result in following routes: ...@@ -93,6 +93,22 @@ The above example will result in following routes:
* ``/it/hello/igor`` will return ``Hello igor`` (because of the fallback). * ``/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 Traits
------ ------
......
...@@ -40,6 +40,11 @@ class TranslationServiceProvider implements ServiceProviderInterface ...@@ -40,6 +40,11 @@ class TranslationServiceProvider implements ServiceProviderInterface
$translator->addLoader('array', new ArrayLoader()); $translator->addLoader('array', new ArrayLoader());
$translator->addLoader('xliff', new XliffFileLoader()); $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 ($app['translator.domains'] as $domain => $data) {
foreach ($data as $locale => $messages) { foreach ($data as $locale => $messages) {
$translator->addResource('array', $messages, $locale, $domain); $translator->addResource('array', $messages, $locale, $domain);
...@@ -49,6 +54,10 @@ class TranslationServiceProvider implements ServiceProviderInterface ...@@ -49,6 +54,10 @@ class TranslationServiceProvider implements ServiceProviderInterface
return $translator; return $translator;
}); });
$app['translator.resources'] = function ($app) {
return array();
};
$app['translator.message_selector'] = $app->share(function () { $app['translator.message_selector'] = $app->share(function () {
return new MessageSelector(); return new MessageSelector();
}); });
......
...@@ -33,7 +33,13 @@ class ValidatorServiceProvider implements ServiceProviderInterface ...@@ -33,7 +33,13 @@ class ValidatorServiceProvider implements ServiceProviderInterface
$r = new \ReflectionClass('Symfony\Component\Validator\Validation'); $r = new \ReflectionClass('Symfony\Component\Validator\Validation');
$file = dirname($r->getFilename()).'/Resources/translations/validators.'.$app['locale'].'.xlf'; $file = dirname($r->getFilename()).'/Resources/translations/validators.'.$app['locale'].'.xlf';
if (file_exists($file)) { 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;
});
} }
} }
......
...@@ -135,4 +135,42 @@ class ValidatorServiceProviderTest extends \PHPUnit_Framework_TestCase ...@@ -135,4 +135,42 @@ class ValidatorServiceProviderTest extends \PHPUnit_Framework_TestCase
array('email@sample.com', true, 0, 0), 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->share($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->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'));
}
} }
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