This repository has been archived by the owner on Jul 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathmatching_schema_translations.rb
79 lines (67 loc) · 2.18 KB
/
matching_schema_translations.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# frozen_string_literal: true
module ThemeCheck
class MatchingSchemaTranslations < LiquidCheck
severity :suggestion
category :translation
doc docs_url(__FILE__)
def on_schema(node)
schema = node.inner_json
return if schema.nil?
# Get all locales used in the schema
used_locales = Set.new([theme.default_locale])
visit_object(schema) do |_, locales|
used_locales += locales
end
used_locales = used_locales.to_a
# Check all used locales are defined in each localized keys
visit_object(schema) do |key, locales|
missing = used_locales - locales
if missing.any?
add_offense("#{key} missing translations for #{missing.join(', ')}", node: node) do |corrector|
key = key.split(".")
missing.each do |language|
SchemaHelper.schema_corrector(schema, key + [language], "TODO")
end
corrector.replace_inner_json(node, schema)
end
end
end
check_locales(schema, node: node)
end
private
def check_locales(schema, node:)
locales = schema["locales"]
return unless locales.is_a?(Hash)
default_locale = locales[theme.default_locale]
if default_locale
locales.each_pair do |name, content|
diff = LocaleDiff.new(default_locale, content)
diff.add_as_offenses(self, key_prefix: ["locales", name], node: node, schema: schema)
end
else
add_offense("Missing default locale in key: locales", node: node)
end
end
def visit_object(object, top_path = [], &block)
return unless object.is_a?(Hash)
top_path += [object["id"]] if object["id"].is_a?(String)
object.each_pair do |key, value|
path = top_path + [key]
case value
when Array
value.each do |item|
visit_object(item, path, &block)
end
when Hash
# Localized key
if value[theme.default_locale].is_a?(String)
block.call(path.join("."), value.keys)
# Nested keys
else
visit_object(value, path, &block)
end
end
end
end
end
end