8000 bug #34711 Fix the translation commands when a template contains a sy… · symfony/symfony@7a7ddc0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7a7ddc0

Browse files
committed
bug #34711 Fix the translation commands when a template contains a syntax error (fabpot)
This PR was merged into the 3.4 branch. Discussion ---------- Fix the translation commands when a template contains a syntax error | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #34586 | License | MIT | Doc PR | n/a When using `debug:translation` or `translation:update`, we should catch exceptions to avoid breaking the command. It was not really an issue before Symfony 4.4/5 as we didn't have templates in the core that use features from optional dependencies. Commits ------- 7f803bc Fix the translation commands when a template contains a syntax error
2 parents 9d78fcc + 7f803bc commit 7a7ddc0

File tree

2 files changed

+8
-24
lines changed

2 files changed

+8
-24
lines changed

src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Symfony\Bridge\Twig\Translation\TwigExtractor;
1717
use Symfony\Component\Translation\MessageCatalogue;
1818
use Twig\Environment;
19-
use Twig\Error\Error;
2019
use Twig\Loader\ArrayLoader;
2120

2221
class TwigExtractorTest extends TestCase
@@ -78,23 +77,15 @@ public function getExtractData()
7877
/**
7978
* @dataProvider resourcesWithSyntaxErrorsProvider
8079
*/
81-
public function testExtractSyntaxError($resources)
80+
public function testExtractSyntaxError($resources, array $messages)
8281
{
83-
$this->expectException('Twig\Error\Error');
8482
$twig = new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock());
8583
$twig->addExtension(new TranslationExtension($this->getMockBuilder('Symfony\Component\Translation\TranslatorInterface')->getMock()));
8684

8785
8000 $extractor = new TwigExtractor($twig);
88-
89-
try {
90-
$extractor->extract($resources, new MessageCatalogue('en'));
91-
} catch (Error $e) {
92-
$this->assertSame(\dirname(__DIR__).strtr('/Fixtures/extractor/syntax_error.twig', '/', \DIRECTORY_SEPARATOR), $e->getFile());
93-
$this->assertSame(1, $e->getLine());
94-
$this->assertSame('Unclosed "block".', $e->getMessage());
95-
96-
throw $e;
97-
}
86+
$catalogue = new MessageCatalogue('en');
87+
$extractor->extract($resources, $catalogue);
88+
$this->assertSame($messages, $catalogue->all());
9889
}
9990

10091
/**
@@ -103,9 +94,9 @@ public function testExtractSyntaxError($resources)
10394
public function resourcesWithSyntaxErrorsProvider()
10495
{
10596
return [
106-
[__DIR__.'/../Fixtures'],
107-
[__DIR__.'/../Fixtures/extractor/syntax_error.twig'],
108-
[new \SplFileInfo(__DIR__.'/../Fixtures/extractor/syntax_error.twig')],
97+
[__DIR__.'/../Fixtures', ['messages' => ['Hi!' => 'Hi!']]],
98+
[__DIR__.'/../Fixtures/extractor/syntax_error.twig', []],
99+
[new \SplFileInfo(__DIR__.'/../Fixtures/extractor/syntax_error.twig'), []],
109100
];
110101
}
111102

src/Symfony/Bridge/Twig/Translation/TwigExtractor.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Bridge\Twig\Translation;
1313

1414
use Symfony\Component\Finder\Finder;
15-
use Symfony\Component\Finder\SplFileInfo;
1615
use Symfony\Component\Translation\Extractor\AbstractFileExtractor;
1716
use Symfony\Component\Translation\Extractor\ExtractorInterface;
1817
use Symfony\Component\Translation\MessageCatalogue;
@@ -58,13 +57,7 @@ public function extract($resource, MessageCatalogue $catalogue)
5857
try {
5958
$this->extractTemplate(file_get_contents($file->getPathname()), $catalogue);
6059
} catch (Error $e) {
61-
if ($file instanceof \SplFileInfo) {
62-
$path = $file->getRealPath() ?: $file->getPathname();
63-
$name = $file instanceof SplFileInfo ? $file->getRelativePathname() : $path;
64-
$e->setSourceContext(new Source('', $name, $path));
65-
}
66-
67-
throw $e;
60+
// ignore errors, these should be fixed by using the linter
6861
}
6962
}
7063
}

0 commit comments

Comments
 (0)
0