mirror of
https://github.com/discourse/discourse.git
synced 2025-06-06 23:07:28 +08:00
Minor improvements to rake i18n:check
This commit is contained in:
@ -2,9 +2,9 @@ require 'i18n/i18n_interpolation_keys_finder'
|
|||||||
require 'yaml'
|
require 'yaml'
|
||||||
|
|
||||||
class LocaleFileChecker
|
class LocaleFileChecker
|
||||||
TYPE_MISSING_INTERPOLATION_KEY = 1
|
TYPE_MISSING_INTERPOLATION_KEYS = 1
|
||||||
TYPE_UNSUPPORTED_INTERPOLATION_KEY = 2
|
TYPE_UNSUPPORTED_INTERPOLATION_KEYS = 2
|
||||||
TYPE_MISSING_PLURAL_KEY = 3
|
TYPE_MISSING_PLURAL_KEYS = 3
|
||||||
|
|
||||||
def check(locale)
|
def check(locale)
|
||||||
@errors = {}
|
@errors = {}
|
||||||
@ -86,8 +86,8 @@ class LocaleFileChecker
|
|||||||
missing_keys.delete("count")
|
missing_keys.delete("count")
|
||||||
end
|
end
|
||||||
|
|
||||||
add_error(keys, TYPE_MISSING_INTERPOLATION_KEY, missing_keys) unless missing_keys.empty?
|
add_error(keys, TYPE_MISSING_INTERPOLATION_KEYS, missing_keys, pluralized: pluralized) unless missing_keys.empty?
|
||||||
add_error(keys, TYPE_UNSUPPORTED_INTERPOLATION_KEY, unsupported_keys) unless unsupported_keys.empty?
|
add_error(keys, TYPE_UNSUPPORTED_INTERPOLATION_KEYS, unsupported_keys, pluralized: pluralized) unless unsupported_keys.empty?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -107,7 +107,7 @@ class LocaleFileChecker
|
|||||||
actual_plural_keys = parent.is_a?(Hash) ? parent.keys : []
|
actual_plural_keys = parent.is_a?(Hash) ? parent.keys : []
|
||||||
missing_plural_keys = expected_plural_keys - actual_plural_keys
|
missing_plural_keys = expected_plural_keys - actual_plural_keys
|
||||||
|
|
||||||
add_error(keys, TYPE_MISSING_PLURAL_KEY, missing_plural_keys) unless missing_plural_keys.empty?
|
add_error(keys, TYPE_MISSING_PLURAL_KEYS, missing_plural_keys, pluralized: true) unless missing_plural_keys.empty?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -136,10 +136,17 @@ class LocaleFileChecker
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_error(keys, type, details)
|
def add_error(keys, type, details, pluralized:)
|
||||||
@errors[@relative_locale_path] ||= []
|
@errors[@relative_locale_path] ||= []
|
||||||
|
|
||||||
|
if pluralized
|
||||||
|
joined_key = keys[1..-2].join(".") << " [#{keys.last}]"
|
||||||
|
else
|
||||||
|
joined_key = keys[1..-1].join(".")
|
||||||
|
end
|
||||||
|
|
||||||
@errors[@relative_locale_path] << {
|
@errors[@relative_locale_path] << {
|
||||||
key: keys[1..-1].join("."),
|
key: joined_key,
|
||||||
type: type,
|
type: type,
|
||||||
details: details.to_s
|
details: details.to_s
|
||||||
}
|
}
|
||||||
|
@ -32,12 +32,12 @@ task "i18n:check", [:locale] => [:environment] do |_, args|
|
|||||||
errors.each do |error|
|
errors.each do |error|
|
||||||
message =
|
message =
|
||||||
case error[:type]
|
case error[:type]
|
||||||
when LocaleFileChecker::TYPE_MISSING_INTERPOLATION_KEY
|
when LocaleFileChecker::TYPE_MISSING_INTERPOLATION_KEYS
|
||||||
"Missing interpolation key".red
|
"Missing interpolation keys".red
|
||||||
when LocaleFileChecker::TYPE_UNSUPPORTED_INTERPOLATION_KEY
|
when LocaleFileChecker::TYPE_UNSUPPORTED_INTERPOLATION_KEYS
|
||||||
"Unsupported interpolation key".red
|
"Unsupported interpolation keys".red
|
||||||
when LocaleFileChecker::TYPE_MISSING_PLURAL_KEY
|
when LocaleFileChecker::TYPE_MISSING_PLURAL_KEYS
|
||||||
"Missing plural key".yellow
|
"Missing plural keys".yellow
|
||||||
end
|
end
|
||||||
details = error[:details] ? ": #{error[:details]}" : ""
|
details = error[:details] ? ": #{error[:details]}" : ""
|
||||||
|
|
||||||
@ -49,4 +49,5 @@ task "i18n:check", [:locale] => [:environment] do |_, args|
|
|||||||
failed_locales.each do |failed_locale|
|
failed_locales.each do |failed_locale|
|
||||||
puts "", "Failed to check locale files for #{failed_locale}".red
|
puts "", "Failed to check locale files for #{failed_locale}".red
|
||||||
end
|
end
|
||||||
|
exit 1 unless failed_locales.empty?
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user