embed resource name in error message

This commit is contained in:
Christian Flothmann 2020-05-11 09:40:02 +02:00
parent 1e1060f2eb
commit 507a5963e4
2 changed files with 16 additions and 1 deletions

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\Translation\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\Exception\RuntimeException;
use Symfony\Component\Translation\Loader\ArrayLoader;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Translator;
@ -552,6 +553,16 @@ class TranslatorTest extends TestCase
// unchanged if it can't be found
$this->assertEquals('some_message2', $translator->transChoice('some_message2', 10, ['%count%' => 10]));
}
public function testMissingLoaderForResourceError()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('No loader is registered for the "twig" format when loading the "messages.en.twig" resource.');
$translator = new Translator('en');
$translator->addResource('twig', 'messages.en.twig', 'en');
$translator->getCatalogue('en');
}
}
class StringClass

View File

@ -376,7 +376,11 @@ EOF
if (isset($this->resources[$locale])) {
foreach ($this->resources[$locale] as $resource) {
if (!isset($this->loaders[$resource[0]])) {
throw new RuntimeException(sprintf('The "%s" translation loader is not registered.', $resource[0]));
if (\is_string($resource[1])) {
throw new RuntimeException(sprintf('No loader is registered for the "%s" format when loading the "%s" resource.', $resource[0], $resource[1]));
}
throw new RuntimeException(sprintf('No loader is registered for the "%s" format.', $resource[0]));
}
$this->catalogues[$locale]->addCatalogue($this->loaders[$resource[0]]->load($resource[1], $locale, $resource[2]));
}