Skip to content

Commit 2322356

Browse files
authored
fix: improve translation key detection (#5)
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.
1 parent 28cd423 commit 2322356

1 file changed

Lines changed: 28 additions & 33 deletions

File tree

src/utilities/locales.ts

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,37 @@ function extractStorefrontTranslationKeys(content: string): Set<string> {
153153
}
154154

155155
function 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 = /render\s+["']utility\.translate["'][\S\s]*?key:\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 = /render\s+["']utility\.translate["'][\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 = /render\s+["']utility\.translate["'][\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

166189
function 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 = /render\s+["']utility.translate["'][^%]*key:\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 = /render\s+["']utility.translate["'][^%]*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-
207202
export async function removeUnreferencedTranslationsFromTheme(
208203
themeDir: string,
209204
options: CleanOptions = {}

0 commit comments

Comments
 (0)