@@ -153,14 +153,37 @@ function extractStorefrontTranslationKeys(content: string): Set<string> {
153153}
154154
155155function extractUtilityTranslateKeys ( content : string ) : Set < string > {
156- // Find translations assigned to variables first
156+ const keys = new Set < string > ( )
157+
158+ // Find all key: parameters in utility.translate calls
159+ const keyPattern = / r e n d e r \s + [ " ' ] u t i l i t y \. t r a n s l a t e [ " ' ] [ \S \s ] * ?k e y : \s * [ " ' ] ( [ ^ " ' ] + ) [ " ' ] / g
160+ const keyMatches = [ ...content . matchAll ( keyPattern ) ]
161+ for ( const match of keyMatches ) {
162+ keys . add ( match [ 1 ] )
163+ }
164+
165+ // Find all t: parameters with string literals
166+ const tStringPattern = / r e n d e r \s + [ " ' ] u t i l i t y \. t r a n s l a t e [ " ' ] [ \S \s ] * ?t : \s * [ " ' ] ( [ ^ " ' ] + ) [ " ' ] / g
167+ const tStringMatches = [ ...content . matchAll ( tStringPattern ) ]
168+ for ( const match of tStringMatches ) {
169+ keys . add ( match [ 1 ] )
170+ }
171+
172+ // Find translations assigned to variables
157173 const assignedTranslations = findTranslationVariables ( content )
158174
159- // Find both direct keys and variable-based keys
160- const directKeys = extractDirectUtilityTranslateKeys ( content )
161- const variableKeys = extractVariableUtilityTranslateKeys ( content , assignedTranslations )
175+ // Find variable-based keys in utility.translate
176+ const variablePattern = / r e n d e r \s + [ " ' ] u t i l i t y \. t r a n s l a t e [ " ' ] [ \S \s ] * ?t : \s * ( \w + ) / g
177+ const varMatches = [ ...content . matchAll ( variablePattern ) ]
178+ for ( const match of varMatches ) {
179+ const varName = match [ 1 ]
180+ const translationKey = assignedTranslations . get ( varName )
181+ if ( translationKey ) {
182+ keys . add ( translationKey )
183+ }
184+ }
162185
163- return new Set ( [ ... directKeys , ... variableKeys ] )
186+ return keys
164187}
165188
166189function findTranslationVariables ( content : string ) : Map < string , string > {
@@ -176,34 +199,6 @@ function findTranslationVariables(content: string): Map<string, string> {
176199 return assignments
177200}
178201
179- function extractDirectUtilityTranslateKeys ( content : string ) : Set < string > {
180- const keys = new Set < string > ( )
181- const pattern = / r e n d e r \s + [ " ' ] u t i l i t y .t r a n s l a t e [ " ' ] [ ^ % ] * k e y : \s * [ " ' ] ( [ ^ " ' ] + ) [ " ' ] / g
182-
183- const matches = [ ...content . matchAll ( pattern ) ]
184- for ( const match of matches ) {
185- keys . add ( match [ 1 ] )
186- }
187-
188- return keys
189- }
190-
191- function extractVariableUtilityTranslateKeys ( content : string , assignedTranslations : Map < string , string > ) : Set < string > {
192- const keys = new Set < string > ( )
193- const pattern = / r e n d e r \s + [ " ' ] u t i l i t y .t r a n s l a t e [ " ' ] [ ^ % ] * t : \s * ( [ ^ \s , } ] + ) / g
194-
195- const matches = [ ...content . matchAll ( pattern ) ]
196- for ( const match of matches ) {
197- const varName = match [ 1 ]
198- const translationKey = assignedTranslations . get ( varName )
199- if ( translationKey ) {
200- keys . add ( translationKey )
201- }
202- }
203-
204- return keys
205- }
206-
207202export async function removeUnreferencedTranslationsFromTheme (
208203 themeDir : string ,
209204 options : CleanOptions = { }
0 commit comments