Skip to content
This repository has been archived by the owner on Jul 27, 2024. It is now read-only.

Commit

Permalink
Eliminates the use of translations that do not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
hannakebedom committed Sep 2, 2021
1 parent d02375a commit a7498c4
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/theme_check/checks/translation_key_exists.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ def on_variable(node)
"'#{key_node.value}' does not have a matching entry in '#{@theme.default_locale_json.relative_path}'",
node: node,
markup: key_node.value,
)
) do |corrector|
corrector.add_translation_key(@theme, key_node.value, "TODO")
end
end
end

Expand Down
13 changes: 13 additions & 0 deletions lib/theme_check/corrector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,18 @@ def create_default_locale_json(theme)
theme.default_locale_json = JsonFile.new("locales/#{theme.default_locale}.default.json", theme.storage)
theme.default_locale_json.update_contents('{}')
end

def add_translation_key(theme, key, value)
levels = key.split(".")
json = theme.default_locale_json.content

return json[levels[0]] = value if levels.length == 1

json[levels[0]] = {} if json[levels[0]].class != Hash
return json[levels[0]][levels[1]] = value if levels.length == 2

json[levels[0]][levels[1]] = {} if json[levels[0]][levels[1]].class != Hash
return json[levels[0]][levels[1]][levels[2]] = value if levels.length == 3
end
end
end
45 changes: 45 additions & 0 deletions test/checks/translation_key_exists_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,49 @@ def test_counts_shopify_provided_translations_as_defined

assert_offenses('', offenses)
end

def test_creates_missing_keys
theme = make_theme(
"locales/en.default.json" => JSON.dump({}),
"templates/index.liquid" => <<~END,
{{"unknownkey" | t}}
{{"unknown.nested.key" | t}}
{{"unknownkey" | translate}}
END
)

analyzer = ThemeCheck::Analyzer.new(theme, [ThemeCheck::TranslationKeyExists.new], true)
analyzer.analyze_theme
analyzer.correct_offenses

expected = { "unknownkey" => "TODO", "unknown" => { "nested" => { "key" => "TODO" } } }
actual = theme.default_locale_json.content

assert_equal(expected, actual)
end

def test_creates_nested_missing_keys
theme = make_theme(
"locales/en.default.json" => JSON.dump({
key: "TODO",
nested: { key: "TODO" },
samplekey: { unknownkey: { key: "TODO" } },
}),
"templates/index.liquid" => <<~END,
{{"unknownkey" | t}}
{{"nested.unknownkey" | t}}
{{"samplekey.unknownkey.sample" | translate}}
{{"samplekey.example.sample" | translate}}
END
)

analyzer = ThemeCheck::Analyzer.new(theme, [ThemeCheck::TranslationKeyExists.new], true)
analyzer.analyze_theme
analyzer.correct_offenses

expected = { "key" => "TODO", "nested" => { "key" => "TODO", "unknownkey" => "TODO" }, "samplekey" => { "unknownkey" => { "key" => "TODO", "sample" => "TODO" }, "example" => { "sample" => "TODO" } }, "unknownkey" => "TODO" }
actual = theme.default_locale_json.content

assert_equal(expected, actual)
end
end

0 comments on commit a7498c4

Please sign in to comment.