Skip to content
Merged
Show file tree
Hide file tree
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
18 changes: 9 additions & 9 deletions src/utilities/locales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,17 @@ function extractStorefrontTranslationKeys(content: string): Set<string> {
}
}

// Combine with t_with_fallback translations
return new Set([...keys, ...extractTWithFallbackTranslationKeys(content)])
// Combine with utility.translate translations
return new Set([...keys, ...extractUtilityTranslateKeys(content)])
}

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

// Find both direct keys and variable-based keys
const directKeys = extractDirectTWithFallbackTranslationKeys(content)
const variableKeys = extractVariableTWithFallbackTranslationKeys(content, assignedTranslations)
const directKeys = extractDirectUtilityTranslateKeys(content)
const variableKeys = extractVariableUtilityTranslateKeys(content, assignedTranslations)

return new Set([...directKeys, ...variableKeys])
}
Expand All @@ -176,9 +176,9 @@ function findTranslationVariables(content: string): Map<string, string> {
return assignments
}

function extractDirectTWithFallbackTranslationKeys(content: string): Set<string> {
function extractDirectUtilityTranslateKeys(content: string): Set<string> {
const keys = new Set<string>()
const pattern = /render\s+["']t_with_fallback["'][^%]*key:\s*["']([^"']+)["']/g
const pattern = /render\s+["']utility.translate["'][^%]*key:\s*["']([^"']+)["']/g

const matches = [...content.matchAll(pattern)]
for (const match of matches) {
Expand All @@ -188,9 +188,9 @@ function extractDirectTWithFallbackTranslationKeys(content: string): Set<string>
return keys
}

function extractVariableTWithFallbackTranslationKeys(content: string, assignedTranslations: Map<string, string>): Set<string> {
function extractVariableUtilityTranslateKeys(content: string, assignedTranslations: Map<string, string>): Set<string> {
const keys = new Set<string>()
const pattern = /render\s+["']t_with_fallback["'][^%]*t:\s*([^\s,}]+)/g
const pattern = /render\s+["']utility.translate["'][^%]*t:\s*([^\s,}]+)/g

const matches = [...content.matchAll(pattern)]
for (const match of matches) {
Expand Down
4 changes: 2 additions & 2 deletions test/commands/theme/locale/clean.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ describe('theme locale clean', () => {

const storefrontContent = JSON.parse(fs.readFileSync(path.join(testThemeLocalesPath, 'en.default.json'), 'utf8'))
expect(storefrontContent).to.have.nested.property('actions.add_to_cart')
expect(storefrontContent).to.have.nested.property('t_with_fallback.direct_key')
expect(storefrontContent).to.have.nested.property('t_with_fallback.variable_key')
expect(storefrontContent).to.have.nested.property('translate.direct_key')
expect(storefrontContent).to.have.nested.property('translate.variable_key')

const schemaContent = JSON.parse(fs.readFileSync(path.join(testThemeLocalesPath, 'en.default.schema.json'), 'utf8'))
expect(schemaContent).to.have.nested.property('section.name')
Expand Down
16 changes: 8 additions & 8 deletions test/commands/theme/locale/sync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ describe('theme locale sync', () => {

const storefrontContent = JSON.parse(fs.readFileSync(path.join(testThemeLocalesPath, 'en.default.json'), 'utf8'))
expect(storefrontContent).to.have.nested.property('actions.add_to_cart')
expect(storefrontContent).to.have.nested.property('t_with_fallback.direct_key')
expect(storefrontContent).to.have.nested.property('t_with_fallback.variable_key')
expect(storefrontContent).to.have.nested.property('translate.direct_key')
expect(storefrontContent).to.have.nested.property('translate.variable_key')

const frStorefrontContent = JSON.parse(fs.readFileSync(path.join(testThemeLocalesPath, 'fr.json'), 'utf8'))
expect(frStorefrontContent).to.have.nested.property('actions.add_to_cart')
expect(frStorefrontContent).to.have.nested.property('t_with_fallback.direct_key')
expect(frStorefrontContent).to.have.nested.property('t_with_fallback.variable_key')
expect(frStorefrontContent).to.have.nested.property('translate.direct_key')
expect(frStorefrontContent).to.have.nested.property('translate.variable_key')

expect(frStorefrontContent.actions.add_to_cart).to.equal(sourceFr.actions.add_to_cart)
})
Expand Down Expand Up @@ -136,8 +136,8 @@ describe('theme locale sync', () => {
expect(storefrontContent).to.not.have.property('unused')

expect(storefrontContent).to.have.nested.property('actions.add_to_cart')
expect(storefrontContent).to.have.nested.property('t_with_fallback.direct_key')
expect(storefrontContent).to.have.nested.property('t_with_fallback.variable_key')
expect(storefrontContent).to.have.nested.property('translate.direct_key')
expect(storefrontContent).to.have.nested.property('translate.variable_key')
})

it('syncs schema files with add-and-override mode', async () => {
Expand Down Expand Up @@ -170,8 +170,8 @@ describe('theme locale sync', () => {
expect(schemaContent).to.not.have.nested.property('additional.new_setting')

expect(storefrontContent).to.have.nested.property('actions.add_to_cart')
expect(storefrontContent).to.have.nested.property('t_with_fallback.direct_key')
expect(storefrontContent).to.have.nested.property('t_with_fallback.variable_key')
expect(storefrontContent).to.have.nested.property('translate.direct_key')
expect(storefrontContent).to.have.nested.property('translate.variable_key')
expect(schemaContent).to.have.nested.property('section.name')
expect(schemaContent).to.have.nested.property('section.settings.logo_label')
})
Expand Down
8 changes: 4 additions & 4 deletions test/fixtures/locales/en.default.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
"additional": {
"new_key": "This is a new translation (source)"
},
"t_with_fallback": {
"direct_key": "Direct key (source)",
"variable_key": "Variable key (source)"
},
"tags": {
"new": "New",
"sale": "Sale",
"featured": "Featured",
"custom": "Custom Tag",
"special": "Special Tag"
},
"translate": {
"direct_key": "Direct key (source)",
"variable_key": "Variable key (source)"
}
}
8 changes: 4 additions & 4 deletions test/fixtures/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
"additional": {
"new_key": "Ceci est une nouvelle traduction (source)"
},
"t_with_fallback": {
"direct_key": "Clé directe (source)",
"variable_key": "Clé variable (source)"
},
"tags": {
"new": "Nouveau",
"sale": "En solde",
"featured": "En vedette",
"custom": "Personnalisée",
"special": "Spéciale"
},
"translate": {
"direct_key": "Clé directe (source)",
"variable_key": "Clé variable (source)"
}
}
2 changes: 1 addition & 1 deletion test/fixtures/theme/locales/en.default.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"actions": {
"add_to_cart": "Add to cart"
},
"t_with_fallback": {
"translate": {
"direct_key": "Direct key",
"variable_key": "Variable key"
},
Expand Down
6 changes: 3 additions & 3 deletions test/fixtures/theme/snippets/t-with-fallback-usage.liquid
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{%- assign translation_key = 't_with_fallback.variable_key' | t -%}
{%- render 't_with_fallback', t: translation_key -%}
{%- render 't_with_fallback', key: 't_with_fallback.direct_key' -%}
{%- assign translation_key = 'translate.variable_key' | t -%}
{%- render 'utility.translate', t: translation_key -%}
{%- render 'utility.translate', key: 'translate.direct_key' -%}