@@ -49,22 +49,38 @@ private static Document FixOldTranslation(CodeFixContext context, SyntaxNode roo
49
49
{
50
50
var diagnosticSpan = diagnostic . Location . SourceSpan ;
51
51
52
+ if ( root is null ) return context . Document ;
53
+
52
54
var invocationExpr = root
53
55
? . FindToken ( diagnosticSpan . Start ) . Parent
54
56
? . AncestorsAndSelf ( )
55
57
. OfType < InvocationExpressionSyntax > ( )
56
- . First ( ) ;
58
+ . FirstOrDefault ( ) ;
57
59
58
- if ( invocationExpr is null || root is null ) return context . Document ;
60
+ if ( invocationExpr is null ) return context . Document ;
59
61
60
62
var argumentList = invocationExpr . ArgumentList . Arguments ;
61
- var argument = argumentList . First ( ) . Expression ;
62
-
63
- if ( GetTranslationKey ( argument ) is string translationKey )
64
- return FixOldTranslationWithoutStringFormat ( context , translationKey , root , invocationExpr ) ;
65
63
66
- if ( GetTranslationKeyFromInnerInvocation ( argument ) is string translationKeyInside )
67
- return FixOldTranslationWithStringFormat ( context , argumentList , translationKeyInside , root , invocationExpr ) ;
64
+ // Loop through the arguments to find the translation key.
65
+ for ( int i = 0 ; i < argumentList . Count ; i ++ )
66
+ {
67
+ var argument = argumentList [ i ] . Expression ;
68
+
69
+ // Case 1: The argument is a literal (direct GetTranslation("key"))
70
+ if ( GetTranslationKey ( argument ) is string translationKey )
71
+ return FixOldTranslationWithoutStringFormat ( context , translationKey , root , invocationExpr ) ;
72
+
73
+ // Case 2: The argument is itself an invocation (nested GetTranslation)
74
+ if ( GetTranslationKeyFromInnerInvocation ( argument ) is string translationKeyInside )
75
+ {
76
+ // If there are arguments following this translation call, treat as a Format call.
77
+ if ( i < argumentList . Count - 1 )
78
+ return FixOldTranslationWithStringFormat ( context , argumentList , translationKeyInside , root , invocationExpr , i ) ;
79
+
80
+ // Otherwise, treat it as a direct translation call.
81
+ return FixOldTranslationWithoutStringFormat ( context , translationKeyInside , root , invocationExpr ) ;
82
+ }
83
+ }
68
84
69
85
return context . Document ;
70
86
}
@@ -94,7 +110,7 @@ private static Document FixOldTranslationWithoutStringFormat(
94
110
private static string GetTranslationKeyFromInnerInvocation ( ExpressionSyntax syntax )
95
111
{
96
112
if ( syntax is InvocationExpressionSyntax invocationExpressionSyntax &&
97
- invocationExpressionSyntax . ArgumentList . Arguments . Count is 1 )
113
+ invocationExpressionSyntax . ArgumentList . Arguments . Count == 1 )
98
114
{
99
115
var firstArgument = invocationExpressionSyntax . ArgumentList . Arguments . First ( ) . Expression ;
100
116
return GetTranslationKey ( firstArgument ) ;
@@ -107,9 +123,11 @@ private static Document FixOldTranslationWithStringFormat(
107
123
SeparatedSyntaxList < ArgumentSyntax > argumentList ,
108
124
string translationKey2 ,
109
125
SyntaxNode root ,
110
- InvocationExpressionSyntax invocationExpr )
126
+ InvocationExpressionSyntax invocationExpr ,
127
+ int translationArgIndex )
111
128
{
112
- var newArguments = string . Join ( ", " , argumentList . Skip ( 1 ) . Select ( a => a . Expression ) ) ;
129
+ // Skip all arguments before and including the translation call
130
+ var newArguments = string . Join ( ", " , argumentList . Skip ( translationArgIndex + 1 ) . Select ( a => a . Expression ) ) ;
113
131
var newInnerInvocationExpr = SyntaxFactory . ParseExpression ( $ "{ Constants . ClassName } .{ translationKey2 } ({ newArguments } )") ;
114
132
115
133
var newRoot = root . ReplaceNode ( invocationExpr , newInnerInvocationExpr ) ;
0 commit comments