Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 28 additions & 33 deletions src/utilities/locales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,37 @@ function extractStorefrontTranslationKeys(content: string): Set<string> {
}

function extractUtilityTranslateKeys(content: string): Set<string> {
// Find translations assigned to variables first
const keys = new Set<string>()

// 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<string, string> {
Expand All @@ -176,34 +199,6 @@ function findTranslationVariables(content: string): Map<string, string> {
return assignments
}

function extractDirectUtilityTranslateKeys(content: string): Set<string> {
const keys = new Set<string>()
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<string, string>): Set<string> {
const keys = new Set<string>()
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 = {}
Expand Down