Skip to content

Commit 723227f

Browse files
authored
Merge pull request #6 from tehbeard/tehbeard-nested-embed-support
Add support for embed tags.
2 parents 4726dd9 + e082adc commit 723227f

File tree

6 files changed

+33
-5
lines changed

6 files changed

+33
-5
lines changed

src/TwigFunctionsScanner.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
namespace Gettext\Scanner;
1414

1515
use Twig\Environment;
16-
use Twig\Source;
1716
use Twig\Node\Expression\FunctionExpression;
17+
use Twig\Node\ModuleNode;
18+
use Twig\Source;
1819

1920
class TwigFunctionsScanner implements FunctionsScannerInterface
2021
{
@@ -31,7 +32,7 @@ private function createFunction(FunctionExpression $node, string $filename): ?Pa
3132
{
3233
$name = $node->getAttribute('name');
3334

34-
if (! in_array($name, $this->functions, true)) {
35+
if (!in_array($name, $this->functions, true)) {
3536
return null;
3637
}
3738

@@ -66,6 +67,13 @@ private function extractGettextFunctions($token, string $filename, array &$funct
6667
foreach ($token->getIterator() as $subToken) {
6768
$this->extractGettextFunctions($subToken, $filename, $functions);
6869
}
70+
71+
if ($token instanceof ModuleNode) {
72+
$embeddedTemplates = $token->getAttribute('embedded_templates');
73+
foreach ($embeddedTemplates as $embed) {
74+
$this->extractGettextFunctions($embed, $filename, $functions);
75+
}
76+
}
6977
}
7078

7179
public function scan(string $code, string $filename): array

tests/TimberFunctionsScannerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function testTwigFunctionsExtractor()
6262
$file = __DIR__ . '/assets/input.html.twig';
6363
$code = file_get_contents($file);
6464
$functions = $scanner->scan($code, $file);
65-
$this->assertCount(11, $functions);
65+
$this->assertCount(12, $functions);
6666

6767
// text 1
6868
$function = array_shift($functions);
@@ -74,7 +74,7 @@ public function testTwigFunctionsExtractor()
7474

7575
// text 3
7676
$function = array_shift($functions);
77-
$this->cmp($function, $file, 7, '__', [ 'text 3 (with parenthesis)']);
77+
$this->cmp($function, $file, 7, '__', ['text 3 (with parenthesis)']);
7878

7979
// text 4
8080
$function = array_shift($functions);

tests/TimberScannerTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ public function testTwigCodeScanner()
3636
$scanner = new TimberScanner(Translations::create());
3737
list('' => $translations) = $this->initAndGetTranslations($scanner);
3838

39-
$this->assertCount(8, $translations);
39+
$this->assertCount(9, $translations);
4040
$this->assertCount(0, $translations->getHeaders());
4141

4242
$file = __DIR__ . '/assets/default-domain.po';
43+
// Note: This test can fail due to `git config core.autocrlf` converting line endings to CRLF
44+
// Double check any test error output for `#Warning: Strings contain different line endings!`
4345
$this->assertSame(
4446
file_get_contents($file),
4547
(new PoGenerator())->generateString($translations),
@@ -59,6 +61,8 @@ public function testTwigCodeScannerOtherDomains()
5961
$this->assertCount(1, $tr1);
6062
$this->assertCount(1, $tr1->getHeaders());
6163
$file1 = __DIR__ . '/assets/text-domain1.po';
64+
// Note: This test can fail due to `git config core.autocrlf` converting line endings to CRLF
65+
// Double check any test error output for `#Warning: Strings contain different line endings!`
6266
$this->assertSame(
6367
file_get_contents($file1),
6468
(new PoGenerator())->generateString($tr1),
@@ -68,6 +72,8 @@ public function testTwigCodeScannerOtherDomains()
6872
$this->assertCount(2, $tr2);
6973
$this->assertCount(1, $tr2->getHeaders());
7074
$file2 = __DIR__ . '/assets/text-domain2.po';
75+
// Note: This test can fail due to `git config core.autocrlf` converting line endings to CRLF
76+
// Double check any test error output for `#Warning: Strings contain different line endings!`
7177
$this->assertSame(
7278
file_get_contents($file2),
7379
(new PoGenerator())->generateString($tr2),

tests/assets/default-domain.po

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,7 @@ msgstr ""
3636
msgid "text 11 with plural"
3737
msgid_plural "The plural form"
3838
msgstr[0] ""
39+
40+
#: ./tests/assets/input.html.twig:26
41+
msgid "text 12 content inside a block in a embed template"
42+
msgstr ""

tests/assets/embed.html.twig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<div class="embedded-content">
2+
<h2>{{ __("text 13 header in the embed template, this should be scanned only if the file containing it is.") }}</h2>
3+
{% block main %}
4+
{% endblock %}
5+
</div>

tests/assets/input.html.twig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,8 @@
2121
</p>
2222
<p>
2323
</div>
24+
{% embed 'embed.html.twig' %}
25+
{% block main %}
26+
<p>{{ __("text 12 content inside a block in a embed template") }}</p>
27+
{% endblock %}
28+
{% endembed %}

0 commit comments

Comments
 (0)