Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/section-locales-translation-keys.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/theme-check-common': patch
---

Stop `TranslationKeyExists` from reporting `sections.<section-name>.<key>` translations that are defined in the section's own schema `locales`.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,57 @@ describe('Module: TranslationKeyExists', () => {
});
});

it('should ignore section-scoped keys included in the section schema', async () => {
const offenses = await check(
{
'locales/en.default.json': '{}',
'sections/test-section.liquid': `{{ 'sections.test-section.title' | t }}
{{ 'sections.test-section.description.desktop' | t }}
{{ 'sections.test-section.does_not_exist' | t }}
{% schema %}
{
"locales": {
"en": {
"title": "Title",
"description": {
"desktop": "Lorem ipsum"
}
}
}
}
{% endschema %}`,
},
[TranslationKeyExists],
);
expect(offenses).to.have.length(1);
expect(offenses).to.containOffense({
check: TranslationKeyExists.meta.code,
message:
"'sections.test-section.does_not_exist' does not have a matching entry in 'locales/en.default.json' or 'sections/test-section.liquid'",
uri: 'file:///sections/test-section.liquid',
});
});

it('should only resolve section-scoped keys in section files', async () => {
const offenses = await check(
{
'locales/en.default.json': '{}',
'snippets/test-section.liquid': `{{ 'sections.test-section.title' | t }}
{% schema %}
{
"locales": {
"en": {
"title": "Title"
}
}
}
{% endschema %}`,
},
[TranslationKeyExists],
);
expect(offenses).to.have.length(1);
});

it('should not crash if the schema locales does not exist', async () => {
const offenses = await check(
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { parseJSON } from '../../json';
import * as path from '../../path';
import { isSection } from '../../to-schema';
import { LiquidCheckDefinition, Severity, SourceCodeType } from '../../types';
import { isError } from '../../utils';

Expand Down Expand Up @@ -73,10 +75,17 @@ export const TranslationKeyExists: LiquidCheckDefinition = {

if (!defaultTranslations && systemTranslationsKeys.length === 0) return;

const sectionKeyPrefix = isSection(context.file.uri)
? `sections.${path.basename(context.file.uri, '.liquid')}.`
: undefined;

nodes.forEach(({ translationKey, startIndex, endIndex }) => {
if (
keyExists(translationKey, defaultTranslations) ||
keyExists(translationKey, schemaLocales) ||
(sectionKeyPrefix !== undefined &&
translationKey.startsWith(sectionKeyPrefix) &&
keyExists(translationKey.slice(sectionKeyPrefix.length), schemaLocales)) ||
systemTranslationsKeys.includes(translationKey)
) {
return;
Expand Down
Loading