mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
Adds a check for invalid message formats to rake i18n:check
This commit is contained in:
parent
3c09026fe4
commit
f13c34aaed
@ -5,6 +5,7 @@ class LocaleFileChecker
|
|||||||
TYPE_MISSING_INTERPOLATION_KEYS = 1
|
TYPE_MISSING_INTERPOLATION_KEYS = 1
|
||||||
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
|
||||||
|
|
||||||
def check(locale)
|
def check(locale)
|
||||||
@errors = {}
|
@errors = {}
|
||||||
@ -19,8 +20,7 @@ class LocaleFileChecker
|
|||||||
|
|
||||||
check_interpolation_keys
|
check_interpolation_keys
|
||||||
check_plural_keys
|
check_plural_keys
|
||||||
|
check_message_format
|
||||||
# TODO check MessageFormat
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@errors
|
@errors
|
||||||
@ -111,6 +111,27 @@ class LocaleFileChecker
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def check_message_format
|
||||||
|
mf_locale, mf_filename = JsLocaleHelper.find_message_format_locale([@locale], true)
|
||||||
|
|
||||||
|
traverse_hash(@locale_yaml, []) do |keys, value|
|
||||||
|
next unless keys.last.ends_with?("_MF")
|
||||||
|
|
||||||
|
begin
|
||||||
|
JsLocaleHelper.with_context do |ctx|
|
||||||
|
ctx.load(mf_filename) if File.exist?(mf_filename)
|
||||||
|
ctx.eval("mf = new MessageFormat('#{mf_locale}');")
|
||||||
|
ctx.eval("mf.precompile(mf.parse(#{value.to_s.inspect}))")
|
||||||
|
end
|
||||||
|
rescue MiniRacer::EvalError => error
|
||||||
|
error_message = error.message.sub(/at undefined[:\d]+/, "").strip
|
||||||
|
add_error(keys, TYPE_INVALID_MESSAGE_FORMAT, error_message, pluralized: false)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
JsLocaleHelper.reset_context
|
||||||
|
end
|
||||||
|
|
||||||
def reference_value(keys)
|
def reference_value(keys)
|
||||||
value = @reference_yaml[REFERENCE_LOCALE]
|
value = @reference_yaml[REFERENCE_LOCALE]
|
||||||
|
|
||||||
|
@ -37,7 +37,9 @@ task "i18n:check", [:locale] => [:environment] do |_, args|
|
|||||||
when LocaleFileChecker::TYPE_UNSUPPORTED_INTERPOLATION_KEYS
|
when LocaleFileChecker::TYPE_UNSUPPORTED_INTERPOLATION_KEYS
|
||||||
"Unsupported interpolation keys".red
|
"Unsupported interpolation keys".red
|
||||||
when LocaleFileChecker::TYPE_MISSING_PLURAL_KEYS
|
when LocaleFileChecker::TYPE_MISSING_PLURAL_KEYS
|
||||||
"Missing plural keys".yellow
|
"Missing plural keys".magenta
|
||||||
|
when LocaleFileChecker::TYPE_INVALID_MESSAGE_FORMAT
|
||||||
|
"Invalid message format".yellow
|
||||||
end
|
end
|
||||||
details = error[:details] ? ": #{error[:details]}" : ""
|
details = error[:details] ? ": #{error[:details]}" : ""
|
||||||
|
|
||||||
@ -49,5 +51,7 @@ 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
|
||||||
|
|
||||||
|
puts ""
|
||||||
exit 1 unless failed_locales.empty?
|
exit 1 unless failed_locales.empty?
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user