Skip to content

Commit 1733c40

Browse files
committed
Merge pull request #3 from Kazadri/translation/improve-php-ast-extractor
Add test for translatable BackedEnum with explicit match in trans method
2 parents 405c413 + 61ae4b1 commit 1733c40

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/Symfony/Component/Translation/Tests/Extractor/Visitor/TransMethodVisitorTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ public function assertCatalogue(MessageCatalogue $catalogue): void
7171
'translatable-short-fqn '.$expectedNowdoc => 'prefixtranslatable-short-fqn '.$expectedNowdoc,
7272
'translatable-short-fqn concatenated message with heredoc and nowdoc' => 'prefixtranslatable-short-fqn concatenated message with heredoc and nowdoc',
7373
'translatable-short-fqn default domain' => 'prefixtranslatable-short-fqn default domain',
74+
'text_align.left.label' => 'prefixtext_align.left.label',
75+
'text_align.center.label' => 'prefixtext_align.center.label',
76+
'text_align.right.label' => 'prefixtext_align.right.label',
7477
],
7578
'not_messages' => [
7679
'other-domain-test-no-params-short-array' => 'prefixother-domain-test-no-params-short-array',
@@ -111,5 +114,7 @@ public function assertCatalogue(MessageCatalogue $catalogue): void
111114

112115
$this->assertEquals(['sources' => [self::FIXTURES_FOLDER . 'translatable-short-fqn.html.php:2']], $catalogue->getMetadata('translatable-short-fqn single-quoted key'));
113116
$this->assertEquals(['sources' => [self::FIXTURES_FOLDER . 'translatable-short-fqn.html.php:37']], $catalogue->getMetadata('translatable-short-fqn other-domain-test-no-params-short-array', 'not_messages'));
117+
118+
$this->assertEquals(['sources' => [self::FIXTURES_FOLDER . 'translatable-backed-enum.html.php:17']], $catalogue->getMetadata('text_align.left.label'));
114119
}
115120
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
This template is used for translation message extraction tests
2+
<?php
3+
// @See https://symfony.com/doc/current/reference/forms/types/enum.html#example-usage
4+
5+
use Symfony\Contracts\Translation\TranslatableInterface;
6+
use Symfony\Contracts\Translation\TranslatorInterface;
7+
8+
enum TextAlign: string implements TranslatableInterface
9+
{
10+
case Left = 'Left aligned';
11+
case Center = 'Center aligned';
12+
case Right = 'Right aligned';
13+
14+
public function trans(TranslatorInterface $translator, ?string $locale = null): string
15+
{
16+
return match ($this) {
17+
self::Left => $translator->trans('text_align.left.label', locale: $locale),
18+
self::Center => $translator->trans('text_align.center.label', locale: $locale),
19+
self::Right => $translator->trans('text_align.right.label', locale: $locale),
20+
};
21+
}
22+
}

0 commit comments

Comments
 (0)