forked from Shopify/theme-check
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslation_key_exists.rb
59 lines (50 loc) · 1.86 KB
/
translation_key_exists.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# frozen_string_literal: true
module ThemeCheck
class TranslationKeyExists < LiquidCheck
severity :error
category :translation
doc docs_url(__FILE__)
def initialize
@schema_locales = {}
@nodes = {}
end
def on_document(node)
@nodes[node.theme_file.name] = []
end
def on_variable(node)
return unless @theme.default_locale_json&.content&.is_a?(Hash)
return unless node.filters.any? { |name, _| name == "t" || name == "translate" }
@nodes[node.theme_file.name] << node
end
def on_schema(node)
if (schema_locales = node.inner_json&.dig("locales", @theme.default_locale))
@schema_locales = schema_locales
end
end
def on_end
@nodes.each_pair do |_file_name, file_nodes|
file_nodes.each do |node|
next unless (key_node = node.children.first)
next unless key_node.value.is_a?(String)
next if key_exists?(key_node.value, @theme.default_locale_json.content) || key_exists?(key_node.value, @schema_locales) || ShopifyLiquid::SystemTranslations.include?(key_node.value)
add_offense(
@schema_locales.empty? ? "'#{key_node.value}' does not have a matching entry in '#{@theme.default_locale_json.relative_path}'" : "'#{key_node.value}' does not have a matching entry in '#{@theme.default_locale_json.relative_path}' or '#{node.theme_file.relative_path}'",
node: node,
markup: key_node.value
) do |corrector|
corrector.add_translation(@theme.default_locale_json, key_node.value.split("."), "TODO")
end
end
end
end
private
def key_exists?(key, pointer)
key.split(".").each do |token|
return false unless pointer.is_a?(Hash)
return false unless pointer.key?(token)
pointer = pointer[token]
end
true
end
end
end