DEV: Update `i18n:check` rake task to detect invalid Markdown links (#13728)
In addition to that it fixes a problem where the check failed on empty locale files and allows calling the rake task with multiple locales.
This commit is contained in:
parent
2318bd66a7
commit
2a95f892af
|
@ -8,6 +8,7 @@ class LocaleFileChecker
|
||||||
TYPE_UNSUPPORTED_INTERPOLATION_KEYS = 2
|
TYPE_UNSUPPORTED_INTERPOLATION_KEYS = 2
|
||||||
TYPE_MISSING_PLURAL_KEYS = 3
|
TYPE_MISSING_PLURAL_KEYS = 3
|
||||||
TYPE_INVALID_MESSAGE_FORMAT = 4
|
TYPE_INVALID_MESSAGE_FORMAT = 4
|
||||||
|
TYPE_INVALID_MARKDOWN_LINK = 5
|
||||||
|
|
||||||
def check(locale)
|
def check(locale)
|
||||||
@errors = {}
|
@errors = {}
|
||||||
|
@ -20,9 +21,12 @@ class LocaleFileChecker
|
||||||
@locale_yaml = YAML.load_file(locale_path)
|
@locale_yaml = YAML.load_file(locale_path)
|
||||||
@reference_yaml = YAML.load_file(reference_path)
|
@reference_yaml = YAML.load_file(reference_path)
|
||||||
|
|
||||||
|
next if @locale_yaml.blank? || @locale_yaml.first[1].blank?
|
||||||
|
|
||||||
check_interpolation_keys
|
check_interpolation_keys
|
||||||
check_plural_keys
|
check_plural_keys
|
||||||
check_message_format
|
check_message_format
|
||||||
|
check_markdown_links
|
||||||
end
|
end
|
||||||
|
|
||||||
@errors
|
@errors
|
||||||
|
@ -93,6 +97,16 @@ class LocaleFileChecker
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def check_markdown_links
|
||||||
|
traverse_hash(@locale_yaml, []) do |keys, value|
|
||||||
|
next if value.is_a?(Array)
|
||||||
|
|
||||||
|
if /\[.*?\]\s+\(.*?\)/.match?(value)
|
||||||
|
add_error(keys, TYPE_INVALID_MARKDOWN_LINK, nil, pluralized: false)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def check_plural_keys
|
def check_plural_keys
|
||||||
known_parent_keys = Set.new
|
known_parent_keys = Set.new
|
||||||
|
|
||||||
|
|
|
@ -6,15 +6,19 @@ require 'seed_data/topics'
|
||||||
require 'colored2'
|
require 'colored2'
|
||||||
|
|
||||||
desc "Checks locale files for errors"
|
desc "Checks locale files for errors"
|
||||||
task "i18n:check", [:locale] => [:environment] do |_, args|
|
task "i18n:check" => [:environment] do |_, args|
|
||||||
failed_locales = []
|
failed_locales = []
|
||||||
|
|
||||||
if args[:locale].present?
|
if args.extras.present?
|
||||||
if LocaleSiteSetting.valid_value?(args[:locale])
|
locales = []
|
||||||
locales = [args[:locale]]
|
|
||||||
else
|
args.extras.each do |locale|
|
||||||
puts "ERROR: #{locale} is not a valid locale"
|
if LocaleSiteSetting.valid_value?(locale)
|
||||||
exit 1
|
locales << locale
|
||||||
|
else
|
||||||
|
puts "ERROR: #{locale} is not a valid locale"
|
||||||
|
exit 1
|
||||||
|
end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
locales = LocaleSiteSetting.supported_locales
|
locales = LocaleSiteSetting.supported_locales
|
||||||
|
@ -44,8 +48,10 @@ task "i18n:check", [:locale] => [:environment] do |_, args|
|
||||||
"Missing plural keys".magenta
|
"Missing plural keys".magenta
|
||||||
when LocaleFileChecker::TYPE_INVALID_MESSAGE_FORMAT
|
when LocaleFileChecker::TYPE_INVALID_MESSAGE_FORMAT
|
||||||
"Invalid message format".yellow
|
"Invalid message format".yellow
|
||||||
|
when LocaleFileChecker::TYPE_INVALID_MARKDOWN_LINK
|
||||||
|
"Invalid markdown links".yellow
|
||||||
end
|
end
|
||||||
details = error[:details] ? ": #{error[:details]}" : ""
|
details = error[:details].present? ? ": #{error[:details]}" : ""
|
||||||
|
|
||||||
puts error[:key] << " -- " << message << details
|
puts error[:key] << " -- " << message << details
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue