From 579baa79057560cd8000ce6d0ecae9ae28271152 Mon Sep 17 00:00:00 2001 From: Chris Berthe Date: Fri, 25 Apr 2025 11:31:48 -0400 Subject: [PATCH] fix: improve translation key detection Use more robust regex patterns with [\S\s]*? to match across nested structures and line breaks. This simplifies the approach to extract utility.translate keys by directly searching for key: and t: parameters rather than trying to parse different Liquid block structures separately. --- src/utilities/locales.ts | 61 ++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 33 deletions(-) diff --git a/src/utilities/locales.ts b/src/utilities/locales.ts index 9db8807..eabb1cf 100644 --- a/src/utilities/locales.ts +++ b/src/utilities/locales.ts @@ -153,14 +153,37 @@ function extractStorefrontTranslationKeys(content: string): Set { } function extractUtilityTranslateKeys(content: string): Set { - // Find translations assigned to variables first + const keys = new Set() + + // Find all key: parameters in utility.translate calls + const keyPattern = /render\s+["']utility\.translate["'][\S\s]*?key:\s*["']([^"']+)["']/g + const keyMatches = [...content.matchAll(keyPattern)] + for (const match of keyMatches) { + keys.add(match[1]) + } + + // Find all t: parameters with string literals + const tStringPattern = /render\s+["']utility\.translate["'][\S\s]*?t:\s*["']([^"']+)["']/g + const tStringMatches = [...content.matchAll(tStringPattern)] + for (const match of tStringMatches) { + keys.add(match[1]) + } + + // Find translations assigned to variables const assignedTranslations = findTranslationVariables(content) - // Find both direct keys and variable-based keys - const directKeys = extractDirectUtilityTranslateKeys(content) - const variableKeys = extractVariableUtilityTranslateKeys(content, assignedTranslations) + // Find variable-based keys in utility.translate + const variablePattern = /render\s+["']utility\.translate["'][\S\s]*?t:\s*(\w+)/g + const varMatches = [...content.matchAll(variablePattern)] + for (const match of varMatches) { + const varName = match[1] + const translationKey = assignedTranslations.get(varName) + if (translationKey) { + keys.add(translationKey) + } + } - return new Set([...directKeys, ...variableKeys]) + return keys } function findTranslationVariables(content: string): Map { @@ -176,34 +199,6 @@ function findTranslationVariables(content: string): Map { return assignments } -function extractDirectUtilityTranslateKeys(content: string): Set { - const keys = new Set() - const pattern = /render\s+["']utility.translate["'][^%]*key:\s*["']([^"']+)["']/g - - const matches = [...content.matchAll(pattern)] - for (const match of matches) { - keys.add(match[1]) - } - - return keys -} - -function extractVariableUtilityTranslateKeys(content: string, assignedTranslations: Map): Set { - const keys = new Set() - const pattern = /render\s+["']utility.translate["'][^%]*t:\s*([^\s,}]+)/g - - const matches = [...content.matchAll(pattern)] - for (const match of matches) { - const varName = match[1] - const translationKey = assignedTranslations.get(varName) - if (translationKey) { - keys.add(translationKey) - } - } - - return keys -} - export async function removeUnreferencedTranslationsFromTheme( themeDir: string, options: CleanOptions = {}