File tree Expand file tree Collapse file tree 3 files changed +49
-2
lines changed
src/Symfony/Component/Translation Expand file tree Collapse file tree 3 files changed +49
-2
lines changed Original file line number Diff line number Diff line change @@ -106,6 +106,11 @@ private function resolveMessagePattern(Node\Expr $expr): ?string
106
106
}
107
107
108
108
$ parts [] = $ this ->resolveExprPart ($ expr );
109
+ // If there is only one string part and does not contains sprintf value(s), TransMethodVisitor already extracted the key.
110
+ if (1 === \count ($ parts ) && !str_contains ($ parts [0 ], '{value} ' ) && !str_contains ($ parts [0 ], '{name} ' )) {
111
+ return null ;
112
+ }
113
+
109
114
$ parts = array_reverse ($ parts );
110
115
111
116
// If any part failed to resolve, abort
@@ -137,6 +142,17 @@ private function resolveExprPart(Node\Expr $expr): ?string
137
142
}
138
143
}
139
144
145
+ if (
146
+ $ expr instanceof Node \Expr \FuncCall &&
147
+ $ expr ->name ->name === 'sprintf '
148
+ ) {
149
+ $ args = $ expr ->args ;
150
+ $ pattern = array_shift ($ args )->value ->value ;
151
+ array_walk ($ args , fn (Node \Arg &$ arg ) => $ arg = $ this ->resolveExprPart ($ arg ->value ));
152
+
153
+ return vsprintf ($ pattern , $ args );
154
+ }
155
+
140
156
return null ; // unsupported part
141
157
}
142
158
Original file line number Diff line number Diff line change 4
4
5
5
use PhpParser \NodeVisitor ;
6
6
use Symfony \Component \Translation \Extractor \Visitor \BackedEnumVisitor ;
7
- use Symfony \Component \Translation \Extractor \Visitor \FormTypeVisitor ;
8
- use Symfony \Component \Translation \Extractor \Visitor \TransMethodVisitor ;
9
7
use Symfony \Component \Translation \MessageCatalogue ;
10
8
11
9
class BackedEnumVisitorTest extends AbstractVisitorTest
@@ -32,6 +30,9 @@ public function assertCatalogue(MessageCatalogue $catalogue): void
32
30
'backed_enum.value_concatenation.foo.label ' => 'prefixbacked_enum.value_concatenation.foo.label ' ,
33
31
'backed_enum.both_concatenation.Foo_foo ' => 'prefixbacked_enum.both_concatenation.Foo_foo ' ,
34
32
'backed_enum.value_concatenation.0 ' => 'prefixbacked_enum.value_concatenation.0 ' ,
33
+ 'backed_enum.value_sprintf.foo ' => 'prefixbacked_enum.value_sprintf.foo ' ,
34
+ 'backed_enum.value_sprintf.Foo.foo ' => 'prefixbacked_enum.value_sprintf.Foo.foo ' ,
35
+ 'backed_enum.value_sprintf.foo.name_concatenation.Foo ' => 'prefixbacked_enum.value_sprintf.foo.name_concatenation.Foo ' ,
35
36
],
36
37
],
37
38
$ catalogue ->all (),
Original file line number Diff line number Diff line change @@ -53,3 +53,33 @@ public function trans(TranslatorInterface $translator, ?string $locale = null):
53
53
return $ translator ->trans ('backed_enum.value_concatenation. ' .$ this ->value , locale: $ locale );
54
54
}
55
55
}
56
+
57
+ enum BackedEnumWithSprintf: string implements TranslatableInterface
58
+ {
59
+ case Foo = 'foo ' ;
60
+
61
+ public function trans (TranslatorInterface $ translator , ?string $ locale = null ): string
62
+ {
63
+ return $ translator ->trans (sprintf ('backed_enum.value_sprintf.%s ' , $ this ->value ), locale: $ locale );
64
+ }
65
+ }
66
+
67
+ enum BackedEnumWithTwoArgumentsInSprintf: string implements TranslatableInterface
68
+ {
69
+ case Foo = 'foo ' ;
70
+
71
+ public function trans (TranslatorInterface $ translator , ?string $ locale = null ): string
72
+ {
73
+ return $ translator ->trans (sprintf ('backed_enum.value_sprintf.%s.%s ' , $ this ->name , $ this ->value ), locale: $ locale );
74
+ }
75
+ }
76
+
77
+ enum BackedEnumWithSprintfAndConcatenation: string implements TranslatableInterface
78
+ {
79
+ case Foo = 'foo ' ;
80
+
81
+ public function trans (TranslatorInterface $ translator , ?string $ locale = null ): string
82
+ {
83
+ return $ translator ->trans (sprintf ('backed_enum.value_sprintf.%s ' , $ this ->value ).'.name_concatenation. ' .$ this ->name , locale: $ locale );
84
+ }
85
+ }
You can’t perform that action at this time.
0 commit comments