Commit 79dec6aa authored by Fabien Potencier's avatar Fabien Potencier

bug #1209 fixed translation override (fabpot)

This PR was merged into the 1.3 branch.

Discussion
----------

fixed translation override

fixes #1041

I've also removed a cookbook entry which is not relevant anymore.

Commits
-------

dd270386 fixed translation override
6d3cc41b removed obsolete cookbook entry
parents 8bd96879 dd270386
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.
...@@ -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